/
tests.js
323 lines (269 loc) · 7.36 KB
/
tests.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
import assert from 'node:assert/strict';
import test from 'node:test';
import urlJoin from '../lib/url-join.js';
test('joins a simple URL', () => {
assert.equal(
urlJoin('http://www.google.com/', 'foo/bar', '?test=123'),
'http://www.google.com/foo/bar?test=123'
);
});
test('joins a simple URL with new syntax', () => {
assert.equal(
urlJoin(['http://www.google.com/', 'foo/bar', '?test=123']),
'http://www.google.com/foo/bar?test=123'
);
});
test('joins a hashbang', () => {
assert.equal(
urlJoin(['http://www.google.com', '#!', 'foo/bar', '?test=123']),
'http://www.google.com/#!/foo/bar?test=123'
);
});
test('joins a protocol', () => {
assert.equal(
urlJoin('http:', 'www.google.com/', 'foo/bar', '?test=123'),
'http://www.google.com/foo/bar?test=123'
);
});
test('joins a protocol with slashes', () => {
assert.equal(
urlJoin('http://', 'www.google.com/', 'foo/bar', '?test=123'),
'http://www.google.com/foo/bar?test=123'
);
});
test('removes extra slashes', () => {
assert.equal(
urlJoin('http:', 'www.google.com///', 'foo/bar', '?test=123'),
'http://www.google.com/foo/bar?test=123'
);
});
test('removes extra slashes in an encoded URL', () => {
assert.equal(
urlJoin('http:', 'www.google.com///', 'foo/bar', '?url=http%3A//Ftest.com'),
'http://www.google.com/foo/bar?url=http%3A//Ftest.com'
);
assert.equal(
urlJoin('http://a.com/23d04b3/', '/b/c.html'),
'http://a.com/23d04b3/b/c.html'
);
assert.equal(
urlJoin("/foo", "/", "bar", "?test=123"),
"/foo/bar?test=123"
);
});
test('joins anchors in URLs', () => {
assert.equal(
urlJoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '#faaaaa'),
'http://www.google.com/foo/bar?test=123#faaaaa'
);
});
test('joins protocol-relative URLs', () => {
assert.equal(
urlJoin('//www.google.com', 'foo/bar', '?test=123'),
'//www.google.com/foo/bar?test=123'
);
});
test('joins file protocol URLs', () => {
assert.equal(
urlJoin('file:/', 'android_asset', 'foo/bar'),
'file://android_asset/foo/bar'
);
assert.equal(
urlJoin('file:', '/android_asset', 'foo/bar'),
'file://android_asset/foo/bar'
);
});
test('joins absolute file protocol URLs', () => {
assert.equal(
urlJoin('file:', '///android_asset', 'foo/bar'),
'file:///android_asset/foo/bar'
);
assert.equal(
urlJoin('file:///', 'android_asset', 'foo/bar'),
'file:///android_asset/foo/bar'
);
assert.equal(
urlJoin('file:///', '//android_asset', 'foo/bar'),
'file:///android_asset/foo/bar'
);
assert.equal(
urlJoin('file:///android_asset', 'foo/bar'),
'file:///android_asset/foo/bar'
);
});
test('joins multiple query params', () => {
assert.equal(
urlJoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?key=456'),
'http://www.google.com/foo/bar?test=123&key=456'
);
assert.equal(
urlJoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?boom=value', '&key=456'),
'http://www.google.com/foo/bar?test=123&boom=value&key=456'
);
assert.equal(
urlJoin('http://example.org/x', '?a=1', '?b=2', '?c=3', '?d=4'),
'http://example.org/x?a=1&b=2&c=3&d=4'
);
assert.equal(
urlJoin('http:', 'www.google.com///', 'foo/bar', '&test=123', '&key=456'),
'http://www.google.com/foo/bar?test=123&key=456'
);
assert.equal(
urlJoin('http:', 'www.google.com///', 'foo/bar', '&test=123', '?key=456'),
'http://www.google.com/foo/bar?test=123&key=456'
);
});
test('filters out empty query parameters', () => {
assert.equal(
urlJoin('http://google.com', '?'),
'http://google.com'
);
assert.equal(
urlJoin('http://google.com', '&'),
'http://google.com'
);
});
test('joins slashes in paths', () => {
assert.equal(
urlJoin('http://example.org', 'a//', 'b//', 'A//', 'B//'),
'http://example.org/a/b/A/B/'
);
});
test('joins colons in paths', () => {
assert.equal(
urlJoin('http://example.org/', ':foo:', 'bar'),
'http://example.org/:foo:/bar'
);
});
test('joins a simple path without a URL', () => {
assert.equal(
urlJoin('/', 'test'),
'/test'
);
});
test('throws for segments that are not a string', () => {
assert.throws(
() => urlJoin(true),
/Url must be a string. Received true/
);
assert.throws(
() => urlJoin('http://blabla.com/', 1),
/Url must be a string. Received 1/
);
assert.throws(
() => urlJoin('http://blabla.com/', undefined, 'test'),
/Url must be a string. Received undefined/
);
assert.throws(
() => urlJoin('http://blabla.com/', null, 'test'),
/Url must be a string. Received null/
);
assert.throws(
() => urlJoin('http://blabla.com/', { foo: 123 }, 'test'),
/Url must be a string. Received \[object Object\]/
);
});
test('joins a path with a colon', () => {
assert.equal(
urlJoin('/users/:userId', '/cars/:carId'),
'/users/:userId/cars/:carId'
);
});
test('joins slashes in the protocol', () => {
assert.equal(
urlJoin('http://example.org', 'a'),
'http://example.org/a'
);
assert.equal(
urlJoin('http:', '//example.org', 'a'),
'http://example.org/a'
);
assert.equal(
urlJoin('http:///example.org', 'a'),
'http://example.org/a'
);
assert.equal(
urlJoin('file:///example.org', 'a'),
'file:///example.org/a'
);
assert.equal(
urlJoin('file:example.org', 'a'),
'file://example.org/a'
);
assert.equal(
urlJoin('file:/', 'example.org', 'a'),
'file://example.org/a'
);
assert.equal(
urlJoin('file:', '/example.org', 'a'),
'file://example.org/a'
);
assert.equal(
urlJoin('file:', '//example.org', 'a'),
'file://example.org/a'
);
});
test('skips empty strings', () => {
assert.equal(
urlJoin('http://foobar.com', '', 'test'),
'http://foobar.com/test'
);
assert.equal(
urlJoin('', 'http://foobar.com', '', 'test'),
'http://foobar.com/test'
);
});
test('returns an empty string if no arguments are supplied', () => {
assert.equal(
urlJoin(),
''
);
});
test('does not mutate the original input', () => {
const input = ['http:', 'www.google.com/', 'foo/bar', '?test=123'];
const expected = Array.from(input);
urlJoin(input);
assert.deepEqual(input, expected);
});
test('does not replace query params after the hash', () => {
assert.equal(
urlJoin('http://example.com', '#a?b?c'),
'http://example.com#a?b?c'
);
});
test('joins broken up query params', () => {
assert.equal(
urlJoin('http://example.com', '/foo/bar?', 'test=123'),
'http://example.com/foo/bar?test=123'
);
});
test('joins broken up hash', () => {
assert.equal(
urlJoin('http://example.com', '/foo/bar#', 'some-hash'),
'http://example.com/foo/bar#some-hash'
);
});
test('joins leading empty string', () => {
assert.equal(
urlJoin('', '/test'),
'/test'
);
});
test('joins a leading IPv6 hostname', () => {
assert.equal(
urlJoin('[2601:195:c381:3560::f42a]/', '/test'),
'[2601:195:c381:3560::f42a]/test'
);
});
test('joins a leading IPv6 host with an IPv4 address in the least significant 32 bits', () => {
assert.equal(
urlJoin('[2601:195:c381:3560::0.0.244.42]', '/test'),
'[2601:195:c381:3560::0.0.244.42]/test'
);
});
test('joins a protocol followed by an IPv6 host', () => {
assert.equal(
urlJoin('https://', '[2601:195:c381:3560::f42a]/', '/test'),
'https://[2601:195:c381:3560::f42a]/test'
);
});