*
* @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 );
} );
}
} );
} );
- 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 );