-
Notifications
You must be signed in to change notification settings - Fork 24
/
opengraph_test.go
118 lines (100 loc) · 3.46 KB
/
opengraph_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
TelegramAndroidApp string
}
func TestMain(m *testing.M) {
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, "seen on", "")
}
func TestNoteAsTelegramInstantView(t *testing.T) {
og := makeRequest(t, "/nevent1qqs860kwt3m500hfnve6vxdpagkfqkm6hq03dnn2n7u8dev580kd2uszyztuwzjyxe4x2dwpgken87tna2rdlhpd02va5cvvgrrywpddnr3jydc2w4t", "TelegramBot (like TwitterBot)")
assert.Equal(t, og.TelegramAndroidApp, "Medium", "")
}
func makeRequest(t *testing.T, path string, ua string) *OpengraphFields {
r := httptest.NewRequest("GET", path, nil)
r.Header.Set("user-agent", ua)
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")
})
doc.Find(`meta[property="al:android:app_name"]`).Each(func(_ int, s *goquery.Selection) {
og.TelegramAndroidApp, _ = s.Attr("content")
})
return nil
}