-
Notifications
You must be signed in to change notification settings - Fork 7
/
opengraph_test.go
153 lines (141 loc) · 3.7 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package opengraph
import (
"net/http"
"strings"
"testing"
)
var tt1 = `<head>
<title>The Rock (1996)</title>
<meta property="og:title" content="The Rock" />
<meta property="fb:app_id" content="115109575169727" />
<meta property="og:type" content="video.movie" />
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
</head>`
var tt2 = `<head>
<title>The Rock (1996)</title>
<meta property="og:title" content="The Rock" />
<meta property="fb:app_id" content="115109575169727" />
<meta property="" content="video.movie" />
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="" />
</head>`
var tt3 = `<head>
<title>The Rock (1996)</title>
<meta property="og:title" content="The Rock">
<meta property="fb:app_id" content="115109575169727">
<meta property="og:type" content="video.movie">
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/">
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg">
</head>`
var tt4 = `
<head>
<title>Apple Watch Hermès</title>
<meta name="og:title" content="Découvrez la Apple Watch Hermès.">
<meta name="og:site_name" content="Apple Watch Hermès">
<meta name="" content="website">
<meta property="og:locale" name="og:country" content="fr">
<meta name="og:url" content="http://hermes.com/applewatchhermes">
<meta itemprop="name" content="Découvrez la Apple Watch Hermès.">`
func TestExport(t *testing.T) {
var ogTests = []struct {
doc string
prefix string
expected []MetaData
}{
{
"",
"og",
[]MetaData{},
},
{
tt1,
"og",
[]MetaData{
{"title", "The Rock", "og"},
{"type", "video.movie", "og"},
{"url", "http://www.imdb.com/title/tt0117500/", "og"},
{"image", "http://ia.media-imdb.com/images/rock.jpg", "og"},
},
},
{
tt1,
"fb",
[]MetaData{
{"app_id", "115109575169727", "fb"},
},
},
{
tt1,
"",
[]MetaData{
{"title", "The Rock", "og"},
{"app_id", "115109575169727", "fb"},
{"type", "video.movie", "og"},
{"url", "http://www.imdb.com/title/tt0117500/", "og"},
{"image", "http://ia.media-imdb.com/images/rock.jpg", "og"},
},
},
{
tt2,
"og",
[]MetaData{
{"title", "The Rock", "og"},
{"url", "http://www.imdb.com/title/tt0117500/", "og"},
},
},
{
tt3,
"og",
[]MetaData{
{"title", "The Rock", "og"},
{"type", "video.movie", "og"},
{"url", "http://www.imdb.com/title/tt0117500/", "og"},
{"image", "http://ia.media-imdb.com/images/rock.jpg", "og"},
},
},
{
tt4,
"og",
[]MetaData{
{"title", "Découvrez la Apple Watch Hermès.", "og"},
{"site_name", "Apple Watch Hermès", "og"},
{"locale", "fr", "og"},
{"url", "http://hermes.com/applewatchhermes", "og"},
},
},
}
for ti, tt := range ogTests {
og, err := ExtractPrefix(strings.NewReader(tt.doc), tt.prefix)
if err != nil {
t.Errorf("Test %d err: %s", ti, err.Error())
}
if len(og) != len(tt.expected) {
t.Fatalf("Test %d got: %+v, expected: %+v", ti, og, tt.expected)
}
for i, _ := range og {
if og[i].Property != tt.expected[i].Property || og[i].Content != tt.expected[i].Content {
t.Errorf("Test %d got: %+v, expected: %+v", ti, og[i], tt.expected[i])
}
}
}
}
func BenchmarkExport(b *testing.B) {
b.StopTimer()
rdr := strings.NewReader(tt1)
b.StartTimer()
for i := 0; i < b.N; i++ {
Extract(rdr)
}
}
func BenchmarkExport2(b *testing.B) {
b.StopTimer()
r, err := http.Get("http://www.imdb.com/title/tt0118715/")
if err != nil {
b.Skipf("skipping, net/http err: %s", err.Error())
}
b.StartTimer()
for i := 0; i < b.N; i++ {
Extract(r.Body)
}
}