-
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathtest.js
More file actions
396 lines (346 loc) · 22.6 KB
/
test.js
File metadata and controls
396 lines (346 loc) · 22.6 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
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
import test from 'ava';
import normalizeUrl from './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('sindresorhus.com', {defaultProtocol: 'https:'}), 'https://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('ftp://sindresorhus.com:21'), 'ftp://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*%7C%3C%3E%3A%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%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F598160082_1280x720.jpg&src1=https%3A%2F%2Ff.vimeocdn.com%2Fimages_v6%2Fshare%2Fplay_icon_overlay.png');
});
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('user:[email protected]'), 'http://sindresorhus.com');
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('user:[email protected]', options), 'http://user:[email protected]');
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');
t.is(normalizeUrl('sindre://user:[email protected]', options), 'sindre://sorhus.com');
t.is(normalizeUrl('sindre://www.sorhus.com', options), 'sindre://sorhus.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('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('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');
});
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('view-source URL', t => {
t.throws(() => {
normalizeUrl('view-source:https://www.sindresorhus.com');
}, {
message: '`view-source:` is not supported as it is a non-standard protocol',
});
});
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}`);
}
});