mediawiki.api: Add parameter ajaxOptions to postWithToken()
authorFomafix <fomafix@googlemail.com>
Thu, 15 May 2014 11:39:03 +0000 (11:39 +0000)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 15 May 2014 17:32:55 +0000 (19:32 +0200)
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
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js

index 6444d93..af33165 100644 (file)
                 *
                 * @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
                                                        // Try again, once
                                                        return api.getToken( tokenType ).then( function ( token ) {
                                                                params.token = token;
-                                                               return api.post( params );
+                                                               return api.post( params, ajaxOptions );
                                                        } );
                                                }
 
index 05eb6b9..2cca7c8 100644 (file)
                } );
        } );
 
-       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' } } );
                );
        } );
 
+       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 );