-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobots_test.go
78 lines (74 loc) · 1.38 KB
/
robots_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
package robots_test
import (
"net/http"
"net/url"
"testing"
"github.com/0x51-dev/robots"
)
func TestFile_Allowed(t *testing.T) {
wc := robots.Wildcard
block := robots.ProductToken("Block")
f := robots.File{
Groups: []*robots.Group{
{
ProductTokens: robots.ProductTokens{
&wc,
},
Rules: []*robots.Rule{
{
Type: robots.Allow,
Path: &url.URL{Path: "/"},
},
},
},
{
ProductTokens: robots.ProductTokens{
&block,
},
Rules: []*robots.Rule{
{
Type: robots.Disallow,
Path: &url.URL{Path: "/"},
},
},
},
},
}
for _, test := range []struct {
pt string
path string
allowed bool
}{
{"ExampleBot", "/", true},
{"Block", "/", false},
} {
u, err := url.Parse(test.path)
if err != nil {
t.Fatal(err)
}
allowed := f.Allowed(robots.ProductToken(test.pt), u)
if allowed != test.allowed {
t.Errorf("expected %v, got %v", test.allowed, allowed)
}
}
}
func TestFile_Remotes(t *testing.T) {
for _, u := range []string{
"https://www.google.com/robots.txt",
"https://github.com/robots.txt",
"https://www.bing.com/robots.txt",
"https://x.com/robots.txt",
} {
resp, err := http.Get(u)
if err != nil {
t.Fatal(err)
}
f, err := robots.ParseRobotsFile(resp.Body)
if err != nil {
t.Fatal(err)
}
if f == nil {
t.Fatal("expected robots file, got nil")
}
}
}