-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
link_validator_test.go
65 lines (52 loc) · 1.32 KB
/
link_validator_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
package main
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/temoto/robotstxt"
)
func TestLinkValidatorReturnTrueForSameHostname(t *testing.T) {
i := newLinkValidator("foo.com", nil, nil)
for _, s := range []string{
"http://foo.com",
"http://foo.com/bar",
"https://foo.com",
} {
u, err := url.Parse(s)
assert.Nil(t, err)
assert.True(t, i.Validate(u))
}
}
func TestLinkValidatorReturnFalseForDifferentHostname(t *testing.T) {
i := newLinkValidator("foo.com", nil, nil)
u, err := url.Parse("http://bar.com")
assert.Nil(t, err)
assert.False(t, i.Validate(u))
}
func TestLinkValidatorValidateWithSitemap(t *testing.T) {
i := newLinkValidator(
"foo.com",
nil,
map[string]struct{}{"http://foo.com/foo": {}},
)
u, err := url.Parse("http://foo.com/foo")
assert.Nil(t, err)
assert.True(t, i.Validate(u))
u, err = url.Parse("http://foo.com/bar")
assert.Nil(t, err)
assert.False(t, i.Validate(u))
}
func TestLinkValidatorValidateWithRobotsTxt(t *testing.T) {
r, err := robotstxt.FromString(`
User-Agent: *
Disallow: /bar
`)
assert.Nil(t, err)
i := newLinkValidator("foo.com", r, nil)
u, err := url.Parse("http://foo.com/foo")
assert.Nil(t, err)
assert.True(t, i.Validate(u))
u, err = url.Parse("http://foo.com/bar")
assert.Nil(t, err)
assert.False(t, i.Validate(u))
}