mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-18 06:44:22 +01:00
a humble test framework for opengraph stuff.
This commit is contained in:
6
go.mod
6
go.mod
@@ -3,6 +3,7 @@ module github.com/fiatjaf/njump
|
||||
go 1.21.0
|
||||
|
||||
require (
|
||||
github.com/PuerkitoBio/goquery v1.5.0
|
||||
github.com/apatters/go-wordwrap v1.0.0
|
||||
github.com/dgraph-io/badger/v4 v4.2.0
|
||||
github.com/fiatjaf/eventstore v0.2.6
|
||||
@@ -16,6 +17,7 @@ require (
|
||||
github.com/pelletier/go-toml v1.9.5
|
||||
github.com/rs/cors v1.10.0
|
||||
github.com/rs/zerolog v1.29.1
|
||||
github.com/stretchr/testify v1.8.4
|
||||
github.com/tylermmorton/tmpl v0.0.0-20230817025807-fd8b24ce5c3d
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
|
||||
mvdan.cc/xurls/v2 v2.5.0
|
||||
@@ -23,11 +25,13 @@ require (
|
||||
|
||||
require (
|
||||
github.com/andybalholm/brotli v1.0.5 // indirect
|
||||
github.com/andybalholm/cascadia v1.0.0 // indirect
|
||||
github.com/aymerick/douceur v0.2.0 // indirect
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
|
||||
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
|
||||
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
|
||||
github.com/dgraph-io/ristretto v0.1.1 // indirect
|
||||
@@ -49,6 +53,7 @@ require (
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/puzpuzpuz/xsync/v2 v2.5.1 // indirect
|
||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
|
||||
github.com/tidwall/gjson v1.17.0 // indirect
|
||||
@@ -60,4 +65,5 @@ require (
|
||||
golang.org/x/net v0.17.0 // indirect
|
||||
golang.org/x/sys v0.14.0 // indirect
|
||||
google.golang.org/protobuf v1.31.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
12
go.sum
12
go.sum
@@ -1,8 +1,12 @@
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk=
|
||||
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
|
||||
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
|
||||
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
|
||||
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
||||
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||
github.com/apatters/go-wordwrap v1.0.0 h1:G6ni4Pt7/I4ED+A5ZvsK8e9XwETD04veDKxEL2QN830=
|
||||
github.com/apatters/go-wordwrap v1.0.0/go.mod h1:3sM7HcArQ+utXnjDQ4d1xjrd8b/wbKuDr/RmbiHgjwI=
|
||||
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
|
||||
@@ -130,6 +134,10 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
|
||||
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
|
||||
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
|
||||
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/lukevers/freetype-go v0.0.0-20150513150840-77e276735410 h1:ED5jVfC//XhwaBl9B8nB+UQFvI/7Iee03tV4gRVWgMA=
|
||||
github.com/lukevers/freetype-go v0.0.0-20150513150840-77e276735410/go.mod h1:cnsFc3HOpydgckvXF3xq4fvlLFOAuTh4VyJ118x8LQc=
|
||||
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
||||
@@ -210,10 +218,12 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
@@ -287,6 +297,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
|
||||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
||||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
||||
3
justfile
3
justfile
@@ -20,3 +20,6 @@ prettier:
|
||||
|
||||
tailwind:
|
||||
tailwind -i tailwind.css -o static/tailwind-bundle.min.css --minify
|
||||
|
||||
test:
|
||||
go test -tags=nocache
|
||||
|
||||
30
main.go
30
main.go
@@ -9,7 +9,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/fiatjaf/eventstore/badger"
|
||||
eventstore_badger "github.com/fiatjaf/eventstore/badger"
|
||||
"github.com/fiatjaf/khatru"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
@@ -73,16 +73,7 @@ func main() {
|
||||
tailwindDebugStuff = template.HTML(fmt.Sprintf("<script src=\"https://cdn.tailwindcss.com?plugins=typography\"></script><script>\n%s</script><style type=\"text/tailwindcss\">%s</style>", config, style))
|
||||
}
|
||||
|
||||
// initialize disk cache
|
||||
defer cache.initialize()()
|
||||
|
||||
// initialize eventstore database
|
||||
if badgerBackend, ok := db.(*badger.BadgerBackend); ok {
|
||||
// it may be NullStore, in which case we do nothing
|
||||
badgerBackend.Path = s.EventStorePath
|
||||
}
|
||||
db.Init()
|
||||
defer db.Close()
|
||||
initCache()
|
||||
|
||||
// initialize routines
|
||||
ctx := context.Background()
|
||||
@@ -123,3 +114,20 @@ func main() {
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
func initCache() func() {
|
||||
// initialize disk cache
|
||||
deinit := cache.initialize()
|
||||
|
||||
// initialize eventstore database
|
||||
if badgerBackend, ok := db.(*eventstore_badger.BadgerBackend); ok {
|
||||
// it may be NullStore, in which case we do nothing
|
||||
badgerBackend.Path = s.EventStorePath
|
||||
}
|
||||
db.Init()
|
||||
|
||||
return func() {
|
||||
deinit()
|
||||
db.Close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,10 @@ import (
|
||||
"github.com/fiatjaf/eventstore/nullstore"
|
||||
)
|
||||
|
||||
var cache = Cache{}
|
||||
var (
|
||||
cache = Cache{}
|
||||
db eventstore.Store = &nullstore.NullStore{}
|
||||
)
|
||||
|
||||
type Cache struct{}
|
||||
|
||||
@@ -22,5 +25,3 @@ func (c *Cache) SetWithTTL(key string, value []byte, ttl time.Duration) {}
|
||||
func (c *Cache) SetJSONWithTTL(key string, value any, ttl time.Duration) {}
|
||||
func (c *Cache) GetPaginatedKeys(prefix string, page int, size int) []string { return []string{} }
|
||||
func (c *Cache) Delete(key string) {}
|
||||
|
||||
var db eventstore.Store = &nullstore.NullStore{}
|
||||
|
||||
108
opengraph_test.go
Normal file
108
opengraph_test.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type OpengraphFields struct {
|
||||
TwitterCard string
|
||||
TwitterTitle string
|
||||
Superscript string
|
||||
Subscript string
|
||||
Image string
|
||||
Video string
|
||||
VideoFullType string
|
||||
Text string
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
initCache()
|
||||
code := m.Run()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func TestHomePage(t *testing.T) {
|
||||
r := httptest.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
renderEvent(w, r)
|
||||
if w.Code != 200 {
|
||||
t.Fatal("homepage is not 200")
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "<form") {
|
||||
fmt.Println(w.Body.String())
|
||||
t.Fatal("homepage doesn't contain a form")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalShortTextNote(t *testing.T) {
|
||||
og := makeRequest(t, "/nevent1qqswgl5fgcwcrhzmy2u9d3nq6dg7xnsp95657xe0xk9xh6xac43vwsqpqqrf49qe")
|
||||
|
||||
assert.Equal(t, og.Image, "", "")
|
||||
assert.Equal(t, og.TwitterCard, "summary", "")
|
||||
assert.Contains(t, og.Text, "Awesome, good feedback.", "")
|
||||
}
|
||||
|
||||
func TestNoteWithTextImage(t *testing.T) {
|
||||
og := makeRequest(t, "/nevent1qqs860kwt3m500hfnve6vxdpagkfqkm6hq03dnn2n7u8dev580kd2uszyztuwzjyxe4x2dwpgken87tna2rdlhpd02va5cvvgrrywpddnr3jydc2w4t")
|
||||
|
||||
assert.Contains(t, og.Image, "/njump/image/nevent1qqs860kwt3m500hfnve6vxdpagkfqkm6hq03dnn2n7u8dev580kd2uszyztuwzjyxe4x2dwpgken87tna2rdlhpd02va5cvvgrrywpddnr3jydc2w4t", "")
|
||||
assert.Equal(t, og.TwitterCard, "summary_large_image", "")
|
||||
assert.Contains(t, og.Text, "Thanks, it's of course a work in progress", "")
|
||||
}
|
||||
|
||||
func makeRequest(t *testing.T, path string) *OpengraphFields {
|
||||
r := httptest.NewRequest("GET", path, nil)
|
||||
w := httptest.NewRecorder()
|
||||
renderEvent(w, r)
|
||||
|
||||
if w.Code != 200 {
|
||||
t.Fatal("short note is not 200")
|
||||
}
|
||||
|
||||
og := &OpengraphFields{}
|
||||
parseHead(w.Body, og)
|
||||
|
||||
return og
|
||||
}
|
||||
|
||||
func parseHead(resp io.Reader, og *OpengraphFields) error {
|
||||
doc, err := goquery.NewDocumentFromReader(resp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse response with goquery: %w", err)
|
||||
}
|
||||
|
||||
doc.Find(`meta[name="twitter:card"]`).Each(func(_ int, s *goquery.Selection) {
|
||||
og.TwitterCard, _ = s.Attr("content")
|
||||
})
|
||||
doc.Find(`meta[name="twitter:title"]`).Each(func(_ int, s *goquery.Selection) {
|
||||
og.TwitterTitle, _ = s.Attr("content")
|
||||
})
|
||||
doc.Find(`meta[property="og:site_name"]`).Each(func(_ int, s *goquery.Selection) {
|
||||
og.Superscript, _ = s.Attr("content")
|
||||
})
|
||||
doc.Find(`meta[property="og:title"]`).Each(func(_ int, s *goquery.Selection) {
|
||||
og.Subscript, _ = s.Attr("content")
|
||||
})
|
||||
doc.Find(`meta[property="og:image"]`).Each(func(_ int, s *goquery.Selection) {
|
||||
og.Image, _ = s.Attr("content")
|
||||
})
|
||||
doc.Find(`meta[property="og:video"]`).Each(func(_ int, s *goquery.Selection) {
|
||||
og.Video, _ = s.Attr("content")
|
||||
})
|
||||
doc.Find(`meta[property="og:video:type"]`).Each(func(_ int, s *goquery.Selection) {
|
||||
og.VideoFullType, _ = s.Attr("content")
|
||||
})
|
||||
doc.Find(`meta[property="og:description"]`).Each(func(_ int, s *goquery.Selection) {
|
||||
og.Text, _ = s.Attr("content")
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user