2 QUnit
.module( 'mediawiki.Uri', QUnit
.newMwEnvironment( {
4 this.mwUriOrg
= mw
.Uri
;
5 mw
.Uri
= mw
.UriRelative( 'http://example.org/w/index.php' );
7 teardown: function () {
8 mw
.Uri
= this.mwUriOrg
;
13 [ true, false ].forEach( function ( strictMode
) {
14 QUnit
.test( 'Basic construction and properties (' + ( strictMode
? '' : 'non-' ) + 'strict mode)', function ( assert
) {
16 uriString
= 'http://www.ietf.org/rfc/rfc2396.txt';
17 uri
= new mw
.Uri( uriString
, {
18 strictMode
: strictMode
23 protocol
: uri
.protocol
,
28 fragment
: uri
.fragment
33 path
: '/rfc/rfc2396.txt',
37 'basic object properties'
42 userInfo
: uri
.getUserInfo(),
43 authority
: uri
.getAuthority(),
44 hostPort
: uri
.getHostPort(),
45 queryString
: uri
.getQueryString(),
46 relativePath
: uri
.getRelativePath(),
47 toString
: uri
.toString()
51 authority
: 'www.ietf.org',
52 hostPort
: 'www.ietf.org',
54 relativePath
: '/rfc/rfc2396.txt',
57 'construct composite components of URI on request'
62 QUnit
.test( 'Constructor( String[, Object ] )', function ( assert
) {
65 uri
= new mw
.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
69 // Strict comparison to assert that numerical values stay strings
70 assert
.strictEqual( uri
.query
.n
, '1', 'Simple parameter with overrideKeys:true' );
71 assert
.strictEqual( uri
.query
.m
, 'bar', 'Last key overrides earlier keys with overrideKeys:true' );
73 uri
= new mw
.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
77 assert
.strictEqual( uri
.query
.n
, '1', 'Simple parameter with overrideKeys:false' );
78 assert
.strictEqual( uri
.query
.m
[ 0 ], 'foo', 'Order of multi-value parameters with overrideKeys:true' );
79 assert
.strictEqual( uri
.query
.m
[ 1 ], 'bar', 'Order of multi-value parameters with overrideKeys:true' );
80 assert
.strictEqual( uri
.query
.m
.length
, 2, 'Number of mult-value field is correct' );
82 uri
= new mw
.Uri( 'ftp://usr:pwd@192.0.2.16/' );
86 protocol
: uri
.protocol
,
88 password
: uri
.password
,
93 fragment
: uri
.fragment
105 'Parse an ftp URI correctly with user and password'
110 return new mw
.Uri( 'glaswegian penguins' );
113 return e
.message
=== 'Bad constructor arguments';
115 'throw error on non-URI as argument to constructor'
120 return new mw
.Uri( 'example.com/bar/baz', {
125 return e
.message
=== 'Bad constructor arguments';
127 'throw error on URI without protocol or // or leading / in strict mode'
130 uri
= new mw
.Uri( 'example.com/bar/baz', {
133 assert
.strictEqual( uri
.toString(), 'http://example.com/bar/baz', 'normalize URI without protocol or // in loose mode' );
135 uri
= new mw
.Uri( 'http://example.com/index.php?key=key&hasOwnProperty=hasOwnProperty&constructor=constructor&watch=watch' );
140 constructor: 'constructor',
141 hasOwnProperty
: 'hasOwnProperty',
144 'Keys in query strings support names of Object prototypes (bug T114344)'
148 QUnit
.test( 'Constructor( Object )', function ( assert
) {
149 var uri
= new mw
.Uri( {
151 host
: 'www.foo.local',
154 assert
.strictEqual( uri
.toString(), 'http://www.foo.local/this', 'Basic properties' );
158 host
: 'www.foo.local',
160 query
: { hi
: 'there' },
163 assert
.strictEqual( uri
.toString(), 'http://www.foo.local/this?hi=there#blah', 'More complex properties' );
169 host
: 'www.foo.local'
173 return e
.message
=== 'Bad constructor arguments';
175 'Construction failed when missing required properties'
179 QUnit
.test( 'Constructor( empty[, Object ] )', function ( assert
) {
180 var testuri
, MyUri
, uri
;
182 testuri
= 'http://example.org/w/index.php?a=1&a=2';
183 MyUri
= mw
.UriRelative( testuri
);
186 assert
.strictEqual( uri
.toString(), testuri
, 'no arguments' );
188 uri
= new MyUri( undefined );
189 assert
.strictEqual( uri
.toString(), testuri
, 'undefined' );
191 uri
= new MyUri( null );
192 assert
.strictEqual( uri
.toString(), testuri
, 'null' );
194 uri
= new MyUri( '' );
195 assert
.strictEqual( uri
.toString(), testuri
, 'empty string' );
197 uri
= new MyUri( null, { overrideKeys
: true } );
198 assert
.deepEqual( uri
.query
, { a
: '2' }, 'null, with options' );
201 QUnit
.test( 'Properties', function ( assert
) {
204 uriBase
= new mw
.Uri( 'http://en.wiki.local/w/api.php' );
206 uri
= uriBase
.clone();
207 uri
.fragment
= 'frag';
208 assert
.strictEqual( uri
.toString(), 'http://en.wiki.local/w/api.php#frag', 'add a fragment' );
209 uri
.fragment
= 'café';
210 assert
.strictEqual( uri
.toString(), 'http://en.wiki.local/w/api.php#caf%C3%A9', 'fragment is url-encoded' );
212 uri
= uriBase
.clone();
213 uri
.host
= 'fr.wiki.local';
215 assert
.strictEqual( uri
.toString(), 'http://fr.wiki.local:8080/w/api.php', 'change host and port' );
217 uri
= uriBase
.clone();
218 uri
.query
.foo
= 'bar';
219 assert
.strictEqual( uri
.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'add query arguments' );
221 delete uri
.query
.foo
;
222 assert
.strictEqual( uri
.toString(), 'http://en.wiki.local/w/api.php', 'delete query arguments' );
224 uri
= uriBase
.clone();
225 uri
.query
.foo
= 'bar';
226 assert
.strictEqual( uri
.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'extend query arguments' );
231 assert
.strictEqual( uri
.toString().indexOf( 'foo=quux' ) !== -1, true, 'extend query arguments' );
232 assert
.strictEqual( uri
.toString().indexOf( 'foo=bar' ) !== -1, false, 'extend query arguments' );
233 assert
.strictEqual( uri
.toString().indexOf( 'pif=paf' ) !== -1, true, 'extend query arguments' );
236 QUnit
.test( '.getQueryString()', function ( assert
) {
237 var uri
= new mw
.Uri( 'http://search.example.com/?q=uri' );
241 protocol
: uri
.protocol
,
246 fragment
: uri
.fragment
,
247 queryString
: uri
.getQueryString()
251 host
: 'search.example.com',
258 'basic object properties'
261 uri
= new mw
.Uri( 'https://example.com/mw/index.php?title=Sandbox/7&other=Sandbox/7&foo' );
263 uri
.getQueryString(),
264 'title=Sandbox/7&other=Sandbox%2F7&foo',
265 'title parameter is escaped the wiki-way'
270 QUnit
.test( 'arrayParams', function ( assert
) {
271 var uri1
, uri2
, uri3
, expectedQ
, expectedS
,
272 uriMissing
, expectedMissingQ
, expectedMissingS
,
273 uriWeird
, expectedWeirdQ
, expectedWeirdS
;
275 uri1
= new mw
.Uri( 'http://example.com/?foo[]=a&foo[]=b&foo[]=c', { arrayParams
: true } );
276 uri2
= new mw
.Uri( 'http://example.com/?foo[0]=a&foo[1]=b&foo[2]=c', { arrayParams
: true } );
277 uri3
= new mw
.Uri( 'http://example.com/?foo[1]=b&foo[0]=a&foo[]=c', { arrayParams
: true } );
278 expectedQ
= { foo
: [ 'a', 'b', 'c' ] };
279 expectedS
= 'foo%5B0%5D=a&foo%5B1%5D=b&foo%5B2%5D=c';
281 assert
.deepEqual( uri1
.query
, expectedQ
,
282 'array query parameters are parsed (implicit indexes)' );
283 assert
.deepEqual( uri1
.getQueryString(), expectedS
,
284 'array query parameters are encoded (always with explicit indexes)' );
285 assert
.deepEqual( uri2
.query
, expectedQ
,
286 'array query parameters are parsed (explicit indexes)' );
287 assert
.deepEqual( uri2
.getQueryString(), expectedS
,
288 'array query parameters are encoded (always with explicit indexes)' );
289 assert
.deepEqual( uri3
.query
, expectedQ
,
290 'array query parameters are parsed (mixed indexes, out of order)' );
291 assert
.deepEqual( uri3
.getQueryString(), expectedS
,
292 'array query parameters are encoded (always with explicit indexes)' );
294 uriMissing
= new mw
.Uri( 'http://example.com/?foo[0]=a&foo[2]=c', { arrayParams
: true } );
295 // eslint-disable-next-line no-sparse-arrays
296 expectedMissingQ
= { foo
: [ 'a', , 'c' ] };
297 expectedMissingS
= 'foo%5B0%5D=a&foo%5B2%5D=c';
299 assert
.deepEqual( uriMissing
.query
, expectedMissingQ
,
300 'array query parameters are parsed (missing array item)' );
301 assert
.deepEqual( uriMissing
.getQueryString(), expectedMissingS
,
302 'array query parameters are encoded (missing array item)' );
304 uriWeird
= new mw
.Uri( 'http://example.com/?foo[0]=a&foo[1][1]=b&foo[x]=c', { arrayParams
: true } );
305 expectedWeirdQ
= { foo
: [ 'a' ], 'foo[1][1]': 'b', 'foo[x]': 'c' };
306 expectedWeirdS
= 'foo%5B0%5D=a&foo%5B1%5D%5B1%5D=b&foo%5Bx%5D=c';
308 assert
.deepEqual( uriWeird
.query
, expectedWeirdQ
,
309 'array query parameters are parsed (multi-dimensional or associative arrays are ignored)' );
310 assert
.deepEqual( uriWeird
.getQueryString(), expectedWeirdS
,
311 'array query parameters are encoded (multi-dimensional or associative arrays are ignored)' );
314 QUnit
.test( '.clone()', function ( assert
) {
317 original
= new mw
.Uri( 'http://foo.example.org/index.php?one=1&two=2' );
318 clone
= original
.clone();
320 assert
.deepEqual( clone
, original
, 'clone has equivalent properties' );
321 assert
.strictEqual( original
.toString(), clone
.toString(), 'toString matches original' );
323 assert
.notStrictEqual( clone
, original
, 'clone is a different object when compared by reference' );
325 clone
.host
= 'bar.example.org';
326 assert
.notEqual( original
.host
, clone
.host
, 'manipulating clone did not effect original' );
327 assert
.notEqual( original
.toString(), clone
.toString(), 'Stringified url no longer matches original' );
329 clone
.query
.three
= 3;
333 { one
: '1', two
: '2' },
334 'Properties is deep cloned (T39708)'
338 QUnit
.test( '.toString() after query manipulation', function ( assert
) {
341 uri
= new mw
.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
345 uri
.query
.n
= [ 'x', 'y', 'z' ];
347 // Verify parts and total length instead of entire string because order
348 // of iteration can vary.
349 assert
.strictEqual( uri
.toString().indexOf( 'm=bar' ) !== -1, true, 'toString preserves other values' );
350 assert
.strictEqual( uri
.toString().indexOf( 'n=x&n=y&n=z' ) !== -1, true, 'toString parameter includes all values of an array query parameter' );
351 assert
.strictEqual( uri
.toString().length
, 'http://www.example.com/dir/?m=bar&n=x&n=y&n=z'.length
, 'toString matches expected string' );
353 uri
= new mw
.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
357 // Change query values
358 uri
.query
.n
= [ 'x', 'y', 'z' ];
360 // Verify parts and total length instead of entire string because order
361 // of iteration can vary.
362 assert
.strictEqual( uri
.toString().indexOf( 'm=foo&m=bar' ) !== -1, true, 'toString preserves other values' );
363 assert
.strictEqual( uri
.toString().indexOf( 'n=x&n=y&n=z' ) !== -1, true, 'toString parameter includes all values of an array query parameter' );
364 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' );
366 // Remove query values
367 uri
.query
.m
.splice( 0, 1 );
370 assert
.strictEqual( uri
.toString(), 'http://www.example.com/dir/?m=bar', 'deletion properties' );
372 // Remove more query values, leaving an empty array
373 uri
.query
.m
.splice( 0, 1 );
374 assert
.strictEqual( uri
.toString(), 'http://www.example.com/dir/', 'empty array value is ommitted' );
377 QUnit
.test( 'Variable defaultUri', function ( assert
) {
379 href
= 'http://example.org/w/index.php#here',
380 UriClass
= mw
.UriRelative( function () {
384 uri
= new UriClass();
387 protocol
: uri
.protocol
,
389 password
: uri
.password
,
394 fragment
: uri
.fragment
402 path
: '/w/index.php',
406 'basic object properties'
409 // Default URI may change, e.g. via history.replaceState, pushState or location.hash (T74334)
410 href
= 'https://example.com/wiki/Foo?v=2';
411 uri
= new UriClass();
414 protocol
: uri
.protocol
,
416 password
: uri
.password
,
421 fragment
: uri
.fragment
433 'basic object properties'
437 QUnit
.test( 'Advanced URL', function ( assert
) {
438 var uri
, queryString
, relativePath
;
440 uri
= new mw
.Uri( 'http://auth@www.example.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value+%28escaped%29#caf%C3%A9' );
444 protocol
: uri
.protocol
,
446 password
: uri
.password
,
451 fragment
: uri
.fragment
457 host
: 'www.example.com',
459 path
: '/dir/dir.2/index.htm',
460 query
: { q1
: '0', test1
: null, test2
: 'value (escaped)' },
463 'basic object properties'
466 assert
.strictEqual( uri
.getUserInfo(), 'auth', 'user info' );
468 assert
.strictEqual( uri
.getAuthority(), 'auth@www.example.com:81', 'authority equal to auth@hostport' );
470 assert
.strictEqual( uri
.getHostPort(), 'www.example.com:81', 'hostport equal to host:port' );
472 queryString
= uri
.getQueryString();
473 assert
.strictEqual( queryString
.indexOf( 'q1=0' ) !== -1, true, 'query param with numbers' );
474 assert
.strictEqual( queryString
.indexOf( 'test1' ) !== -1, true, 'query param with null value is included' );
475 assert
.strictEqual( queryString
.indexOf( 'test1=' ) !== -1, false, 'query param with null value does not generate equals sign' );
476 assert
.strictEqual( queryString
.indexOf( 'test2=value+%28escaped%29' ) !== -1, true, 'query param is url escaped' );
478 relativePath
= uri
.getRelativePath();
479 assert
.ok( relativePath
.indexOf( uri
.path
) >= 0, 'path in relative path' );
480 assert
.ok( relativePath
.indexOf( uri
.getQueryString() ) >= 0, 'query string in relative path' );
481 assert
.ok( relativePath
.indexOf( mw
.Uri
.encode( uri
.fragment
) ) >= 0, 'escaped fragment in relative path' );
484 QUnit
.test( 'Parse a uri with an @ symbol in the path and query', function ( assert
) {
485 var uri
= new mw
.Uri( 'http://www.example.com/test@test?x=@uri&y@=uri&z@=@' );
489 protocol
: uri
.protocol
,
491 password
: uri
.password
,
496 fragment
: uri
.fragment
,
497 queryString
: uri
.getQueryString()
503 host
: 'www.example.com',
506 query
: { x
: '@uri', 'y@': 'uri', 'z@': '@' },
508 queryString
: 'x=%40uri&y%40=uri&z%40=%40'
510 'basic object properties'
514 QUnit
.test( 'Handle protocol-relative URLs', function ( assert
) {
517 UriRel
= mw
.UriRelative( 'glork://en.wiki.local/foo.php' );
519 uri
= new UriRel( '//en.wiki.local/w/api.php' );
520 assert
.strictEqual( uri
.protocol
, 'glork', 'create protocol-relative URLs with same protocol as document' );
522 uri
= new UriRel( '/foo.com' );
523 assert
.strictEqual( uri
.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in loose mode' );
525 uri
= new UriRel( 'http:/foo.com' );
526 assert
.strictEqual( uri
.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in loose mode' );
528 uri
= new UriRel( '/foo.com', true );
529 assert
.strictEqual( uri
.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in strict mode' );
531 uri
= new UriRel( 'http:/foo.com', true );
532 assert
.strictEqual( uri
.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in strict mode' );
535 QUnit
.test( 'T37658', function ( assert
) {
536 var testProtocol
, testServer
, testPort
, testPath
, UriClass
, uri
, href
;
538 testProtocol
= 'https://';
539 testServer
= 'foo.example.org';
543 UriClass
= mw
.UriRelative( testProtocol
+ testServer
+ '/some/path/index.html' );
544 uri
= new UriClass( testPath
);
545 href
= uri
.toString();
546 assert
.strictEqual( href
, testProtocol
+ testServer
+ testPath
, 'Root-relative URL gets host & protocol supplied' );
548 UriClass
= mw
.UriRelative( testProtocol
+ testServer
+ ':' + testPort
+ '/some/path.php' );
549 uri
= new UriClass( testPath
);
550 href
= uri
.toString();
551 assert
.strictEqual( href
, testProtocol
+ testServer
+ ':' + testPort
+ testPath
, 'Root-relative URL gets host, protocol, and port supplied' );