From 597142b2e5a0c44b4efd369d1d339e3813aeefdf Mon Sep 17 00:00:00 2001 From: Fomafix Date: Thu, 15 May 2014 11:39:03 +0000 Subject: [PATCH] mediawiki.api: Add parameter ajaxOptions to postWithToken() This allows to upload a file with token. var api = new mw.Api(); api.postWithToken( 'edit', { action: 'upload', format: 'json', filename: 'Foobar.png', file: file }, { contentType: 'multipart/form-data' } ); Change-Id: I7a0fec3c1ef2c7881d4c04b1a9dd44fac02cb346 --- resources/src/mediawiki.api/mediawiki.api.js | 13 ++++-- .../mediawiki.api/mediawiki.api.test.js | 40 ++++++++++++++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/resources/src/mediawiki.api/mediawiki.api.js b/resources/src/mediawiki.api/mediawiki.api.js index 6444d93f2a..af33165422 100644 --- a/resources/src/mediawiki.api/mediawiki.api.js +++ b/resources/src/mediawiki.api/mediawiki.api.js @@ -244,15 +244,22 @@ * * @param {string} tokenType The name of the token, like options or edit. * @param {Object} params API parameters + * @param {Object} [ajaxOptions] * @return {jQuery.Promise} See #post * @since 1.22 */ - postWithToken: function ( tokenType, params ) { + postWithToken: function ( tokenType, params, ajaxOptions ) { var api = this; + // Do not allow deprecated ok-callback + // FIXME: Remove this check when the deprecated ok-callback is removed in #post + if ( $.isFunction( ajaxOptions ) ) { + ajaxOptions = undefined; + } + return api.getToken( tokenType ).then( function ( token ) { params.token = token; - return api.post( params ).then( + return api.post( params, ajaxOptions ).then( // If no error, return to caller as-is null, // Error handler @@ -265,7 +272,7 @@ // Try again, once return api.getToken( tokenType ).then( function ( token ) { params.token = token; - return api.post( params ); + return api.post( params, ajaxOptions ); } ); } 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 05eb6b988e..2cca7c8a68 100644 --- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js @@ -142,7 +142,7 @@ } ); } ); - QUnit.test( 'postWithToken()', function ( assert ) { + QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) { QUnit.expect( 1 ); var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ); @@ -163,6 +163,44 @@ ); } ); + QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) { + QUnit.expect( 3 ); + + var api = new mw.Api(); + + api.postWithToken( + 'edit', + { + action: 'example' + }, + { + headers: { + 'X-Foo': 'Bar' + } + } + ); + + api.postWithToken( + 'edit', + { + action: 'example' + }, + function () { + assert.ok( false, 'This parameter cannot be a callback' ); + } + ) + .always( function ( data ) { + assert.equal( data.example, 'quux' ); + } ); + + assert.equal( this.server.requests.length, 2, 'Request made' ); + assert.equal( this.server.requests[0].requestHeaders['X-Foo'], 'Bar', 'Header sent' ); + + this.server.respond( function ( request ) { + request.respond( 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ); + } ); + } ); + QUnit.test( 'postWithToken() - badtoken', function ( assert ) { QUnit.expect( 1 ); -- 2.20.1