-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler_test.go
223 lines (188 loc) · 6.24 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package wikicrawl
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
)
type expectedCounts struct {
linkCount int
brokenCount int
requestCount int
}
func validateCrawl(t *testing.T, expected expectedCounts, handler func(http.ResponseWriter, *http.Request)) {
requests := 0
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
requests++
handler(rw, req)
}))
defer server.Close()
result := NewCrawler(server.URL, "").Crawl(server.URL)
if len(result.Visited.Set) != expected.linkCount {
t.Errorf(`Visited links do not match expected.
Expected %d, found %d.`, expected.linkCount, len(result.Visited.Set))
}
if len(result.Broken.Set) != expected.brokenCount {
t.Errorf(`Broken links do not match expected.
Expected %d, found %d.`, expected.brokenCount, len(result.Broken.Set))
}
if requests != expected.requestCount {
t.Errorf(`Crawler should only request every internal link once.
Expected %d, found %d.`, expected.requestCount, requests)
}
}
func TestCrawl(t *testing.T) {
t.Run("Mocking web crawl tests", func(t *testing.T) {
t.Run("Validate single page with multiple links", func(t *testing.T) {
t.Parallel()
ex := expectedCounts{linkCount: 3, brokenCount: 0, requestCount: 3}
validateCrawl(t, ex, func(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, `<html><body><a href="/path1" /><a href="/path2" /></body></html>`)
})
})
t.Run("Avoid duplicate requests", func(t *testing.T) {
t.Parallel()
ex := expectedCounts{linkCount: 2, brokenCount: 0, requestCount: 2}
validateCrawl(t, ex, func(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, `<html><body><a href="/path" /><a href="/path" /></body></html>`)
})
})
t.Run("Record broken links", func(t *testing.T) {
t.Parallel()
ex := expectedCounts{linkCount: 3, brokenCount: 1, requestCount: 3}
validateCrawl(t, ex, func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/error" {
rw.WriteHeader(500)
return
}
fmt.Fprintf(rw, `<html><body><a href="/path" /><a href="/error" /></body></html>`)
})
})
})
}
func TestWikiPageTitle(t *testing.T) {
t.Run("Validate getting Wikimedia page title", func(t *testing.T) {
t.Run("Validate successful link", func(t *testing.T) {
t.Parallel()
title := "Page_Title"
link, _ := url.Parse("http://testing.com?title=" + title)
if WikiPageTitle(link) != "Page_Title" {
t.Errorf("Wikimedia page title mismatch - url: %s, title: %s", link, title)
}
})
})
}
func TestValidateLink(t *testing.T) {
t.Run("Validate Wikimedia links", func(t *testing.T) {
t.Run("Validate successful link", func(t *testing.T) {
t.Parallel()
link, _ := url.Parse("http://testing.com?title=Accept")
c := NewCrawler("http://testing.com", "")
if !c.ValidateLink(link) {
t.Errorf("Url incorrectly marked as invalid: %s.", link)
}
})
t.Run("Validate link with missing title", func(t *testing.T) {
t.Parallel()
link, _ := url.Parse("http://testing.com?notitle=1")
c := NewCrawler("http://testing.com", "")
if !c.ValidateLink(link) {
t.Errorf("Url incorrectly marked as invalid: %s.", link)
}
})
t.Run("Skip outside link", func(t *testing.T) {
t.Parallel()
link, _ := url.Parse("http://otherdomain.com?title=Accept")
c := NewCrawler("http://testing.com", "")
if c.ValidateLink(link) {
t.Errorf("Url incorrectly marked as valid: %s.", link)
}
})
t.Run("Skip forbidden pages", func(t *testing.T) {
t.Parallel()
link, _ := url.Parse("http://testing.com?title=Help:Skip")
c := NewCrawler("http://testing.com", "")
if c.ValidateLink(link) {
t.Errorf("Url incorrectly marked as valid: %s.", link)
}
})
})
}
func validateParseLinks(t *testing.T, html string, expected LinkSet) {
found := ParseLinks(strings.NewReader(html))
if !reflect.DeepEqual(found, expected) {
t.Errorf("Parsing links failed, got: %v, want: %v.", found, expected)
}
}
func TestParseLinks(t *testing.T) {
t.Run("Parse HTML links", func(t *testing.T) {
t.Run("Parsing single link", func(t *testing.T) {
t.Parallel()
html := `<html><body><a href="testing"></body></html>`
expected := NewLinkSet()
expected.Add("testing")
validateParseLinks(t, html, expected)
})
t.Run("Malformed HTML missing closing body tag", func(t *testing.T) {
t.Parallel()
html := `<html><body><a href="testing"></html>`
expected := NewLinkSet()
expected.Add("testing")
validateParseLinks(t, html, expected)
})
t.Run("Parsing self closing tag", func(t *testing.T) {
t.Parallel()
html := `<html><body><a href="testing" /></body></html>`
expected := NewLinkSet()
expected.Add("testing")
validateParseLinks(t, html, expected)
})
})
}
func TestNormalizeUrl(t *testing.T) {
t.Run("Normalize URL's", func(t *testing.T) {
t.Run("Relative Url", func(t *testing.T) {
t.Parallel()
base, _ := url.Parse("http://testing.com")
link, _ := url.Parse("/path")
found := NormalizeUrl(link, base).String()
expected := "http://testing.com/path"
if found != expected {
t.Errorf("Url malformed, got: %s, want: %s.", found, expected)
}
})
t.Run("Mismatched protocol", func(t *testing.T) {
t.Parallel()
base, _ := url.Parse("http://testing.com")
link, _ := url.Parse("https://testing.com/path")
found := NormalizeUrl(link, base).String()
expected := "http://testing.com/path"
if found != expected {
t.Errorf("Url malformed, got: %s, want: %s.", found, expected)
}
})
t.Run("Trim unwanted parameters", func(t *testing.T) {
t.Parallel()
base, _ := url.Parse("http://testing.com")
link, _ := url.Parse("http://testing.com/path?title=title&bad=2")
found := NormalizeUrl(link, base).String()
expected := "http://testing.com/path?title=title"
if found != expected {
t.Errorf("Url malformed, got: %s, want: %s.", found, expected)
}
})
t.Run("Lowercase host and scheme", func(t *testing.T) {
t.Parallel()
base, _ := url.Parse("HTTP://Testing.Com")
link, _ := url.Parse("HTTP://Testing.Com/path")
found := NormalizeUrl(link, base).String()
expected := "http://testing.com/path"
if found != expected {
t.Errorf("Url malformed, got: %s, want: %s.", found, expected)
}
})
})
}