forked from goware/urlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlx_test.go
330 lines (289 loc) · 12.8 KB
/
urlx_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package urlx_test
import (
"fmt"
"testing"
"github.com/goware/urlx"
)
func TestParse(t *testing.T) {
tests := []struct {
in string
out string
err bool
}{
// Error out on missing host:
{in: "", err: true},
{in: "/", err: true},
{in: "//", err: true},
// Test schemes:
{in: "http://example.com", out: "http://example.com"},
{in: "HTTP://x.example.com", out: "http://x.example.com"},
{in: "http://localhost", out: "http://localhost"},
{in: "http://user.local", out: "http://user.local"},
{in: "https://example.com", out: "https://example.com"},
{in: "HTTPS://example.com", out: "https://example.com"},
{in: "ssh://example.com:22", out: "ssh://example.com:22"},
{in: "jabber://example.com:5222", out: "jabber://example.com:5222"},
// Leading double slashes (any scheme) defaults to http:
{in: "//example.com", out: "http://example.com"},
// Empty scheme defaults to http:
{in: "localhost", out: "http://localhost"},
{in: "LOCALHOST", out: "http://localhost"},
{in: "localhost:80", out: "http://localhost:80"},
{in: "localhost:8080", out: "http://localhost:8080"},
{in: "user.local", out: "http://user.local"},
{in: "user.local:80", out: "http://user.local:80"},
{in: "user.local:8080", out: "http://user.local:8080"},
{in: "127.0.0.1", out: "http://127.0.0.1"},
{in: "127.0.0.1:80", out: "http://127.0.0.1:80"},
{in: "127.0.0.1:8080", out: "http://127.0.0.1:8080"},
{in: "[2001:db8:a0b:12f0::1]", out: "http://[2001:db8:a0b:12f0::1]"},
{in: "[2001:db8:a0b:12f0::80]", out: "http://[2001:db8:a0b:12f0::80]"},
// Keep the port even on matching scheme:
{in: "http://localhost:80", out: "http://localhost:80"},
{in: "http://localhost:8080", out: "http://localhost:8080"},
{in: "http://x.example.io:8080", out: "http://x.example.io:8080"},
{in: "[2001:db8:a0b:12f0::80]:80", out: "http://[2001:db8:a0b:12f0::80]:80"},
{in: "[2001:db8:a0b:12f0::1]:8080", out: "http://[2001:db8:a0b:12f0::1]:8080"},
// Test domains, subdomains etc.:
{in: "example.com", out: "http://example.com"},
{in: "1.example.com", out: "http://1.example.com"},
{in: "1.example.io", out: "http://1.example.io"},
{in: "subsub.sub.example.com", out: "http://subsub.sub.example.com"},
// Test userinfo:
{in: "[email protected]", out: "http://[email protected]"},
{in: "user:[email protected]", out: "http://user:[email protected]"},
{in: "https://user:[email protected]", out: "https://user:[email protected]"},
// Lowercase scheme and host by default. Let net/url normalize URL by default:
{in: "hTTp://subSUB.sub.EXAMPLE.COM/x//////y///foo.mp3?c=z&a=x&b=y#t=20", out: "http://subsub.sub.example.com/x//////y///foo.mp3?c=z&a=x&b=y#t=20"},
// IDNA Punycode domains.
// TODO: net/url escapes all the fields in String() method. Should we fix it?
{in: "http://www.žluťoučký-kůň.cz/úpěl-ďábelské-ódy", out: "http://www.%C5%BElu%C5%A5ou%C4%8Dk%C3%BD-k%C5%AF%C5%88.cz/%C3%BAp%C4%9Bl-%C4%8F%C3%A1belsk%C3%A9-%C3%B3dy"},
{in: "http://www.xn--luouk-k-z2a6lsyxjlexh.cz/úpěl-ďábelské-ódy", out: "http://www.xn--luouk-k-z2a6lsyxjlexh.cz/%C3%BAp%C4%9Bl-%C4%8F%C3%A1belsk%C3%A9-%C3%B3dy"},
{in: "http://żółć.pl/żółć.html", out: "http://%C5%BC%C3%B3%C5%82%C4%87.pl/%C5%BC%C3%B3%C5%82%C4%87.html"},
{in: "http://xn--kda4b0koi.pl/żółć.html", out: "http://xn--kda4b0koi.pl/%C5%BC%C3%B3%C5%82%C4%87.html"},
// IANA TLDs.
// TODO: net/url escapes all the fields in String() method. Should we fix it?
{in: "https://pressly.餐厅", out: "https://pressly.%E9%A4%90%E5%8E%85"},
{in: "https://pressly.组织机构", out: "https://pressly.%E7%BB%84%E7%BB%87%E6%9C%BA%E6%9E%84"},
// Some obviously wrong data:
{in: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==", err: true},
{in: "javascript:evilFunction()", err: true},
{in: "otherscheme:garbage", err: true},
{in: "<funnnytag>", err: true},
}
for _, tt := range tests {
url, err := urlx.Parse(tt.in)
if err != nil {
if !tt.err {
t.Errorf(`"%s": unexpected error "%v"`, tt.in, err)
}
continue
}
if tt.err && err == nil {
t.Errorf(`"%s": expected error`, tt.in)
continue
}
if url.String() != tt.out {
t.Errorf(`"%s": got "%s", want "%v"`, tt.in, url, tt.out)
}
}
}
func TestParseWithDefaultScheme(t *testing.T) {
// Make sure that when you specify a default scheme of https, it applies
// to the result but only when the `in` does not have a scheme specified
tests := []struct {
in string
out string
err bool
}{
// Error out on missing host:
{in: "", err: true},
{in: "/", err: true},
{in: "//", err: true},
// Test schemes:
{in: "http://example.com", out: "http://example.com"},
{in: "HTTP://x.example.com", out: "http://x.example.com"},
{in: "http://localhost", out: "http://localhost"},
{in: "http://user.local", out: "http://user.local"},
{in: "https://example.com", out: "https://example.com"},
{in: "HTTPS://example.com", out: "https://example.com"},
{in: "ssh://example.com:22", out: "ssh://example.com:22"},
{in: "jabber://example.com:5222", out: "jabber://example.com:5222"},
// Leading double slashes (any scheme) defaults to http:
{in: "//example.com", out: "https://example.com"},
// Empty scheme defaults to https:
{in: "localhost", out: "https://localhost"},
{in: "LOCALHOST", out: "https://localhost"},
{in: "localhost:80", out: "https://localhost:80"},
{in: "localhost:8080", out: "https://localhost:8080"},
{in: "user.local", out: "https://user.local"},
{in: "user.local:80", out: "https://user.local:80"},
{in: "user.local:8080", out: "https://user.local:8080"},
{in: "127.0.0.1", out: "https://127.0.0.1"},
{in: "127.0.0.1:80", out: "https://127.0.0.1:80"},
{in: "127.0.0.1:8080", out: "https://127.0.0.1:8080"},
{in: "[2001:db8:a0b:12f0::1]", out: "https://[2001:db8:a0b:12f0::1]"},
{in: "[2001:db8:a0b:12f0::80]", out: "https://[2001:db8:a0b:12f0::80]"},
// Keep the port even on matching scheme:
{in: "http://localhost:80", out: "http://localhost:80"},
{in: "http://localhost:8080", out: "http://localhost:8080"},
{in: "http://x.example.io:8080", out: "http://x.example.io:8080"},
{in: "[2001:db8:a0b:12f0::80]:80", out: "https://[2001:db8:a0b:12f0::80]:80"},
{in: "[2001:db8:a0b:12f0::1]:8080", out: "https://[2001:db8:a0b:12f0::1]:8080"},
// Test domains, subdomains etc.:
{in: "example.com", out: "https://example.com"},
{in: "1.example.com", out: "https://1.example.com"},
{in: "1.example.io", out: "https://1.example.io"},
{in: "subsub.sub.example.com", out: "https://subsub.sub.example.com"},
// Test userinfo:
{in: "[email protected]", out: "https://[email protected]"},
{in: "user:[email protected]", out: "https://user:[email protected]"},
{in: "https://user:[email protected]", out: "https://user:[email protected]"},
// Lowercase scheme and host by default. Let net/url normalize URL by default:
{in: "hTTp://subSUB.sub.EXAMPLE.COM/x//////y///foo.mp3?c=z&a=x&b=y#t=20", out: "http://subsub.sub.example.com/x//////y///foo.mp3?c=z&a=x&b=y#t=20"},
// IDNA Punycode domains.
// TODO: net/url escapes all the fields in String() method. Should we fix it?
{in: "http://www.žluťoučký-kůň.cz/úpěl-ďábelské-ódy", out: "http://www.%C5%BElu%C5%A5ou%C4%8Dk%C3%BD-k%C5%AF%C5%88.cz/%C3%BAp%C4%9Bl-%C4%8F%C3%A1belsk%C3%A9-%C3%B3dy"},
{in: "http://www.xn--luouk-k-z2a6lsyxjlexh.cz/úpěl-ďábelské-ódy", out: "http://www.xn--luouk-k-z2a6lsyxjlexh.cz/%C3%BAp%C4%9Bl-%C4%8F%C3%A1belsk%C3%A9-%C3%B3dy"},
{in: "http://żółć.pl/żółć.html", out: "http://%C5%BC%C3%B3%C5%82%C4%87.pl/%C5%BC%C3%B3%C5%82%C4%87.html"},
{in: "http://xn--kda4b0koi.pl/żółć.html", out: "http://xn--kda4b0koi.pl/%C5%BC%C3%B3%C5%82%C4%87.html"},
// IANA TLDs.
// TODO: net/url escapes all the fields in String() method. Should we fix it?
{in: "https://pressly.餐厅", out: "https://pressly.%E9%A4%90%E5%8E%85"},
{in: "https://pressly.组织机构", out: "https://pressly.%E7%BB%84%E7%BB%87%E6%9C%BA%E6%9E%84"},
// Some obviously wrong data:
{in: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==", err: true},
{in: "javascript:evilFunction()", err: true},
{in: "otherscheme:garbage", err: true},
{in: "<funnnytag>", err: true},
}
for _, tt := range tests {
url, err := urlx.ParseWithDefaultScheme(tt.in, "https")
if err != nil {
if !tt.err {
t.Errorf(`"%s": unexpected error "%v"`, tt.in, err)
}
continue
}
if tt.err && err == nil {
t.Errorf(`"%s": expected error`, tt.in)
continue
}
if url.String() != tt.out {
t.Errorf(`"%s": got "%s", want "%v"`, tt.in, url, tt.out)
}
}
}
func TestURLNormalize(t *testing.T) {
tests := []struct {
in string
out string
err bool
}{
// Remove unnecessary host dots:
// Purell bug? They claim the following works..
//{in: "http://..example..com../index.html", out: "http://example.com/index.html"},
// Remove default port:
{in: "http://example.com:80/index.html", out: "http://example.com/index.html"},
{in: "localhost:80", out: "http://localhost"},
{in: "127.0.0.1:80", out: "http://127.0.0.1"},
{in: "[2001:db8:a0b:12f0::1]:80", out: "http://[2001:db8:a0b:12f0::1]"},
// Empty scheme defaults to http:
{in: "localhost", out: "http://localhost"},
{in: "LOCALHOST", out: "http://localhost"},
{in: "localhost:80", out: "http://localhost"},
{in: "localhost:8080", out: "http://localhost:8080"},
{in: "user.local", out: "http://user.local"},
{in: "user.local:80", out: "http://user.local"},
{in: "user.local:8080", out: "http://user.local:8080"},
{in: "127.0.0.1", out: "http://127.0.0.1"},
{in: "127.0.0.1:80", out: "http://127.0.0.1"},
{in: "127.0.0.1:8080", out: "http://127.0.0.1:8080"},
{in: "[2001:db8:a0b:12f0::1]", out: "http://[2001:db8:a0b:12f0::1]"},
{in: "[2001:db8:a0b:12f0::1]:80", out: "http://[2001:db8:a0b:12f0::1]"},
{in: "[2001:db8:a0b:12f0::1]:8080", out: "http://[2001:db8:a0b:12f0::1]:8080"},
{in: "[2001:db8:a0b:12f0::80]", out: "http://[2001:db8:a0b:12f0::80]"},
{in: "[2001:db8:a0b:12f0::80]:80", out: "http://[2001:db8:a0b:12f0::80]"},
{in: "[2001:db8:a0b:12f0::80]:8080", out: "http://[2001:db8:a0b:12f0::80]:8080"},
{in: "http://localhost:8080", out: "http://localhost:8080"},
{in: "http://x.example.io:8080", out: "http://x.example.io:8080"},
// Remove duplicate slashes.
{in: "http://example.com///x//////y///index.html", out: "http://example.com/x/y/index.html"},
// Remove unnecesary dots from path:
{in: "http://example.com/./x/y/z/../index.html", out: "http://example.com/x/y/index.html"},
// Sort query:
{in: "http://example.com/index.html?c=z&a=x&b=y", out: "http://example.com/index.html?a=x&b=y&c=z"},
// Leave fragment as is:
{in: "http://example.com/index.html#t=20", out: "http://example.com/index.html#t=20"},
// README example:
{in: "localhost:80///x///y/z/../././index.html?b=y&a=x#t=20", out: "http://localhost/x/y/index.html?a=x&b=y#t=20"},
// Decode Punycode into UTF8.
{in: "http://www.xn--luouk-k-z2a6lsyxjlexh.cz/úpěl-ďábelské-ódy", out: "http://www.žluťoučký-kůň.cz/%C3%BAp%C4%9Bl-%C4%8F%C3%A1belsk%C3%A9-%C3%B3dy"},
{in: "http://xn--kda4b0koi.pl/żółć.html", out: "http://żółć.pl/%C5%BC%C3%B3%C5%82%C4%87.html"},
// ..more robust test cases covered by Purell
}
for _, tt := range tests {
u, _ := urlx.Parse(tt.in)
url, err := urlx.Normalize(u)
if err != nil {
if !tt.err {
t.Errorf(`%v: unexpected error \"%v\"`, tt.in, err)
}
continue
}
if tt.err && err == nil {
t.Errorf(`%v: expected error`, tt.in)
continue
}
if url != tt.out {
t.Errorf(`%v: got "%v", want "%v"`, tt.in, url, tt.out)
}
}
}
func TestURLResolve(t *testing.T) {
tests := []struct {
in string
out string
err bool
}{
{in: "localhost", out: "127.0.0.1"},
{in: "google.com"},
{in: "some.weird.hostname.example.com", err: true},
}
for _, tt := range tests {
u, _ := urlx.Parse(tt.in)
ip, err := urlx.Resolve(u)
if !tt.err && err != nil {
t.Errorf(`%v: unexpected error \"%v\"`, tt.in, err)
continue
}
if tt.err && err == nil {
t.Errorf(`%v: expected error`, tt.in)
}
if tt.out != "" && tt.out != fmt.Sprint(ip) {
t.Errorf(`%v: got "%v", want "%v"`, tt.in, ip, tt.out)
}
}
}
func TestURIEncode(t *testing.T) {
tests := []struct {
in string
out string
err bool
}{
{in: "http://site.com/image/Look At Me.jpg", out: "http://site.com/image/Look%20At%20Me.jpg"},
}
for _, tt := range tests {
u, err := urlx.URIEncode(tt.in)
if !tt.err && err != nil {
t.Errorf(`%v: unexpected error \"%v\"`, tt.in, err)
continue
}
if tt.err && err == nil {
t.Errorf(`%v: expected error`, tt.in)
}
if tt.out != "" && tt.out != fmt.Sprint(u) {
t.Errorf(`%v: got "%v", want "%v"`, tt.in, u, tt.out)
}
}
}