forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_parser_test.go
143 lines (125 loc) · 3.06 KB
/
page_parser_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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPageParserParsePage(t *testing.T) {
_, err := newPageParser(newLinkFinder(nil)).Parse("http://foo.com", nil)
assert.Nil(t, err)
}
func TestPageParserFailWithInvalidURL(t *testing.T) {
_, err := newPageParser(newLinkFinder(nil)).Parse(":", nil)
assert.NotNil(t, err)
}
func TestPageParserSetCorrectURL(t *testing.T) {
s := "http://foo.com"
p, err := newPageParser(newLinkFinder(nil)).Parse(s, nil)
assert.Nil(t, err)
assert.Equal(t, s, p.URL().String())
}
func TestPageParserIgnorePageURLFragment(t *testing.T) {
s := "http://foo.com#id"
p, err := newPageParser(newLinkFinder(nil)).Parse(s, nil)
assert.Nil(t, err)
assert.Equal(t, "http://foo.com", p.URL().String())
}
func TestPageParserKeepQuery(t *testing.T) {
s := "http://foo.com?bar=baz"
p, err := newPageParser(newLinkFinder(nil)).Parse(s, nil)
assert.Nil(t, err)
assert.Equal(t, s, p.URL().String())
}
func TestPageParserResolveLinksWithBaseTag(t *testing.T) {
p, err := newPageParser(newLinkFinder(nil)).Parse(
"http://foo.com",
[]byte(`
<html>
<head>
<base href="foo/" />
</head>
<body>
<a href="bar" />
</body>
</html>
`),
)
assert.Nil(t, err)
assert.Equal(t, map[string]error{"http://foo.com/foo/bar": nil}, p.Links())
}
func TestPageParserResolveLinksWithBlankBaseTag(t *testing.T) {
p, err := newPageParser(newLinkFinder(nil)).Parse(
"http://foo.com",
[]byte(`
<html>
<head>
<base href="_blank" />
</head>
<body>
<a href="bar" />
</body>
</html>
`),
)
assert.Nil(t, err)
assert.Equal(t, map[string]error{"http://foo.com/bar": nil}, p.Links())
}
func TestPageParserFailToParseWithInvalidBaseTag(t *testing.T) {
_, err := newPageParser(newLinkFinder(nil)).Parse(
"http://foo.com",
[]byte(`
<html>
<head>
<base href=":" />
</head>
<body>
</body>
</html>
`),
)
assert.NotNil(t, err)
}
func TestPageParserParseID(t *testing.T) {
p, err := newPageParser(newLinkFinder(nil)).Parse(
"http://foo.com",
[]byte(`<p id="foo" />`),
)
assert.Nil(t, err)
assert.Equal(t, map[string]struct{}{"foo": {}}, p.Fragments())
}
func TestPageParserParseName(t *testing.T) {
p, err := newPageParser(newLinkFinder(nil)).Parse(
"http://foo.com",
[]byte(`<p name="foo" />`),
)
assert.Nil(t, err)
assert.Equal(t, map[string]struct{}{"foo": {}}, p.Fragments())
}
func TestPageParserParseIDAndName(t *testing.T) {
p, err := newPageParser(newLinkFinder(nil)).Parse(
"http://foo.com",
[]byte(`<p id="foo" name="bar" />`),
)
assert.Nil(t, err)
assert.Equal(t, map[string]struct{}{"foo": {}, "bar": {}}, p.Fragments())
}
func TestPageParserParseLinks(t *testing.T) {
for _, ss := range [][2]string{
{
`<a href="foo">bar</a>`,
"http://foo.com/foo",
},
{
`<img src="foo.img" />`,
"http://foo.com/foo.img",
},
} {
p, err := newPageParser(newLinkFinder(nil)).Parse(
"http://foo.com",
[]byte(ss[0]),
)
assert.Nil(t, err)
assert.Equal(t, 1, len(p.Links()))
_, ok := p.Links()[ss[1]]
assert.True(t, ok)
}
}