forked from johnreutersward/opengraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopengraph_test.go
More file actions
135 lines (123 loc) · 2.96 KB
/
opengraph_test.go
File metadata and controls
135 lines (123 loc) · 2.96 KB
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
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>`
func TestExport(t *testing.T) {
var ogTests = []struct {
doc string
ns string
expected []MetaData
}{
{
"",
"og:",
[]MetaData{},
},
{
tt1,
"og:",
[]MetaData{
{"title", "The Rock"},
{"type", "video.movie"},
{"url", "http://www.imdb.com/title/tt0117500/"},
{"image", "http://ia.media-imdb.com/images/rock.jpg"},
},
},
{
tt1,
"fb:",
[]MetaData{
{"app_id", "115109575169727"},
},
},
{
tt1,
"",
[]MetaData{
{"og:title", "The Rock"},
{"fb:app_id", "115109575169727"},
{"og:type", "video.movie"},
{"og:url", "http://www.imdb.com/title/tt0117500/"},
{"og:image", "http://ia.media-imdb.com/images/rock.jpg"},
},
},
{
tt2,
"og:",
[]MetaData{
{"title", "The Rock"},
{"url", "http://www.imdb.com/title/tt0117500/"},
},
},
{
tt3,
"og:",
[]MetaData{
{"title", "The Rock"},
{"type", "video.movie"},
{"url", "http://www.imdb.com/title/tt0117500/"},
{"image", "http://ia.media-imdb.com/images/rock.jpg"},
},
},
}
for _, tt := range ogTests {
Namespace = tt.ns
og, err := Extract(strings.NewReader(tt.doc))
if err != nil {
t.Errorf("err: %s", err.Error())
}
if len(og) != len(tt.expected) {
t.Fatalf("got: %+v, expected: %+v", og, tt.expected)
}
for i, _ := range og {
if og[i].Property != tt.expected[i].Property || og[i].Content != tt.expected[i].Content {
t.Errorf("got: %+v, expected: %+v", 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)
}
}