forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_finder_test.go
146 lines (120 loc) · 2.94 KB
/
link_finder_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
package main
import (
"fmt"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/net/html"
)
func TestLinkFinderFindLinks(t *testing.T) {
b, err := url.Parse("https://localhost")
assert.Nil(t, err)
for _, c := range []struct {
html string
linkCount int
}{
{``, 0},
{`<a href="/" />`, 1},
{`<a href="/foo" />`, 1},
// TODO: Test <frame> tag.
{`<iframe src="/iframe"></iframe>`, 1},
{`<img src="/foo.jpg" />`, 1},
{`<link href="/link" />`, 1},
{`<script src="/foo.js"></script>`, 1},
{`<source src="/foo.png" />`, 1},
{`<source srcset="/foo.png" />`, 1},
{`<source src="/foo.png" srcset="/bar.png" />`, 2},
{`<track src="/foo.vtt" />`, 1},
{`<a href="/"><img src="/foo.png" /></a>`, 2},
{`<a href="/" /><a href="/" />`, 1},
} {
n, err := html.Parse(strings.NewReader(htmlWithBody(c.html)))
assert.Nil(t, err)
s, e := 0, 0
for _, err := range newLinkFinder(nil).Find(n, b) {
if err == nil {
s++
} else {
e++
}
}
assert.Equal(t, c.linkCount, s)
assert.Equal(t, 0, e)
}
}
func TestLinkFinderFindLinkWithoutEncodedSpaces(t *testing.T) {
b, err := url.Parse("http://foo.com")
assert.Nil(t, err)
n, err := html.Parse(strings.NewReader(
htmlWithBody(`<a href="http://foo.com/a%20b" />`)),
)
assert.Nil(t, err)
ls := newLinkFinder(nil).Find(n, b)
err, ok := ls["http://foo.com/a%20b"]
assert.True(t, ok)
assert.Nil(t, err)
}
func TestLinkFinderFindLinkWithoutSpacesNotEncoded(t *testing.T) {
b, err := url.Parse("http://foo.com")
assert.Nil(t, err)
n, err := html.Parse(strings.NewReader(
htmlWithBody(`<a href="http://foo.com/a b" />`)),
)
assert.Nil(t, err)
ls := newLinkFinder(nil).Find(n, b)
err, ok := ls["http://foo.com/a%20b"]
assert.True(t, ok)
assert.Nil(t, err)
}
func TestLinkFinderFindLinkWithLeadingAndTrailingSpaces(t *testing.T) {
b, err := url.Parse("http://foo.com")
assert.Nil(t, err)
n, err := html.Parse(strings.NewReader(
htmlWithBody(`<a href=" http://foo.com " />`)),
)
assert.Nil(t, err)
ls := newLinkFinder(nil).Find(n, b)
err, ok := ls["http://foo.com"]
assert.True(t, ok)
assert.Nil(t, err)
}
func TestLinkFinderFailWithInvalidURL(t *testing.T) {
b, err := url.Parse("http://foo.com")
assert.Nil(t, err)
n, err := html.Parse(strings.NewReader(htmlWithBody(`<a href=":" />`)))
assert.Nil(t, err)
ls := newLinkFinder(nil).Find(n, b)
assert.Equal(t, 1, len(ls))
assert.NotNil(t, ls[":"])
}
func TestLinkFinderIsLinkExcluded(t *testing.T) {
for _, x := range []struct {
regexps []string
answer bool
}{
{
[]string{"foo\\.com"},
true,
},
{
[]string{"foo"},
true,
},
{
[]string{"bar", "foo"},
true,
},
{
[]string{"bar"},
false,
},
} {
rs, err := compileRegexps(x.regexps)
assert.Nil(t, err)
assert.Equal(t, x.answer, newLinkFinder(rs).isLinkExcluded("http://foo.com"))
}
}
func htmlWithBody(b string) string {
return fmt.Sprintf(`<html><body>%v</body></html>`, b)
}