ajaxOptions = undefined;
}
- return api.getToken( tokenType ).then( function ( token ) {
+ return api.getToken( tokenType, params.assert ).then( function ( token ) {
params.token = token;
return api.post( params, ajaxOptions ).then(
// If no error, return to caller as-is
params.token = undefined;
// Try again, once
- return api.getToken( tokenType ).then( function ( token ) {
+ return api.getToken( tokenType, params.assert ).then( function ( token ) {
params.token = token;
return api.post( params, ajaxOptions );
} );
/**
* Get a token for a certain action from the API.
*
+ * The assert parameter is only for internal use by postWithToken.
+ *
* @param {string} type Token type
* @return {jQuery.Promise}
* @return {Function} return.done
* @return {string} return.done.token Received token.
* @since 1.22
*/
- getToken: function ( type ) {
+ getToken: function ( type, assert ) {
var apiPromise,
promiseGroup = promises[ this.defaults.ajax.url ],
d = promiseGroup && promiseGroup[ type + 'Token' ];
if ( !d ) {
- apiPromise = this.get( { action: 'tokens', type: type } );
+ apiPromise = this.get( { action: 'tokens', type: type, assert: assert } );
d = apiPromise
.then( function ( data ) {
);
} );
+ QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
+ QUnit.expect( 2 );
+
+ var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
+
+ api.postWithToken( 'testasserttoken', { action: 'example', key: 'foo', assert: 'user' } )
+ .fail( function ( errorCode ) {
+ assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
+ } );
+
+ assert.equal( this.server.requests.length, 1, 'Request for token made' );
+ this.server.respondWith( /assert=user/, function ( request ) {
+ request.respond(
+ 200,
+ { 'Content-Type': 'application/json' },
+ '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
+ );
+ } );
+
+ this.server.respond();
+ } );
+
QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
QUnit.expect( 3 );