Debian Codegraph @

test/url-toolkit.js

raw symbols SCIP
1 const URLToolkit = require('../src/url-toolkit');
2
3 describe('url toolkit', () => {
4 describe('works with a selection of valid urls', () => {
5 // From spec: https://tools.ietf.org/html/rfc1808#section-5.1
6 test('http://a/b/c/d;p?q#f', 'g:h', 'g:h');
7 test('http://a/b/c/d;p?q#f', 'g', 'http://a/b/c/g');
8 test('http://a/b/c/d;p?q#f', './g', 'http://a/b/c/g');
9 test('http://a/b/c/d;p?q#f', 'g/', 'http://a/b/c/g/');
10 test('http://a/b/c/d;p?q#f', '/g', 'http://a/g');
11 test('http://a/b/c/d;p?q#f', '//g', 'http://g');
12 test('http://a/b/c/d;p?q#f', '?y', 'http://a/b/c/d;p?y');
13 test('http://a/b/c/d;p?q#f', 'g?y', 'http://a/b/c/g?y');
14 test('http://a/b/c/d;p?q#f', 'g?y/./x', 'http://a/b/c/g?y/./x');
15 test('http://a/b/c/d;p?q#f', '#s', 'http://a/b/c/d;p?q#s');
16 test('http://a/b/c/d;p?q#f', 'g#s', 'http://a/b/c/g#s');
17 test('http://a/b/c/d;p?q#f', 'g#s/./x', 'http://a/b/c/g#s/./x');
18 test('http://a/b/c/d;p?q#f', 'g?y#s', 'http://a/b/c/g?y#s');
19 test('http://a/b/c/d;p?q#f', ';x', 'http://a/b/c/d;x');
20 test('http://a/b/c/d;p?q#f', 'g;x', 'http://a/b/c/g;x');
21 test('http://a/b/c/d;p?q#f', 'g;x?y#s', 'http://a/b/c/g;x?y#s');
22 test('http://a/b/c/d;p?q#f', '.', 'http://a/b/c/');
23 test('http://a/b/c/d;p?q#f', './', 'http://a/b/c/');
24 test('http://a/b/c/d;p?q#f', '..', 'http://a/b/');
25 test('http://a/b/c/d;p?q#f', '../', 'http://a/b/');
26 test('http://a/b/c/d;p?q#f', '../g', 'http://a/b/g');
27 test('http://a/b/c/d;p?q#f', '../..', 'http://a/');
28 test('http://a/b/c/d;p?q#f', '../../', 'http://a/');
29 test('http://a/b/c/d;p?q#f', '../../g', 'http://a/g');
30
31 test('http://a/b/c/d;p?q#f', '', 'http://a/b/c/d;p?q#f');
32 test('http://a/b/c/d;p?q#f', '../../../g', 'http://a/../g');
33 test('http://a/b/c/d;p?q#f', '../../../../g', 'http://a/../../g');
34 test('http://a/b/c/d;p?q#f', '/./g', 'http://a/./g');
35 test('http://a/b/c/d;p?q#f', '/../g', 'http://a/../g');
36 test('http://a/b/c/d;p?q#f', 'g.', 'http://a/b/c/g.');
37 test('http://a/b/c/d;p?q#f', '.g', 'http://a/b/c/.g');
38 test('http://a/b/c/d;p?q#f', 'g..', 'http://a/b/c/g..');
39 test('http://a/b/c/d;p?q#f', '..g', 'http://a/b/c/..g');
40 test('http://a/b/c/d;p?q#f', './../g', 'http://a/b/g');
41 test('http://a/b/c/d;p?q#f', './g/.', 'http://a/b/c/g/');
42 test('http://a/b/c/d;p?q#f', 'g/./h', 'http://a/b/c/g/h');
43 test('http://a/b/c/d;p?q#f', 'g/../h', 'http://a/b/c/h');
44
45 test('http://a/b/c/d;p?q#f', 'http:g', 'http:g');
46 test('http://a/b/c/d;p?q#f', 'http:', 'http:');
47
48 // Custom
49 test(
50 'http://a.com/b/cd/./e.m3u8?test=1#something',
51 '',
52 'http://a.com/b/cd/./e.m3u8?test=1#something'
53 );
54 test(
55 'http://a.com/b/cd/./e.m3u8?test=1#something',
56 '',
57 'http://a.com/b/cd/e.m3u8?test=1#something',
58 { alwaysNormalize: true }
59 );
60
61 test(
62 'http://a.com/b/cd/e.m3u8',
63 'https://example.com/z.ts',
64 'https://example.com/z.ts'
65 );
66 test('http://a.com/b/cd/e.m3u8', 'g:h', 'g:h');
67 test(
68 'http://a.com/b/cd/e.m3u8',
69 'https://example.com:8080/z.ts',
70 'https://example.com:8080/z.ts'
71 );
72
73 test('http://a.com/b/cd/e.m3u8', 'z.ts', 'http://a.com/b/cd/z.ts');
74 test(
75 'http://a.com:8080/b/cd/e.m3u8',
76 'z.ts',
77 'http://a.com:8080/b/cd/z.ts'
78 );
79 test('http://a.com/b/cd/', 'z.ts', 'http://a.com/b/cd/z.ts');
80 test('http://a.com/b/cd', 'z.ts', 'http://a.com/b/z.ts');
81 test('http://a.com/', 'z.ts', 'http://a.com/z.ts');
82 test('http://a.com/?test=1', 'z.ts', 'http://a.com/z.ts');
83 test('http://a.com', 'z.ts', 'http://a.com/z.ts');
84 test('http://a.com?test=1', 'z.ts', 'http://a.com/z.ts');
85 test('http://a.com/b/cd?test=1', 'z.ts', 'http://a.com/b/z.ts');
86 test('http://a.com/b/cd#something', 'z.ts', 'http://a.com/b/z.ts');
87 test('http://a.com/b/cd?test=1#something', 'z.ts', 'http://a.com/b/z.ts');
88 test(
89 'http://a.com/b/cd?test=1#something',
90 'z.ts?abc=1',
91 'http://a.com/b/z.ts?abc=1'
92 );
93 test(
94 'http://a.com/b/cd?test=1#something',
95 'z.ts#test',
96 'http://a.com/b/z.ts#test'
97 );
98 test(
99 'http://a.com/b/cd?test=1#something',
100 'z.ts?abc=1#test',
101 'http://a.com/b/z.ts?abc=1#test'
102 );
103
104 test('http://a.com/b/cd?test=1#something', ';x', 'http://a.com/b/cd;x');
105 test('http://a.com/b/cd?test=1#something', './;x', 'http://a.com/b/;x');
106 test('http://a.com/b/cd?test=1#something', 'g;x', 'http://a.com/b/g;x');
107 test('http://a_b.com/b/cd?test=1#something', 'g;x', 'http://a_b.com/b/g;x');
108 test('http://a-b.com/b/cd?test=1#something', 'g;x', 'http://a-b.com/b/g;x');
109 test('http://a.b.com/b/cd?test=1#something', 'g;x', 'http://a.b.com/b/g;x');
110 test('http://a~b.com/b/cd?test=1#something', 'g;x', 'http://a~b.com/b/g;x');
111
112 test('a.com', 'z.ts', 'a.com/z.ts');
113 test('a.com/', 'z.ts', 'a.com/z.ts');
114 test('a.com/b/cd', 'z.ts', 'a.com/b/z.ts');
115 test('a.com/b/cd', '../z.ts', 'a.com/z.ts');
116 test('a.com/b/cd', '/z.ts', 'a.com/z.ts');
117 test('a.com/b/cd', '/b/z.ts', 'a.com/b/z.ts');
118
119 test(
120 'http://a.com/b/cd/e.m3u8?test=1#something',
121 'subdir/z.ts?abc=1#test',
122 'http://a.com/b/cd/subdir/z.ts?abc=1#test'
123 );
124 test(
125 'http://a.com/b/cd/e.m3u8?test=1#something',
126 '/subdir/z.ts?abc=1#test',
127 'http://a.com/subdir/z.ts?abc=1#test'
128 );
129 test(
130 'http://a.com/b/cd/e.m3u8?test=1#something',
131 '//example.com/z.ts?abc=1#test',
132 'http://example.com/z.ts?abc=1#test'
133 );
134 test(
135 'https://a.com/b/cd/e.m3u8?test=1#something',
136 '//example.com/z.ts?abc=1#test',
137 'https://example.com/z.ts?abc=1#test'
138 );
139 test(
140 'https://a.com/b/cd/e.m3u8?test=1#something',
141 './z.ts?abc=1#test',
142 'https://a.com/b/cd/z.ts?abc=1#test'
143 );
144 test(
145 'https://a.com/b/cd/e.m3u8?test=1#something',
146 '../z.ts?abc=1#test',
147 'https://a.com/b/z.ts?abc=1#test'
148 );
149 test(
150 'https://a.com/b/cd/e.m3u8?test=1#something',
151 './../z.ts?abc=1#test',
152 'https://a.com/b/z.ts?abc=1#test'
153 );
154 test(
155 'https://a.com/b/cd/e.m3u8?test=1#something',
156 '././z.ts?abc=1#test',
157 'https://a.com/b/cd/z.ts?abc=1#test'
158 );
159 test(
160 'https://a.com/b/cd/e/f.m3u8?test=1#something',
161 '../../z.ts?abc=1#test',
162 'https://a.com/b/z.ts?abc=1#test'
163 );
164 test(
165 'https://a.com/b/cd/e.m3u8?test=1#something',
166 '../../z.ts?abc=1#test',
167 'https://a.com/z.ts?abc=1#test'
168 );
169 test(
170 'https://a.com/b/cd/e.m3u8?test=1#something',
171 '../../z.ts?abc=1&something=blah/./../test#test',
172 'https://a.com/z.ts?abc=1&something=blah/./../test#test'
173 );
174 test(
175 'https://a.com/b/cd/e/f.m3u8?test=1#something',
176 './../../z.ts?abc=1#test',
177 'https://a.com/b/z.ts?abc=1#test'
178 );
179
180 test(
181 'https://a.com/b/cd/e.m3u8?test=1#something',
182 'subdir/pointless/../z.ts?abc=1#test',
183 'https://a.com/b/cd/subdir/z.ts?abc=1#test'
184 );
185 test(
186 'https://a.com/b/cd/e.m3u8?test=1#something',
187 '/subdir/pointless/../z.ts?abc=1#test',
188 'https://a.com/subdir/z.ts?abc=1#test',
189 { alwaysNormalize: true }
190 );
191 test(
192 'https://a.com/b/cd/e.m3u8?test=1#something',
193 '/subdir/pointless/../z.ts?abc=1#test',
194 'https://a.com/subdir/pointless/../z.ts?abc=1#test'
195 );
196 test(
197 'https://a.com/b/cd/e.m3u8?test=1#something',
198 '//example.com/subdir/pointless/../z.ts?abc=1#test',
199 'https://example.com/subdir/z.ts?abc=1#test',
200 { alwaysNormalize: true }
201 );
202 test(
203 'https://a.com/b/cd/e.m3u8?test=1#something',
204 '//example.com/subdir/pointless/../z.ts?abc=1#test',
205 'https://example.com/subdir/pointless/../z.ts?abc=1#test'
206 );
207
208 test(
209 'https://a-b.something.com/b/cd/e.m3u8?test=1#something',
210 '//example.com/subdir/pointless/../z.ts?abc=1#test',
211 'https://example.com/subdir/z.ts?abc=1#test',
212 { alwaysNormalize: true }
213 );
214 test(
215 'https://a-b.something.com/b/cd/e.m3u8?test=1#something',
216 '//example.com/subdir/pointless/../z.ts?abc=1#test',
217 'https://example.com/subdir/pointless/../z.ts?abc=1#test'
218 );
219
220 test(
221 '//a.com/b/cd/e.m3u8',
222 'https://example.com/z.ts',
223 'https://example.com/z.ts'
224 );
225 test('//a.com/b/cd/e.m3u8', '//example.com/z.ts', '//example.com/z.ts');
226 test(
227 '//a.com/b/cd/e.m3u8',
228 '/example.com/z.ts',
229 '//a.com/example.com/z.ts'
230 );
231 test('//a.com/b/cd/e.m3u8', 'g:h', 'g:h');
232 test(
233 '//a.com/b/cd/e.m3u8',
234 'https://example.com:8080/z.ts',
235 'https://example.com:8080/z.ts'
236 );
237 test('//a.com/b/cd/e.m3u8', 'z.ts', '//a.com/b/cd/z.ts');
238 test('//a.com/b/cd/e.m3u8', '../../z.ts', '//a.com/z.ts');
239
240 test('//a.com/b/cd/e.m3u8', '../../../z.ts', '//a.com/../z.ts');
241
242 test(
243 '/a/b/cd/e.m3u8',
244 'https://example.com/z.ts',
245 'https://example.com/z.ts'
246 );
247 test('/a/b/cd/e.m3u8', '/example.com/z.ts', '/example.com/z.ts');
248 test('/a/b/cd/e.m3u8', '//example.com/z.ts', '//example.com/z.ts');
249 test('/a/b/cd/e.m3u8', 'g:h', 'g:h');
250 test(
251 '/a/b/cd/e.m3u8',
252 'https://example.com:8080/z.ts',
253 'https://example.com:8080/z.ts'
254 );
255 test('/a/b/cd/e.m3u8', 'z.ts', '/a/b/cd/z.ts');
256 test('/a/b/cd/e.m3u8', '../../../z.ts', '/z.ts');
257
258 test('http://ö.de/a/b', 'z.ts', 'http://ö.de/a/z.ts');
259 test('http://ö.de/a', 'z.ts', 'http://ö.de/z.ts');
260 test('http://ö.de/', 'z.ts', 'http://ö.de/z.ts');
261 test('http://ö.de', 'z.ts', 'http://ö.de/z.ts');
262 test('ö.de', 'z.ts', 'ö.de/z.ts');
263
264 test('http://a/b/c/d;p?q', './', 'http://a/b/c/');
265 test('http://a/b/c/d;p?q', '.', 'http://a/b/c/');
266 test('http://a/b/c/d;p?q', '../', 'http://a/b/');
267 test('http://a/b/c/d;p?q', '..', 'http://a/b/');
268
269 test(
270 'http://a.com/b/cd/e.m3u8?test=1#something',
271 '',
272 'http://a.com/b/cd/e.m3u8?test=1#something'
273 );
274
275 test(
276 'http://a.com/b/cd/e.m3u8?test=1#something',
277 'a_:b',
278 'http://a.com/b/cd/a_:b'
279 );
280 test('http://a.com/b/cd/e.m3u8?test=1#something', 'a:b', 'a:b');
281 test(
282 'http://a.com/b/cd/e.m3u8?test=1#something',
283 './a:b',
284 'http://a.com/b/cd/a:b'
285 );
286
287 test(
288 'http://a.com/expiretime=111;dirmatch=true/master.m3u8',
289 './a:b',
290 'http://a.com/expiretime=111;dirmatch=true/a:b'
291 );
292
293 test('http://0.0.0.0/a/b.c', 'd', 'http://0.0.0.0/a/d');
294 test('http://[0:0:0:0::0]/a/b.c', 'd', 'http://[0:0:0:0::0]/a/d');
295
296 test('http://example.com/', 'a#\nb', 'http://example.com/a#\nb');
297
298 // in the URL living standard (https://url.spec.whatwg.org/)
299 // `http` is a 'special scheme', and that results in
300 // the `///` becoming `//`, meaning `netLoc` would essentially be
301 // `//example.com` instead of `//`
302 // This library is specifically RFC 1808, which does not have these
303 // special cases.
304 test('http:///example.com/a/', '../../b', 'http:///b');
305 });
306 });
307
308 function test(base, relative, expected, opts) {
309 opts = opts || {};
310 it(`"${base}" + "${relative}" ${JSON.stringify(opts)}`, () => {
311 expect(URLToolkit.parseURL(base)).toMatchSnapshot();
312 expect(URLToolkit.parseURL(relative)).toMatchSnapshot();
313 expect(URLToolkit.buildURLFromParts(URLToolkit.parseURL(relative))).toBe(
314 relative
315 );
316 expect(URLToolkit.buildURLFromParts(URLToolkit.parseURL(base))).toBe(base);
317 expect(URLToolkit.buildAbsoluteURL(base, relative, opts)).toBe(expected);
318 });
319 }