forked from richyen/walker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurl_test.go
More file actions
155 lines (147 loc) · 3.76 KB
/
url_test.go
File metadata and controls
155 lines (147 loc) · 3.76 KB
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
package walker
import (
"testing"
"time"
)
func TestURLParsing(t *testing.T) {
orig := Config.Fetcher.PurgeSidList
defer func() {
Config.Fetcher.PurgeSidList = orig
PostConfigHooks()
}()
Config.Fetcher.PurgeSidList = []string{"jsessionid", "phpsessid"}
PostConfigHooks()
tests := []struct {
tag string
input string
expect string
}{
{
tag: "UpCase",
input: "HTTP://A.com/page1.com",
expect: "http://a.com/page1.com",
},
{
tag: "Fragment",
input: "http://a.com/page1.com#Fragment",
expect: "http://a.com/page1.com",
},
{
tag: "PathSID",
input: "http://a.com/page1.com;jsEssIoniD=436100313FAFBBB9B4DC8BA3C2EC267B",
expect: "http://a.com/page1.com",
},
{
tag: "PathSID2",
input: "http://a.com/page1.com;phPseSsId=436100313FAFBBB9B4DC8BA3C2EC267B",
expect: "http://a.com/page1.com",
},
{
tag: "QuerySID",
input: "http://a.com/page1.com?foo=bar&jsessionID=436100313FAFBBB9B4DC8BA3C2EC267B&baz=niffler",
expect: "http://a.com/page1.com?baz=niffler&foo=bar",
},
{
tag: "QuerySID2",
input: "http://a.com/page1.com?PHPSESSID=436100313FAFBBB9B4DC8BA3C2EC267B",
expect: "http://a.com/page1.com",
},
{
tag: "EmbeddedPort",
input: "http://a.com:8080/page1.com",
expect: "http://a.com:8080/page1.com",
},
}
for _, tst := range tests {
u, err := ParseAndNormalizeURL(tst.input)
if err != nil {
t.Fatalf("For tag %q ParseURL failed %v", tst.tag, err)
}
got := u.String()
if got != tst.expect {
t.Errorf("For tag %q link mismatch got %q, expected %q", tst.tag, got, tst.expect)
}
}
}
func TestURLEqual(t *testing.T) {
tests := []struct {
tag string
expect bool
link1 *URL
link2 *URL
}{
{"basic equal", true,
MustParse("http://www.test.com/"), MustParse("http://www.test.com/")},
{"basic not equal", false,
MustParse("http://www.test.com/stuff"), MustParse("http://www.test.com/")},
{"query param equal", true,
MustParse("http://www.test.com/?a=b"), MustParse("http://www.test.com/?a=b")},
{"query param not equal", false,
MustParse("http://www.test.com/?a=1"), MustParse("http://www.test.com/?a=2")},
{"protocol not equal", false,
MustParse("http://www.test.com"), MustParse("https://www.test.com")},
{"time equal", true,
&URL{
URL: MustParse("http://www.test.com").URL,
LastCrawled: NotYetCrawled,
},
&URL{
URL: MustParse("http://www.test.com").URL,
LastCrawled: NotYetCrawled,
},
},
{"time not equal", false,
&URL{
URL: MustParse("http://www.test.com").URL,
LastCrawled: NotYetCrawled,
},
&URL{
URL: MustParse("http://www.test.com").URL,
LastCrawled: time.Now(),
},
},
}
for _, tst := range tests {
result := tst.link1.Equal(tst.link2)
if result != tst.expect {
t.Errorf("Tag: %v\nExpected Equal() to be %v but was %v for %v and %v",
tst.tag, tst.expect, result, tst.link1, tst.link2)
}
}
}
func TestURLEqualIgnoreLastCrawled(t *testing.T) {
tests := []struct {
tag string
expect bool
link1 *URL
link2 *URL
}{
{"time equal", true,
&URL{
URL: MustParse("http://www.test.com").URL,
LastCrawled: NotYetCrawled,
},
&URL{
URL: MustParse("http://www.test.com").URL,
LastCrawled: NotYetCrawled,
},
},
{"time not equal", true,
&URL{
URL: MustParse("http://www.test.com").URL,
LastCrawled: NotYetCrawled,
},
&URL{
URL: MustParse("http://www.test.com").URL,
LastCrawled: time.Now(),
},
},
}
for _, tst := range tests {
result := tst.link1.EqualIgnoreLastCrawled(tst.link2)
if result != tst.expect {
t.Errorf("Tag: %v\nExpected Equal() to be %v but was %v for %v and %v",
tst.tag, tst.expect, result, tst.link1, tst.link2)
}
}
}