-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler_test.go
189 lines (136 loc) · 4.6 KB
/
crawler_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package crawler
import (
"fmt"
net "net/url"
"testing"
"golang.org/x/net/html"
"robot/bro"
)
func TestValidateShouldSkipInvalidURLs(t *testing.T) {
root, _ := net.Parse("http://localhost:8080/")
if validate(root, nil) {
t.Errorf("validate should skip nil")
}
if url, _ := net.Parse("host:8080"); validate(root, url) {
t.Errorf("validate should skip different domain")
}
if url, _ := net.Parse("javascript:void(0)"); validate(root, url) {
t.Errorf("validate should skip unexpected scheme")
}
}
func TestNormalizeShouldSkipInvalidURLs(t *testing.T) {
root, _ := net.Parse("http://localhost:8080/")
if res := normalize(root, ""); res != nil {
t.Errorf("normalize should skip empty url")
}
if res := normalize(root, "/"); res.String() != "http://localhost:8080/" {
t.Errorf("normalize should handle root url")
}
if res := normalize(root, "#"); res != nil {
t.Errorf("normalize should skip url with empty fragment")
}
if res := normalize(root, "#fragment"); res != nil {
t.Errorf("normalize should skip url with any fragment")
}
}
func TestNormalizeShouldKeepOriginalHost(t *testing.T) {
root, _ := net.Parse("http://localhost:8080/")
if res := normalize(root, "http://host:8080/fx/"); res.String() != "http://host:8080/fx/" {
t.Errorf("normalize should keep original host")
}
}
func TestNormalizeShouldFixPath(t *testing.T) {
root, _ := net.Parse("http://localhost:8080/")
if res := normalize(root, "/fx/"); res.String() != "http://localhost:8080/fx/" {
t.Errorf("normalize should fix path: %s", res)
}
if res := normalize(root, "fx/"); res.String() != "http://localhost:8080/fx/" {
t.Errorf("normalize should fix path: %s", res)
}
if res := normalize(root, "fx"); res.String() != "http://localhost:8080/fx" {
t.Errorf("normalize should fix path: %s", res)
}
root2, _ := net.Parse("http://localhost:8080")
if res := normalize(root2, "fx"); res.String() != "http://localhost:8080/fx" {
t.Errorf("normalize should fix path: %s", res)
}
}
func TestNormalizeShouldDropFragment(t *testing.T) {
root, _ := net.Parse("http://localhost:8080/")
if res := normalize(root, "jsp#id"); res.String() != "http://localhost:8080/jsp" {
t.Errorf("normalize should drop any fragment")
}
}
func TestNormalizeShouldDropNotRelevantQueryParamsAndSortOthers(t *testing.T) {
root, _ := net.Parse("http://localhost:8080/")
should1 := "http://localhost:8080/?activeComponent=Reports&uuid=coreboo2k02bo0000kfd4plquvalkl6k"
if res := normalize(root, "?uuid=coreboo2k02bo0000kfd4plquvalkl6k&activeComponent=Reports"); res.String() != should1 {
t.Errorf("normalize should sort query params: %s", res)
}
if res := normalize(root, "http://localhost:8080/?uuid=coreboo2k02bo0000kfd4plquvalkl6k&activeComponent=Reports"); res.String() != should1 {
t.Errorf("normalize should sort query params: %s", res)
}
should2 := "http://localhost:8080/?activeComponent=Reports"
if res := normalize(root, "?activeComponent=Reports&dropme=yes"); res.String() != should2 {
t.Errorf("normalize should drop not relevant query params: %s", res)
}
if res := normalize(root, "http://localhost:8080/?activeComponent=Reports&dropme=yes"); res.String() != should2 {
t.Errorf("normalize should drop not relevant query params: %s", res)
}
}
func TestCrawlWithoutDeepLimit(t *testing.T) {
should := 5
visited := crawl("/", mockConfig(-1))
if visited != should {
t.Errorf("Test shoud visit %d urls but was %d", should, visited)
}
}
func TestCrawlWithZeroDeepLimit(t *testing.T) {
should := 1
visited := crawl("/", mockConfig(0))
if visited != should {
t.Errorf("Test shoud visit %d urls but was %d", should, visited)
}
}
func TestCrawlWithDeepLimit(t *testing.T) {
should := 3
visited := crawl("/", mockConfig(1))
if visited != should {
t.Errorf("Test shoud visit %d urls but was %d", should, visited)
}
}
func mock(maxDepth int) *Config {
return &Crawler{"http://golang.org", 2, maxDepth, &clientMock{}, &parserMock{}, nil}
}
type parserMock struct{}
type clientMock struct{}
func (self *parserMock) Parse(status int, url string, doc *html.Node) ([]string, []string) {
return data[url], nil
}
func (self *clientMock) RequestUrl(url string, callback bro.DocumentCallbackFunc) (int, error) {
if _, ok := data[url]; ok {
callback(200, url, nil)
return 200, nil
}
return -1, fmt.Errorf("not found: %s", url)
}
var data = map[string][]string{
"http://golang.org/": []string{
"/pkg/",
"/cmd/",
},
"http://golang.org/pkg/": []string{
"/",
"/cmd/",
"/pkg/fmt/",
"/pkg/os/",
},
"http://golang.org/pkg/fmt/": []string{
"/",
"/pkg/",
},
"http://golang.org/pkg/os/": []string{
"/",
"/pkg/",
},
}