X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fqunit%2Fsuites%2Fresources%2Fmediawiki%2Fmediawiki.Uri.test.js;h=f2cb70b8af57f66ad23bc2c2d0f102f420227317;hb=b4675c6125736c11b5a4c14c25a2bfaf41102264;hp=918c923a7b559a87744e07cbf464131bc7001659;hpb=ecaed93c99a21cfdb46bf7e0cb4352a5492bdc70;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js index 918c923a7b..9230ab734f 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js @@ -1,4 +1,4 @@ -( function ( mw ) { +( function () { QUnit.module( 'mediawiki.Uri', QUnit.newMwEnvironment( { setup: function () { this.mwUriOrg = mw.Uri; @@ -105,6 +105,18 @@ 'Parse an ftp URI correctly with user and password' ); + uri = new mw.Uri( 'http://example.com/?foo[1]=b&foo[0]=a&foo[]=c' ); + + assert.deepEqual( + uri.query, + { + 'foo[1]': 'b', + 'foo[0]': 'a', + 'foo[]': 'c' + }, + 'Array query parameters parsed as normal with arrayParams:false' + ); + assert.throws( function () { return new mw.Uri( 'glaswegian penguins' ); @@ -130,7 +142,7 @@ uri = new mw.Uri( 'example.com/bar/baz', { strictMode: false } ); - assert.equal( uri.toString(), 'http://example.com/bar/baz', 'normalize URI without protocol or // in loose mode' ); + assert.strictEqual( uri.toString(), 'http://example.com/bar/baz', 'normalize URI without protocol or // in loose mode' ); uri = new mw.Uri( 'http://example.com/index.php?key=key&hasOwnProperty=hasOwnProperty&constructor=constructor&watch=watch' ); assert.deepEqual( @@ -151,7 +163,7 @@ host: 'www.foo.local', path: '/this' } ); - assert.equal( uri.toString(), 'http://www.foo.local/this', 'Basic properties' ); + assert.strictEqual( uri.toString(), 'http://www.foo.local/this', 'Basic properties' ); uri = new mw.Uri( { protocol: 'http', @@ -160,7 +172,7 @@ query: { hi: 'there' }, fragment: 'blah' } ); - assert.equal( uri.toString(), 'http://www.foo.local/this?hi=there#blah', 'More complex properties' ); + assert.strictEqual( uri.toString(), 'http://www.foo.local/this?hi=there#blah', 'More complex properties' ); assert.throws( function () { @@ -183,16 +195,16 @@ MyUri = mw.UriRelative( testuri ); uri = new MyUri(); - assert.equal( uri.toString(), testuri, 'no arguments' ); + assert.strictEqual( uri.toString(), testuri, 'no arguments' ); uri = new MyUri( undefined ); - assert.equal( uri.toString(), testuri, 'undefined' ); + assert.strictEqual( uri.toString(), testuri, 'undefined' ); uri = new MyUri( null ); - assert.equal( uri.toString(), testuri, 'null' ); + assert.strictEqual( uri.toString(), testuri, 'null' ); uri = new MyUri( '' ); - assert.equal( uri.toString(), testuri, 'empty string' ); + assert.strictEqual( uri.toString(), testuri, 'empty string' ); uri = new MyUri( null, { overrideKeys: true } ); assert.deepEqual( uri.query, { a: '2' }, 'null, with options' ); @@ -205,32 +217,32 @@ uri = uriBase.clone(); uri.fragment = 'frag'; - assert.equal( uri.toString(), 'http://en.wiki.local/w/api.php#frag', 'add a fragment' ); + assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php#frag', 'add a fragment' ); uri.fragment = 'café'; - assert.equal( uri.toString(), 'http://en.wiki.local/w/api.php#caf%C3%A9', 'fragment is url-encoded' ); + assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php#caf%C3%A9', 'fragment is url-encoded' ); uri = uriBase.clone(); uri.host = 'fr.wiki.local'; uri.port = '8080'; - assert.equal( uri.toString(), 'http://fr.wiki.local:8080/w/api.php', 'change host and port' ); + assert.strictEqual( uri.toString(), 'http://fr.wiki.local:8080/w/api.php', 'change host and port' ); uri = uriBase.clone(); uri.query.foo = 'bar'; - assert.equal( uri.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'add query arguments' ); + assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'add query arguments' ); delete uri.query.foo; - assert.equal( uri.toString(), 'http://en.wiki.local/w/api.php', 'delete query arguments' ); + assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php', 'delete query arguments' ); uri = uriBase.clone(); uri.query.foo = 'bar'; - assert.equal( uri.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'extend query arguments' ); + assert.strictEqual( uri.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'extend query arguments' ); uri.extend( { foo: 'quux', pif: 'paf' } ); - assert.ok( uri.toString().indexOf( 'foo=quux' ) >= 0, 'extend query arguments' ); - assert.ok( uri.toString().indexOf( 'foo=bar' ) === -1, 'extend query arguments' ); - assert.ok( uri.toString().indexOf( 'pif=paf' ) >= 0, 'extend query arguments' ); + assert.strictEqual( uri.toString().indexOf( 'foo=quux' ) !== -1, true, 'extend query arguments' ); + assert.strictEqual( uri.toString().indexOf( 'foo=bar' ) !== -1, false, 'extend query arguments' ); + assert.strictEqual( uri.toString().indexOf( 'pif=paf' ) !== -1, true, 'extend query arguments' ); } ); QUnit.test( '.getQueryString()', function ( assert ) { @@ -259,7 +271,7 @@ ); uri = new mw.Uri( 'https://example.com/mw/index.php?title=Sandbox/7&other=Sandbox/7&foo' ); - assert.equal( + assert.strictEqual( uri.getQueryString(), 'title=Sandbox/7&other=Sandbox%2F7&foo', 'title parameter is escaped the wiki-way' @@ -267,6 +279,50 @@ } ); + QUnit.test( 'arrayParams', function ( assert ) { + var uri1, uri2, uri3, expectedQ, expectedS, + uriMissing, expectedMissingQ, expectedMissingS, + uriWeird, expectedWeirdQ, expectedWeirdS; + + uri1 = new mw.Uri( 'http://example.com/?foo[]=a&foo[]=b&foo[]=c', { arrayParams: true } ); + uri2 = new mw.Uri( 'http://example.com/?foo[0]=a&foo[1]=b&foo[2]=c', { arrayParams: true } ); + uri3 = new mw.Uri( 'http://example.com/?foo[1]=b&foo[0]=a&foo[]=c', { arrayParams: true } ); + expectedQ = { foo: [ 'a', 'b', 'c' ] }; + expectedS = 'foo%5B0%5D=a&foo%5B1%5D=b&foo%5B2%5D=c'; + + assert.deepEqual( uri1.query, expectedQ, + 'array query parameters are parsed (implicit indexes)' ); + assert.deepEqual( uri1.getQueryString(), expectedS, + 'array query parameters are encoded (always with explicit indexes)' ); + assert.deepEqual( uri2.query, expectedQ, + 'array query parameters are parsed (explicit indexes)' ); + assert.deepEqual( uri2.getQueryString(), expectedS, + 'array query parameters are encoded (always with explicit indexes)' ); + assert.deepEqual( uri3.query, expectedQ, + 'array query parameters are parsed (mixed indexes, out of order)' ); + assert.deepEqual( uri3.getQueryString(), expectedS, + 'array query parameters are encoded (always with explicit indexes)' ); + + uriMissing = new mw.Uri( 'http://example.com/?foo[0]=a&foo[2]=c', { arrayParams: true } ); + // eslint-disable-next-line no-sparse-arrays + expectedMissingQ = { foo: [ 'a', , 'c' ] }; + expectedMissingS = 'foo%5B0%5D=a&foo%5B2%5D=c'; + + assert.deepEqual( uriMissing.query, expectedMissingQ, + 'array query parameters are parsed (missing array item)' ); + assert.deepEqual( uriMissing.getQueryString(), expectedMissingS, + 'array query parameters are encoded (missing array item)' ); + + uriWeird = new mw.Uri( 'http://example.com/?foo[0]=a&foo[1][1]=b&foo[x]=c', { arrayParams: true } ); + expectedWeirdQ = { foo: [ 'a' ], 'foo[1][1]': 'b', 'foo[x]': 'c' }; + expectedWeirdS = 'foo%5B0%5D=a&foo%5B1%5D%5B1%5D=b&foo%5Bx%5D=c'; + + assert.deepEqual( uriWeird.query, expectedWeirdQ, + 'array query parameters are parsed (multi-dimensional or associative arrays are ignored)' ); + assert.deepEqual( uriWeird.getQueryString(), expectedWeirdS, + 'array query parameters are encoded (multi-dimensional or associative arrays are ignored)' ); + } ); + QUnit.test( '.clone()', function ( assert ) { var original, clone; @@ -274,7 +330,7 @@ clone = original.clone(); assert.deepEqual( clone, original, 'clone has equivalent properties' ); - assert.equal( original.toString(), clone.toString(), 'toString matches original' ); + assert.strictEqual( original.toString(), clone.toString(), 'toString matches original' ); assert.notStrictEqual( clone, original, 'clone is a different object when compared by reference' ); @@ -302,9 +358,9 @@ // Verify parts and total length instead of entire string because order // of iteration can vary. - assert.ok( uri.toString().indexOf( 'm=bar' ), 'toString preserves other values' ); - assert.ok( uri.toString().indexOf( 'n=x&n=y&n=z' ), 'toString parameter includes all values of an array query parameter' ); - assert.equal( uri.toString().length, 'http://www.example.com/dir/?m=bar&n=x&n=y&n=z'.length, 'toString matches expected string' ); + assert.strictEqual( uri.toString().indexOf( 'm=bar' ) !== -1, true, 'toString preserves other values' ); + assert.strictEqual( uri.toString().indexOf( 'n=x&n=y&n=z' ) !== -1, true, 'toString parameter includes all values of an array query parameter' ); + assert.strictEqual( uri.toString().length, 'http://www.example.com/dir/?m=bar&n=x&n=y&n=z'.length, 'toString matches expected string' ); uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', { overrideKeys: false @@ -315,19 +371,19 @@ // Verify parts and total length instead of entire string because order // of iteration can vary. - assert.ok( uri.toString().indexOf( 'm=foo&m=bar' ) >= 0, 'toString preserves other values' ); - assert.ok( uri.toString().indexOf( 'n=x&n=y&n=z' ) >= 0, 'toString parameter includes all values of an array query parameter' ); - assert.equal( uri.toString().length, 'http://www.example.com/dir/?m=foo&m=bar&n=x&n=y&n=z'.length, 'toString matches expected string' ); + assert.strictEqual( uri.toString().indexOf( 'm=foo&m=bar' ) !== -1, true, 'toString preserves other values' ); + assert.strictEqual( uri.toString().indexOf( 'n=x&n=y&n=z' ) !== -1, true, 'toString parameter includes all values of an array query parameter' ); + 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' ); // Remove query values uri.query.m.splice( 0, 1 ); delete uri.query.n; - assert.equal( uri.toString(), 'http://www.example.com/dir/?m=bar', 'deletion properties' ); + assert.strictEqual( uri.toString(), 'http://www.example.com/dir/?m=bar', 'deletion properties' ); // Remove more query values, leaving an empty array uri.query.m.splice( 0, 1 ); - assert.equal( uri.toString(), 'http://www.example.com/dir/', 'empty array value is ommitted' ); + assert.strictEqual( uri.toString(), 'http://www.example.com/dir/', 'empty array value is ommitted' ); } ); QUnit.test( 'Variable defaultUri', function ( assert ) { @@ -419,17 +475,17 @@ 'basic object properties' ); - assert.equal( uri.getUserInfo(), 'auth', 'user info' ); + assert.strictEqual( uri.getUserInfo(), 'auth', 'user info' ); - assert.equal( uri.getAuthority(), 'auth@www.example.com:81', 'authority equal to auth@hostport' ); + assert.strictEqual( uri.getAuthority(), 'auth@www.example.com:81', 'authority equal to auth@hostport' ); - assert.equal( uri.getHostPort(), 'www.example.com:81', 'hostport equal to host:port' ); + assert.strictEqual( uri.getHostPort(), 'www.example.com:81', 'hostport equal to host:port' ); queryString = uri.getQueryString(); - assert.ok( queryString.indexOf( 'q1=0' ) >= 0, 'query param with numbers' ); - assert.ok( queryString.indexOf( 'test1' ) >= 0, 'query param with null value is included' ); - assert.ok( queryString.indexOf( 'test1=' ) === -1, 'query param with null value does not generate equals sign' ); - assert.ok( queryString.indexOf( 'test2=value+%28escaped%29' ) >= 0, 'query param is url escaped' ); + assert.strictEqual( queryString.indexOf( 'q1=0' ) !== -1, true, 'query param with numbers' ); + assert.strictEqual( queryString.indexOf( 'test1' ) !== -1, true, 'query param with null value is included' ); + assert.strictEqual( queryString.indexOf( 'test1=' ) !== -1, false, 'query param with null value does not generate equals sign' ); + assert.strictEqual( queryString.indexOf( 'test2=value+%28escaped%29' ) !== -1, true, 'query param is url escaped' ); relativePath = uri.getRelativePath(); assert.ok( relativePath.indexOf( uri.path ) >= 0, 'path in relative path' ); @@ -473,19 +529,19 @@ UriRel = mw.UriRelative( 'glork://en.wiki.local/foo.php' ); uri = new UriRel( '//en.wiki.local/w/api.php' ); - assert.equal( uri.protocol, 'glork', 'create protocol-relative URLs with same protocol as document' ); + assert.strictEqual( uri.protocol, 'glork', 'create protocol-relative URLs with same protocol as document' ); uri = new UriRel( '/foo.com' ); - assert.equal( uri.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in loose mode' ); + assert.strictEqual( uri.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in loose mode' ); uri = new UriRel( 'http:/foo.com' ); - assert.equal( uri.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in loose mode' ); + assert.strictEqual( uri.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in loose mode' ); uri = new UriRel( '/foo.com', true ); - assert.equal( uri.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in strict mode' ); + assert.strictEqual( uri.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in strict mode' ); uri = new UriRel( 'http:/foo.com', true ); - assert.equal( uri.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in strict mode' ); + assert.strictEqual( uri.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in strict mode' ); } ); QUnit.test( 'T37658', function ( assert ) { @@ -499,11 +555,11 @@ UriClass = mw.UriRelative( testProtocol + testServer + '/some/path/index.html' ); uri = new UriClass( testPath ); href = uri.toString(); - assert.equal( href, testProtocol + testServer + testPath, 'Root-relative URL gets host & protocol supplied' ); + assert.strictEqual( href, testProtocol + testServer + testPath, 'Root-relative URL gets host & protocol supplied' ); UriClass = mw.UriRelative( testProtocol + testServer + ':' + testPort + '/some/path.php' ); uri = new UriClass( testPath ); href = uri.toString(); - assert.equal( href, testProtocol + testServer + ':' + testPort + testPath, 'Root-relative URL gets host, protocol, and port supplied' ); + assert.strictEqual( href, testProtocol + testServer + ':' + testPort + testPath, 'Root-relative URL gets host, protocol, and port supplied' ); } ); -}( mediaWiki ) ); +}() );