forked from sindresorhus/normalize-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
418 lines (364 loc) · 23.6 KB
/
test.js
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
const test = require('ava');
const normalizeUrl = require('./index.js');
test('main', t => {
t.is(normalizeUrl('sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('sindresorhus.com '), 'http://sindresorhus.com');
t.is(normalizeUrl('sindresorhus.com.'), 'http://sindresorhus.com');
t.is(normalizeUrl('SindreSorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('HTTP://sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('//sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com:80'), 'http://sindresorhus.com');
t.is(normalizeUrl('https://sindresorhus.com:443'), 'https://sindresorhus.com');
t.is(normalizeUrl('http://www.sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('www.com'), 'http://www.com');
t.is(normalizeUrl('http://www.www.sindresorhus.com'), 'http://www.www.sindresorhus.com');
t.is(normalizeUrl('www.sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/foo/'), 'http://sindresorhus.com/foo');
t.is(normalizeUrl('sindresorhus.com/?foo=bar baz'), 'http://sindresorhus.com/?foo=bar+baz');
t.is(normalizeUrl('https://foo.com/https://bar.com'), 'https://foo.com/https://bar.com');
t.is(normalizeUrl('https://foo.com/https://bar.com/foo//bar'), 'https://foo.com/https://bar.com/foo/bar');
t.is(normalizeUrl('https://foo.com/http://bar.com'), 'https://foo.com/http://bar.com');
t.is(normalizeUrl('https://foo.com/http://bar.com/foo//bar'), 'https://foo.com/http://bar.com/foo/bar');
t.is(normalizeUrl('http://sindresorhus.com/%7Efoo/'), 'http://sindresorhus.com/~foo', 'decode URI octets');
t.is(normalizeUrl('https://foo.com/%FAIL%/07/94/ca/55.jpg'), 'https://foo.com/%FAIL%/07/94/ca/55.jpg');
t.is(normalizeUrl('http://sindresorhus.com/?'), 'http://sindresorhus.com');
t.is(normalizeUrl('êxample.com'), 'http://xn--xample-hva.com');
t.is(normalizeUrl('http://sindresorhus.com/?b=bar&a=foo'), 'http://sindresorhus.com/?a=foo&b=bar');
t.is(normalizeUrl('http://sindresorhus.com/?foo=bar*|<>:"'), 'http://sindresorhus.com/?foo=bar*|%3C%3E:%22');
t.is(normalizeUrl('http://sindresorhus.com:5000'), 'http://sindresorhus.com:5000');
t.is(normalizeUrl('//sindresorhus.com/', {normalizeProtocol: false}), '//sindresorhus.com');
t.is(normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false}), '//sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/foo#bar'), 'http://sindresorhus.com/foo#bar');
t.is(normalizeUrl('http://sindresorhus.com/foo#bar', {stripHash: true}), 'http://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com/foo#bar:~:text=hello%20world', {stripHash: true}), 'http://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com/foo/bar/../baz'), 'http://sindresorhus.com/foo/baz');
t.is(normalizeUrl('http://sindresorhus.com/foo/bar/./baz'), 'http://sindresorhus.com/foo/bar/baz');
// t.is(normalizeUrl('sindre://www.sorhus.com'), 'sindre://sorhus.com');
// t.is(normalizeUrl('sindre://www.sorhus.com/'), 'sindre://sorhus.com');
// t.is(normalizeUrl('sindre://www.sorhus.com/foo/bar'), 'sindre://sorhus.com/foo/bar');
t.is(normalizeUrl('https://i.vimeocdn.com/filter/overlay?src0=https://i.vimeocdn.com/video/598160082_1280x720.jpg&src1=https://f.vimeocdn.com/images_v6/share/play_icon_overlay.png'), 'https://i.vimeocdn.com/filter/overlay?src0=https://i.vimeocdn.com/video/598160082_1280x720.jpg&src1=https://f.vimeocdn.com/images_v6/share/play_icon_overlay.png');
});
test('defaultProtocol option', t => {
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https'}), 'https://sindresorhus.com');
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'http'}), 'http://sindresorhus.com');
// Legacy
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https:'}), 'https://sindresorhus.com');
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'http:'}), 'http://sindresorhus.com');
});
test('stripAuthentication option', t => {
t.is(normalizeUrl('http://user:[email protected]'), 'http://sindresorhus.com');
t.is(normalizeUrl('https://user:[email protected]'), 'https://sindresorhus.com');
t.is(normalizeUrl('https://user:[email protected]/@user'), 'https://sindresorhus.com/@user');
t.is(normalizeUrl('http://user:password@www.êxample.com'), 'http://xn--xample-hva.com');
// t.is(normalizeUrl('sindre://user:[email protected]'), 'sindre://sorhus.com');
const options = {stripAuthentication: false};
t.is(normalizeUrl('http://user:[email protected]', options), 'http://user:[email protected]');
t.is(normalizeUrl('https://user:[email protected]', options), 'https://user:[email protected]');
t.is(normalizeUrl('https://user:[email protected]/@user', options), 'https://user:[email protected]/@user');
t.is(normalizeUrl('http://user:password@www.êxample.com', options), 'http://user:[email protected]');
// t.is(normalizeUrl('sindre://user:[email protected]', options), 'sindre://user:[email protected]');
});
test('stripProtocol option', t => {
const options = {stripProtocol: true};
t.is(normalizeUrl('http://www.sindresorhus.com', options), 'sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com', options), 'sindresorhus.com');
t.is(normalizeUrl('https://www.sindresorhus.com', options), 'sindresorhus.com');
t.is(normalizeUrl('//www.sindresorhus.com', options), 'sindresorhus.com');
});
test('stripTextFragment option', t => {
t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/about#'), 'http://sindresorhus.com/about');
t.is(normalizeUrl('http://sindresorhus.com/about#:~:text=hello'), 'http://sindresorhus.com/about');
t.is(normalizeUrl('http://sindresorhus.com/about#main'), 'http://sindresorhus.com/about#main');
t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello'), 'http://sindresorhus.com/about#main');
t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello%20world'), 'http://sindresorhus.com/about#main');
const options = {stripTextFragment: false};
t.is(normalizeUrl('http://sindresorhus.com', options), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/about#:~:text=hello', options), 'http://sindresorhus.com/about#:~:text=hello');
t.is(normalizeUrl('http://sindresorhus.com/about#main', options), 'http://sindresorhus.com/about#main');
t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello', options), 'http://sindresorhus.com/about#main:~:text=hello');
t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello%20world', options), 'http://sindresorhus.com/about#main:~:text=hello%20world');
const options2 = {stripHash: true, stripTextFragment: false};
t.is(normalizeUrl('http://sindresorhus.com', options2), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/about#:~:text=hello', options2), 'http://sindresorhus.com/about');
t.is(normalizeUrl('http://sindresorhus.com/about#main', options2), 'http://sindresorhus.com/about');
t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello', options2), 'http://sindresorhus.com/about');
t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello%20world', options2), 'http://sindresorhus.com/about');
});
test('stripWWW option', t => {
const options = {stripWWW: false};
t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');
t.is(normalizeUrl('www.sindresorhus.com', options), 'http://www.sindresorhus.com');
t.is(normalizeUrl('http://www.êxample.com', options), 'http://www.xn--xample-hva.com');
// t.is(normalizeUrl('sindre://www.sorhus.com', options), 'sindre://www.sorhus.com');
const options2 = {stripWWW: true};
t.is(normalizeUrl('http://www.vue.amsterdam', options2), 'http://vue.amsterdam');
t.is(normalizeUrl('http://www.sorhus.xx--bck1b9a5dre4c', options2), 'http://sorhus.xx--bck1b9a5dre4c');
const tooLongTLDURL = 'http://www.sorhus.' + ''.padEnd(64, 'a');
t.is(normalizeUrl(tooLongTLDURL, options2), tooLongTLDURL);
});
test('removeQueryParameters option', t => {
const options = {
stripWWW: false,
removeQueryParameters: [/^utm_\w+/i, 'ref'],
};
t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test'), 'http://sindresorhus.com/?foo=bar');
t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');
t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com/?foo=bar');
t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com/?foo=bar');
});
test('removeQueryParameters boolean `true` option', t => {
const options = {
stripWWW: false,
removeQueryParameters: true,
};
t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');
t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com');
t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com');
});
test('removeQueryParameters boolean `false` option', t => {
const options = {
stripWWW: false,
removeQueryParameters: false,
};
t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');
t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com/?foo=bar');
t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test');
});
test('keepQueryParameters option', t => {
const options = {
stripWWW: false,
removeQueryParameters: false,
keepQueryParameters: [/^utm_\w+/i, 'ref'],
};
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');
t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com');
t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com/?ref=test_ref&utm_medium=test');
});
test('forceHttp option', t => {
const options = {forceHttp: true};
t.is(normalizeUrl('https://sindresorhus.com'), 'https://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com', options), 'http://sindresorhus.com');
t.is(normalizeUrl('https://www.sindresorhus.com', options), 'http://sindresorhus.com');
t.is(normalizeUrl('//sindresorhus.com', options), 'http://sindresorhus.com');
});
test('forceHttp option with forceHttps', t => {
t.throws(() => {
normalizeUrl('https://www.sindresorhus.com', {forceHttp: true, forceHttps: true});
}, {
message: 'The `forceHttp` and `forceHttps` options cannot be used together',
});
});
test('forceHttps option', t => {
const options = {forceHttps: true};
t.is(normalizeUrl('https://sindresorhus.com'), 'https://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com', options), 'https://sindresorhus.com');
t.is(normalizeUrl('https://www.sindresorhus.com', options), 'https://sindresorhus.com');
t.is(normalizeUrl('//sindresorhus.com', options), 'https://sindresorhus.com');
});
test('removeTrailingSlash option', t => {
const options = {removeTrailingSlash: false};
t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/'), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com', options), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/', options), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/redirect'), 'http://sindresorhus.com/redirect');
t.is(normalizeUrl('http://sindresorhus.com/redirect/'), 'http://sindresorhus.com/redirect');
t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect/');
t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect/');
t.is(normalizeUrl('http://sindresorhus.com/#/'), 'http://sindresorhus.com/#/');
t.is(normalizeUrl('http://sindresorhus.com/#/', options), 'http://sindresorhus.com/#/');
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true'), 'http://sindresorhus.com/?unicorns=true');
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true', options), 'http://sindresorhus.com/?unicorns=true');
});
test('removeExplicitPort option', t => {
const options = {removeExplicitPort: true};
t.is(normalizeUrl('http://sindresorhus.com:123', options), 'http://sindresorhus.com');
t.is(normalizeUrl('https://sindresorhus.com:123', options), 'https://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com:443', options), 'http://sindresorhus.com');
t.is(normalizeUrl('https://sindresorhus.com:80', options), 'https://sindresorhus.com');
});
test('removeSingleSlash option', t => {
const options = {removeSingleSlash: false};
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');
t.is(normalizeUrl('https://sindresorhus.com/', options), 'https://sindresorhus.com/');
t.is(normalizeUrl('https://sindresorhus.com/redirect', options), 'https://sindresorhus.com/redirect');
t.is(normalizeUrl('https://sindresorhus.com/redirect/', options), 'https://sindresorhus.com/redirect');
t.is(normalizeUrl('https://sindresorhus.com/#/', options), 'https://sindresorhus.com/#/');
t.is(normalizeUrl('https://sindresorhus.com/?unicorns=true', options), 'https://sindresorhus.com/?unicorns=true');
});
test('removeSingleSlash option combined with removeTrailingSlash option', t => {
const options = {removeTrailingSlash: false, removeSingleSlash: false};
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');
t.is(normalizeUrl('https://sindresorhus.com/', options), 'https://sindresorhus.com/');
t.is(normalizeUrl('https://sindresorhus.com/redirect', options), 'https://sindresorhus.com/redirect');
t.is(normalizeUrl('https://sindresorhus.com/redirect/', options), 'https://sindresorhus.com/redirect/');
t.is(normalizeUrl('https://sindresorhus.com/#/', options), 'https://sindresorhus.com/#/');
t.is(normalizeUrl('https://sindresorhus.com/?unicorns=true', options), 'https://sindresorhus.com/?unicorns=true');
});
test('removeDirectoryIndex option', t => {
const options1 = {removeDirectoryIndex: ['index.html', 'index.php']};
t.is(normalizeUrl('http://sindresorhus.com/index.html'), 'http://sindresorhus.com/index.html');
t.is(normalizeUrl('http://sindresorhus.com/index.html', options1), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/index.htm', options1), 'http://sindresorhus.com/index.htm');
t.is(normalizeUrl('http://sindresorhus.com/index.php', options1), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/path/index.html'), 'http://sindresorhus.com/path/index.html');
t.is(normalizeUrl('http://sindresorhus.com/path/index.html', options1), 'http://sindresorhus.com/path');
t.is(normalizeUrl('http://sindresorhus.com/path/index.htm', options1), 'http://sindresorhus.com/path/index.htm');
t.is(normalizeUrl('http://sindresorhus.com/path/index.php', options1), 'http://sindresorhus.com/path');
t.is(normalizeUrl('http://sindresorhus.com/foo/bar/index.html', options1), 'http://sindresorhus.com/foo/bar');
const options2 = {removeDirectoryIndex: [/^index\.[a-z]+$/, 'remove.html']};
t.is(normalizeUrl('http://sindresorhus.com/index.html'), 'http://sindresorhus.com/index.html');
t.is(normalizeUrl('http://sindresorhus.com/index.html', options2), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/index/index.html', options2), 'http://sindresorhus.com/index');
t.is(normalizeUrl('http://sindresorhus.com/remove.html', options2), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/default.htm', options2), 'http://sindresorhus.com/default.htm');
t.is(normalizeUrl('http://sindresorhus.com/index.php', options2), 'http://sindresorhus.com');
const options3 = {removeDirectoryIndex: true};
t.is(normalizeUrl('http://sindresorhus.com/index.html'), 'http://sindresorhus.com/index.html');
t.is(normalizeUrl('http://sindresorhus.com/index.html', options3), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/index.htm', options3), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/index.php', options3), 'http://sindresorhus.com');
});
test('removeTrailingSlash and removeDirectoryIndex options)', t => {
const options1 = {
removeTrailingSlash: true,
removeDirectoryIndex: true,
};
t.is(normalizeUrl('http://sindresorhus.com/path/', options1), 'http://sindresorhus.com/path');
t.is(normalizeUrl('http://sindresorhus.com/path/index.html', options1), 'http://sindresorhus.com/path');
t.is(normalizeUrl('http://sindresorhus.com/#/path/', options1), 'http://sindresorhus.com/#/path/');
t.is(normalizeUrl('http://sindresorhus.com/foo/#/bar/', options1), 'http://sindresorhus.com/foo#/bar/');
const options2 = {
removeTrailingSlash: false,
removeDirectoryIndex: true,
};
t.is(normalizeUrl('http://sindresorhus.com/path/', options2), 'http://sindresorhus.com/path/');
t.is(normalizeUrl('http://sindresorhus.com/path/index.html', options2), 'http://sindresorhus.com/path/');
t.is(normalizeUrl('http://sindresorhus.com/#/path/', options2), 'http://sindresorhus.com/#/path/');
});
test('sortQueryParameters option', t => {
const options1 = {
sortQueryParameters: true,
};
t.is(normalizeUrl('http://sindresorhus.com/?a=Z&b=Y&c=X&d=W', options1), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');
t.is(normalizeUrl('http://sindresorhus.com/?b=Y&c=X&a=Z&d=W', options1), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');
t.is(normalizeUrl('http://sindresorhus.com/?a=Z&d=W&b=Y&c=X', options1), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');
t.is(normalizeUrl('http://sindresorhus.com/', options1), 'http://sindresorhus.com');
const options2 = {
sortQueryParameters: false,
};
t.is(normalizeUrl('http://sindresorhus.com/?a=Z&b=Y&c=X&d=W', options2), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');
t.is(normalizeUrl('http://sindresorhus.com/?b=Y&c=X&a=Z&d=W', options2), 'http://sindresorhus.com/?b=Y&c=X&a=Z&d=W');
t.is(normalizeUrl('http://sindresorhus.com/?a=Z&d=W&b=Y&c=X', options2), 'http://sindresorhus.com/?a=Z&d=W&b=Y&c=X');
t.is(normalizeUrl('http://sindresorhus.com/', options2), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/?a=/path', options1), normalizeUrl('http://sindresorhus.com/?a=/path', options2));
});
test('invalid urls', t => {
t.throws(() => {
normalizeUrl('http://');
}, {
message: /^Invalid URL/,
});
t.throws(() => {
normalizeUrl('/');
}, {
message: /^Invalid URL/,
});
t.throws(() => {
normalizeUrl('/relative/path/');
}, {
message: /^Invalid URL/,
});
});
test('remove duplicate pathname slashes', t => {
t.is(normalizeUrl('http://sindresorhus.com////foo/bar'), 'http://sindresorhus.com/foo/bar');
t.is(normalizeUrl('http://sindresorhus.com////foo////bar'), 'http://sindresorhus.com/foo/bar');
t.is(normalizeUrl('//sindresorhus.com//foo', {normalizeProtocol: false}), '//sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com:5000///foo'), 'http://sindresorhus.com:5000/foo');
t.is(normalizeUrl('http://sindresorhus.com///foo'), 'http://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com:5000//foo'), 'http://sindresorhus.com:5000/foo');
t.is(normalizeUrl('http://sindresorhus.com//foo'), 'http://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com/s3://sindresorhus.com'), 'http://sindresorhus.com/s3://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/s3://sindresorhus.com//foo'), 'http://sindresorhus.com/s3://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com//foo/s3://sindresorhus.com'), 'http://sindresorhus.com/foo/s3://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/git://sindresorhus.com'), 'http://sindresorhus.com/git://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/git://sindresorhus.com//foo'), 'http://sindresorhus.com/git://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com//foo/git://sindresorhus.com//foo'), 'http://sindresorhus.com/foo/git://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com/a://sindresorhus.com//foo'), 'http://sindresorhus.com/a:/sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com/alongprotocolwithin50charlimitxxxxxxxxxxxxxxxxxxxx://sindresorhus.com//foo'), 'http://sindresorhus.com/alongprotocolwithin50charlimitxxxxxxxxxxxxxxxxxxxx://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com/alongprotocolexceeds50charlimitxxxxxxxxxxxxxxxxxxxxx://sindresorhus.com//foo'), 'http://sindresorhus.com/alongprotocolexceeds50charlimitxxxxxxxxxxxxxxxxxxxxx:/sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com/a2-.+://sindresorhus.com'), 'http://sindresorhus.com/a2-.+://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/a2-.+_://sindresorhus.com'), 'http://sindresorhus.com/a2-.+_:/sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/2abc://sindresorhus.com'), 'http://sindresorhus.com/2abc:/sindresorhus.com');
});
test('data URL', t => {
// Invalid URL.
t.throws(() => {
normalizeUrl('data:');
}, {
message: 'Invalid URL: data:',
});
// Strip default MIME type
t.is(normalizeUrl('data:text/plain,foo'), 'data:,foo');
// Strip default charset
t.is(normalizeUrl('data:;charset=us-ascii,foo'), 'data:,foo');
// Normalize away trailing semicolon.
t.is(normalizeUrl('data:;charset=UTF-8;,foo'), 'data:;charset=utf-8,foo');
// Empty MIME type.
t.is(normalizeUrl('data:,'), 'data:,');
// Empty MIME type with charset.
t.is(normalizeUrl('data:;charset=utf-8,foo'), 'data:;charset=utf-8,foo');
// Lowercase the MIME type.
t.is(normalizeUrl('data:TEXT/HTML,foo'), 'data:text/html,foo');
// Strip empty hash.
t.is(normalizeUrl('data:,foo# '), 'data:,foo');
// Key only mediaType attribute.
t.is(normalizeUrl('data:;foo=;bar,'), 'data:;foo;bar,');
// Lowercase the charset.
t.is(normalizeUrl('data:;charset=UTF-8,foo'), 'data:;charset=utf-8,foo');
// Remove spaces after the comma when it's base64.
t.is(normalizeUrl('data:;base64, Zm9v #foo #bar'), 'data:;base64,Zm9v#foo #bar');
// Keep spaces when it's not base64.
t.is(normalizeUrl('data:, foo #bar'), 'data:, foo #bar');
// Options.
const options = {
defaultProtocol: 'http',
normalizeProtocol: true,
forceHttp: true,
stripHash: true,
stripWWW: true,
stripProtocol: true,
removeQueryParameters: [/^utm_\w+/i, 'ref'],
sortQueryParameters: true,
removeTrailingSlash: true,
removeDirectoryIndex: true,
};
t.is(normalizeUrl('data:,sindresorhus.com/', options), 'data:,sindresorhus.com/');
t.is(normalizeUrl('data:,sindresorhus.com/index.html', options), 'data:,sindresorhus.com/index.html');
t.is(normalizeUrl('data:,sindresorhus.com?foo=bar&a=a&utm_medium=test', options), 'data:,sindresorhus.com?foo=bar&a=a&utm_medium=test');
t.is(normalizeUrl('data:,foo#bar', options), 'data:,foo');
t.is(normalizeUrl('data:,www.sindresorhus.com', options), 'data:,www.sindresorhus.com');
});
test('prevents homograph attack', t => {
// The input string uses Unicode to make it look like a valid `ebay.com` URL.
t.is(normalizeUrl('https://ebаy.com'), 'https://xn--eby-7cd.com');
});
test('does not have exponential performance for data URLs', t => {
for (let index = 0; index < 1000; index += 50) {
const url = 'data:' + Array.from({length: index}).fill(',#').join('') + '\ra';
const start = Date.now();
try {
normalizeUrl(url);
} catch {}
const difference = Date.now() - start;
t.true(difference < 100, `Execution time: ${difference}`);
}
});
test('ignore custom schemes', t => {
t.is(normalizeUrl('tel:004346382763'), 'tel:004346382763');
t.is(normalizeUrl('mailto:[email protected]'), 'mailto:[email protected]');
t.is(normalizeUrl('sindre://www.sindresorhus.com'), 'sindre://www.sindresorhus.com');
t.is(normalizeUrl('foo:bar'), 'foo:bar');
});