Debian Codegraph @

tests/qunit/resources/mediawiki.Uri.test.js

raw symbols SCIP
1 QUnit.module( 'mediawiki.Uri', ( hooks ) => {
2 hooks.beforeEach( function () {
3 this.sandbox.stub( mw, 'Uri', mw.UriRelative( 'http://example.org/w/index.php' ) );
4 } );
5
6 QUnit.test.each( 'new mw.Uri( string ) with HTTP value', {
7 'strict mode': true,
8 'non-strict mode': false
9 }, ( assert, strictMode ) => {
10 const uriString = 'http://www.ietf.org/rfc/rfc2396.txt';
11 const uri = new mw.Uri( uriString, {
12 strictMode: strictMode
13 } );
14
15 assert.propContains( uri,
16 {
17 protocol: 'http',
18 user: undefined,
19 password: undefined,
20 host: 'www.ietf.org',
21 port: undefined,
22 path: '/rfc/rfc2396.txt',
23 query: {},
24 fragment: undefined
25 },
26 'basic object properties'
27 );
28
29 assert.deepEqual(
30 {
31 userInfo: uri.getUserInfo(),
32 authority: uri.getAuthority(),
33 hostPort: uri.getHostPort(),
34 queryString: uri.getQueryString(),
35 relativePath: uri.getRelativePath(),
36 toString: uri.toString()
37 },
38 {
39 userInfo: '',
40 authority: 'www.ietf.org',
41 hostPort: 'www.ietf.org',
42 queryString: '',
43 relativePath: '/rfc/rfc2396.txt',
44 toString: uriString
45 },
46 'construct composite components of URI on request'
47 );
48 } );
49
50 QUnit.test.each( 'new mw.Uri( string ) with overrideKeys', [
51 [ true, {
52 // simple parameter
53 n: '1',
54 // last key overrides earlier keys
55 m: 'bar'
56 } ],
57 [ false, {
58 // simple parameter
59 n: '1',
60 // preserve order of multi-value parameters
61 m: [ 'foo', 'bar' ]
62 } ]
63 ], ( assert, [ overrideKeys, expected ] ) => {
64 const uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
65 overrideKeys: overrideKeys
66 } );
67
68 assert.propEqual( uri.query, expected, 'query object' );
69 } );
70
71 QUnit.test( 'new mw.Uri( string ) with non-HTTP values', ( assert ) => {
72 let uri = new mw.Uri( 'ftp://usr:[email protected]/' );
73 assert.propContains( uri,
74 {
75 protocol: 'ftp',
76 user: 'usr',
77 password: 'pwd',
78 host: '192.0.2.16',
79 port: undefined,
80 path: '/',
81 query: {},
82 fragment: undefined
83 },
84 'ftp URI with user and password'
85 );
86
87 uri = new mw.Uri( 'http://example.com/?foo[1]=b&foo[0]=a&foo[]=c' );
88 assert.deepEqual(
89 uri.query,
90 {
91 'foo[1]': 'b',
92 'foo[0]': 'a',
93 'foo[]': 'c'
94 },
95 'Array query parameters parsed as normal with arrayParams:false'
96 );
97 assert.throws(
98 () => new mw.Uri( 'glaswegian penguins' ),
99 ( e ) => e.message === 'Bad constructor arguments',
100 'throw error on non-URI as argument to constructor'
101 );
102 assert.throws(
103 () => new mw.Uri( 'example.com/bar/baz', {
104 strictMode: true
105 } ),
106 ( e ) => e.message === 'Bad constructor arguments',
107 'throw error on URI without protocol or // or leading / in strict mode'
108 );
109
110 uri = new mw.Uri( 'example.com/bar/baz', {
111 strictMode: false
112 } );
113 assert.strictEqual( uri.toString(), 'http://example.com/bar/baz', 'normalize URI without protocol or // in loose mode' );
114
115 uri = new mw.Uri( 'http://example.com/index.php?key=key&hasOwnProperty=hasOwnProperty&constructor=constructor&watch=watch' );
116 assert.deepEqual(
117 uri.query,
118 {
119 key: 'key',
120 constructor: 'constructor',
121 hasOwnProperty: 'hasOwnProperty',
122 watch: 'watch'
123 },
124 'Keys in query strings support names of Object prototypes (bug T114344)'
125 );
126 } );
127
128 QUnit.test( 'new mw.Uri( Object )', ( assert ) => {
129 let uri = new mw.Uri( {
130 protocol: 'http',
131 host: 'www.foo.local',
132 path: '/this'
133 } );
134 assert.strictEqual( uri.toString(), 'http://www.foo.local/this', 'simple input' );
135
136 uri = new mw.Uri( {
137 protocol: 'http',
138 host: 'www.foo.local',
139 path: '/this',
140 query: { hi: 'there' },
141 fragment: 'blah'
142 } );
143 assert.strictEqual( uri.toString(), 'http://www.foo.local/this?hi=there#blah', 'complex input' );
144
145 assert.throws(
146 () => new mw.Uri( {
147 protocol: 'http',
148 host: 'www.foo.local'
149 } ),
150 ( e ) => e.message === 'Bad constructor arguments',
151 'missing required properties'
152 );
153 } );
154
155 QUnit.test( 'new mw.Uri( empty )', ( assert ) => {
156 const testuri = 'http://example.org/w/index.php?a=1&a=2';
157 const MyUri = mw.UriRelative( testuri );
158
159 let uri = new MyUri();
160 assert.strictEqual( uri.toString(), testuri, 'no arguments' );
161
162 uri = new MyUri( undefined );
163 assert.strictEqual( uri.toString(), testuri, 'undefined' );
164
165 uri = new MyUri( null );
166 assert.strictEqual( uri.toString(), testuri, 'null' );
167
168 uri = new MyUri( '' );
169 assert.strictEqual( uri.toString(), testuri, 'empty string' );
170
171 uri = new MyUri( null, { overrideKeys: true } );
172 assert.deepEqual( uri.query, { a: '2' }, 'null, with options' );
173 } );
174
175 QUnit.test( 'Setting properties', ( assert ) => {
176 const uriBase = new mw.Uri( 'http://en.wiki.local/w/api.php' );
177
178 let uri = uriBase.clone();
179 uri.fragment = 'frag';
180 assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php#frag', 'add a fragment' );
181 uri.fragment = 'café';
182 assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php#caf%C3%A9', 'fragment is url-encoded' );
183
184 uri = uriBase.clone();
185 uri.host = 'fr.wiki.local';
186 uri.port = '8080';
187 assert.strictEqual( uri.toString(), 'http://fr.wiki.local:8080/w/api.php', 'change host and port' );
188
189 uri = uriBase.clone();
190 uri.query.foo = 'bar';
191 assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'add query arguments' );
192
193 delete uri.query.foo;
194 assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php', 'delete query arguments' );
195
196 uri = uriBase.clone();
197 uri.query.foo = 'bar';
198 assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'extend query arguments' );
199 uri.extend( {
200 foo: 'quux',
201 pif: 'paf',
202 absent: undefined // T372742
203 } );
204 assert.true( uri.toString().includes( 'foo=quux' ), 'extend query arguments' );
205 assert.false( uri.toString().includes( 'foo=bar' ), 'extend query arguments' );
206 assert.true( uri.toString().includes( 'pif=paf' ), 'extend query arguments' );
207 assert.false( uri.toString().includes( 'absent' ), 'extend query arguments' );
208 } );
209
210 QUnit.test( '.getQueryString()', ( assert ) => {
211 let uri = new mw.Uri( 'http://search.example.com/?q=uri' );
212
213 assert.propContains( uri,
214 {
215 protocol: 'http',
216 user: undefined,
217 password: undefined,
218 host: 'search.example.com',
219 port: undefined,
220 path: '/',
221 query: { q: 'uri' },
222 fragment: undefined
223 },
224 'basic object properties'
225 );
226 assert.strictEqual( uri.getQueryString(), 'q=uri', 'query string' );
227
228 uri = new mw.Uri( 'https://example.com/mw/index.php?title=Sandbox/7&other=Sandbox/7&foo' );
229 assert.strictEqual(
230 uri.getQueryString(),
231 'title=Sandbox/7&other=Sandbox%2F7&foo',
232 'title parameter is escaped the wiki-way'
233 );
234 } );
235
236 QUnit.test( 'arrayParams', ( assert ) => {
237 const uri1 = new mw.Uri( 'http://example.com/?foo[]=a&foo[]=b&foo[]=c', { arrayParams: true } );
238 const uri2 = new mw.Uri( 'http://example.com/?foo[0]=a&foo[1]=b&foo[2]=c', { arrayParams: true } );
239 const uri3 = new mw.Uri( 'http://example.com/?foo[1]=b&foo[0]=a&foo[]=c', { arrayParams: true } );
240 const expectedQ = { foo: [ 'a', 'b', 'c' ] };
241 const expectedS = 'foo%5B0%5D=a&foo%5B1%5D=b&foo%5B2%5D=c';
242
243 assert.deepEqual( uri1.query, expectedQ,
244 'array query parameters are parsed (implicit indexes)' );
245 assert.deepEqual( uri1.getQueryString(), expectedS,
246 'array query parameters are encoded (always with explicit indexes)' );
247 assert.deepEqual( uri2.query, expectedQ,
248 'array query parameters are parsed (explicit indexes)' );
249 assert.deepEqual( uri2.getQueryString(), expectedS,
250 'array query parameters are encoded (always with explicit indexes)' );
251 assert.deepEqual( uri3.query, expectedQ,
252 'array query parameters are parsed (mixed indexes, out of order)' );
253 assert.deepEqual( uri3.getQueryString(), expectedS,
254 'array query parameters are encoded (always with explicit indexes)' );
255
256 const uriMissing = new mw.Uri( 'http://example.com/?foo[0]=a&foo[2]=c', { arrayParams: true } );
257 // eslint-disable-next-line no-sparse-arrays
258 const expectedMissingQ = { foo: [ 'a', , 'c' ] };
259 const expectedMissingS = 'foo%5B0%5D=a&foo%5B2%5D=c';
260
261 assert.deepEqual( uriMissing.query, expectedMissingQ,
262 'array query parameters are parsed (missing array item)' );
263 assert.deepEqual( uriMissing.getQueryString(), expectedMissingS,
264 'array query parameters are encoded (missing array item)' );
265
266 const uriWeird = new mw.Uri( 'http://example.com/?foo[0]=a&foo[1][1]=b&foo[x]=c', { arrayParams: true } );
267 const expectedWeirdQ = { foo: [ 'a' ], 'foo[1][1]': 'b', 'foo[x]': 'c' };
268 const expectedWeirdS = 'foo%5B0%5D=a&foo%5B1%5D%5B1%5D=b&foo%5Bx%5D=c';
269
270 assert.deepEqual( uriWeird.query, expectedWeirdQ,
271 'array query parameters are parsed (multi-dimensional or associative arrays are ignored)' );
272 assert.deepEqual( uriWeird.getQueryString(), expectedWeirdS,
273 'array query parameters are encoded (multi-dimensional or associative arrays are ignored)' );
274 } );
275
276 QUnit.test( '.clone()', ( assert ) => {
277 const original = new mw.Uri( 'http://foo.example.org/index.php?one=1&two=2' );
278 const clone = original.clone();
279
280 assert.deepEqual( clone, original, 'clone has equivalent properties' );
281 assert.strictEqual( original.toString(), clone.toString(), 'toString matches original' );
282
283 assert.notStrictEqual( clone, original, 'clone is a different object when compared by reference' );
284
285 clone.host = 'bar.example.org';
286 assert.notStrictEqual( original.host, clone.host, 'manipulating clone did not effect original' );
287 assert.notStrictEqual( original.toString(), clone.toString(), 'Stringified url no longer matches original' );
288
289 clone.query.three = 3;
290
291 assert.deepEqual(
292 original.query,
293 { one: '1', two: '2' },
294 'Properties is deep cloned (T39708)'
295 );
296 } );
297
298 QUnit.test( '.toString() after query manipulation', ( assert ) => {
299 let uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
300 overrideKeys: true
301 } );
302
303 uri.query.n = [ 'x', 'y', 'z' ];
304
305 // Verify parts and total length instead of entire string because order
306 // of iteration can vary.
307 assert.true( uri.toString().includes( 'm=bar' ), 'toString preserves other values' );
308 assert.true( uri.toString().includes( 'n=x&n=y&n=z' ), 'toString parameter includes all values of an array query parameter' );
309 assert.strictEqual( uri.toString().length, 'http://www.example.com/dir/?m=bar&n=x&n=y&n=z'.length, 'toString matches expected string' );
310
311 uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
312 overrideKeys: false
313 } );
314
315 // Change query values
316 uri.query.n = [ 'x', 'y', 'z' ];
317
318 // Verify parts and total length instead of entire string because order
319 // of iteration can vary.
320 assert.true( uri.toString().includes( 'm=foo&m=bar' ), 'toString preserves other values' );
321 assert.true( uri.toString().includes( 'n=x&n=y&n=z' ), 'toString parameter includes all values of an array query parameter' );
322 assert.strictEqual( uri.toString().length, 'http://www.example.com/dir/?m=foo&m=bar&n=x&n=y&n=z'.length, 'toString matches expected string' );
323
324 // Remove query values
325 uri.query.m.splice( 0, 1 );
326 delete uri.query.n;
327
328 assert.strictEqual( uri.toString(), 'http://www.example.com/dir/?m=bar', 'deletion properties' );
329
330 // Remove more query values, leaving an empty array
331 uri.query.m.splice( 0, 1 );
332 assert.strictEqual( uri.toString(), 'http://www.example.com/dir/', 'empty array value is omitted' );
333 } );
334
335 QUnit.test( 'Variable defaultUri', ( assert ) => {
336 let href = 'http://example.org/w/index.php#here';
337 const UriClass = mw.UriRelative( () => href );
338
339 let uri = new UriClass();
340 assert.propContains( uri,
341 {
342 protocol: 'http',
343 user: undefined,
344 password: undefined,
345 host: 'example.org',
346 port: undefined,
347 path: '/w/index.php',
348 query: {},
349 fragment: 'here'
350 },
351 'basic object properties'
352 );
353
354 // Default URI may change, e.g. via history.replaceState, pushState or location.hash (T74334)
355 href = 'https://example.com/wiki/Foo?v=2';
356 uri = new UriClass();
357 assert.propContains( uri,
358 {
359 protocol: 'https',
360 user: undefined,
361 password: undefined,
362 host: 'example.com',
363 port: undefined,
364 path: '/wiki/Foo',
365 query: { v: '2' },
366 fragment: undefined
367 },
368 'basic object properties'
369 );
370 } );
371
372 QUnit.test( 'Advanced URL', ( assert ) => {
373 const uri = new mw.Uri( 'http://[email protected]:81/dir/dir.2/index.htm?q1=0&&test1&test2=value+%28escaped%29#caf%C3%A9' );
374 assert.propContains( uri,
375 {
376 protocol: 'http',
377 user: 'auth',
378 password: undefined,
379 host: 'www.example.com',
380 port: '81',
381 path: '/dir/dir.2/index.htm',
382 query: { q1: '0', test1: null, test2: 'value (escaped)' },
383 fragment: 'café'
384 },
385 'basic object properties'
386 );
387
388 assert.strictEqual( uri.getUserInfo(), 'auth', 'user info' );
389 assert.strictEqual( uri.getAuthority(), '[email protected]:81', 'authority equal to auth@hostport' );
390 assert.strictEqual( uri.getHostPort(), 'www.example.com:81', 'hostport equal to host:port' );
391
392 const queryString = uri.getQueryString();
393 assert.true( queryString.includes( 'q1=0' ), 'query param with numbers' );
394 assert.true( queryString.includes( 'test1' ), 'query param with null value is included' );
395 assert.false( queryString.includes( 'test1=' ), 'query param with null value does not generate equals sign' );
396 assert.true( queryString.includes( 'test2=value+%28escaped%29' ), 'query param is url escaped' );
397
398 const relativePath = uri.getRelativePath();
399 assert.true( relativePath.includes( uri.path ), 'path in relative path' );
400 assert.true( relativePath.includes( uri.getQueryString() ), 'query string in relative path' );
401 assert.true( relativePath.includes( mw.Uri.encode( uri.fragment ) ), 'escaped fragment in relative path' );
402 } );
403
404 QUnit.test( 'Parse a uri with an @ symbol in the path and query', ( assert ) => {
405 const uri = new mw.Uri( 'http://www.example.com/test@test?x=@uri&y@=uri&z@=@' );
406 assert.propContains( uri,
407 {
408 protocol: 'http',
409 user: undefined,
410 password: undefined,
411 host: 'www.example.com',
412 port: undefined,
413 path: '/test@test',
414 query: { x: '@uri', 'y@': 'uri', 'z@': '@' },
415 fragment: undefined
416 },
417 'basic object properties'
418 );
419 assert.strictEqual( uri.getQueryString(), 'x=%40uri&y%40=uri&z%40=%40', 'query string' );
420 } );
421
422 QUnit.test( 'Handle protocol-relative URLs', ( assert ) => {
423 const UriRel = mw.UriRelative( 'glork://en.wiki.local/foo.php' );
424
425 let uri = new UriRel( '//en.wiki.local/w/api.php' );
426 assert.strictEqual( uri.protocol, 'glork', 'create protocol-relative URLs with same protocol as document' );
427
428 uri = new UriRel( '/foo.com' );
429 assert.strictEqual( uri.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in loose mode' );
430
431 uri = new UriRel( 'http:/foo.com' );
432 assert.strictEqual( uri.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in loose mode' );
433
434 uri = new UriRel( '/foo.com', true );
435 assert.strictEqual( uri.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in strict mode' );
436
437 uri = new UriRel( 'http:/foo.com', true );
438 assert.strictEqual( uri.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in strict mode' );
439 } );
440
441 QUnit.test( 'T37658', ( assert ) => {
442 const testProtocol = 'https://';
443 const testServer = 'foo.example.org';
444 const testPort = '3004';
445 const testPath = '/!1qy';
446
447 let UriClass = mw.UriRelative( testProtocol + testServer + '/some/path/index.html' );
448 let uri = new UriClass( testPath );
449 let href = uri.toString();
450 assert.strictEqual( href, testProtocol + testServer + testPath, 'Root-relative URL gets host & protocol supplied' );
451
452 UriClass = mw.UriRelative( testProtocol + testServer + ':' + testPort + '/some/path.php' );
453 uri = new UriClass( testPath );
454 href = uri.toString();
455 assert.strictEqual( href, testProtocol + testServer + ':' + testPort + testPath, 'Root-relative URL gets host, protocol, and port supplied' );
456 } );
457 } );