From: Timo Tijhof Date: Tue, 31 Jan 2017 00:42:23 +0000 (+0000) Subject: mediawiki.api: Add unit tests for pipe-joining non-string values X-Git-Tag: 1.31.0-rc.0~4114^2 X-Git-Url: http://git.cyclocoop.org/data/File:Denys_Savchenko_%27%27Pentecoste%27%27.jpg?a=commitdiff_plain;h=1a3d0d82b4809b73978f5801dc74b0dfa86bc1d7;p=lhc%2Fweb%2Fwiklou.git mediawiki.api: Add unit tests for pipe-joining non-string values Also document the fact that null/undefined in array values are submitted to the API as empty string. We may want to change this. Change-Id: I099b055ba8ddd367b6df2dd8f2997d8c6cd243df --- diff --git a/resources/src/mediawiki/api.js b/resources/src/mediawiki/api.js index 5abd2d4fa9..f3d48e691a 100644 --- a/resources/src/mediawiki/api.js +++ b/resources/src/mediawiki/api.js @@ -150,6 +150,9 @@ /** * Massage parameters from the nice format we accept into a format suitable for the API. * + * NOTE: A value of undefined/null in an array will be represented by Array#join() + * as the empty string. Should we filter silently? Warn? Leave as-is? + * * @private * @param {Object} parameters (modified in-place) * @param {boolean} useUS Whether to use U+001F when joining multi-valued parameters. diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js index 6a00ac9b17..6fcdbb33b5 100644 --- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js @@ -89,7 +89,7 @@ return api.post( { action: 'test' }, { contentType: 'multipart/form-data' } ); } ); - QUnit.test( 'Converting arrays to pipe-separated', function ( assert ) { + QUnit.test( 'Converting arrays to pipe-separated (string)', function ( assert ) { var api = new mw.Api(); this.server.respond( function ( request ) { @@ -100,6 +100,29 @@ return api.get( { test: [ 'foo', 'bar', 'baz' ] } ); } ); + QUnit.test( 'Converting arrays to pipe-separated (mw.Title)', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( function ( request ) { + assert.ok( request.url.match( /test=Foo%7CBar/ ), 'Pipe-separated value was submitted' ); + request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); + } ); + + return api.get( { test: [ new mw.Title( 'Foo' ), new mw.Title( 'Bar' ) ] } ); + } ); + + QUnit.test( 'Converting arrays to pipe-separated (misc primitives)', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( function ( request ) { + assert.ok( request.url.match( /test=true%7Cfalse%7C%7C%7C0%7C1%2E2/ ), 'Pipe-separated value was submitted: ' + request.url ); + request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); + } ); + + // undefined/null will become empty string + return api.get( { test: [ true, false, undefined, null, 0, 1.2 ] } ); + } ); + QUnit.test( 'Omitting false booleans', function ( assert ) { var api = new mw.Api();