* @return {jQuery.Promise} Received token.
*/
getToken: function ( type, assert ) {
- var apiPromise, promiseGroup, d;
+ var apiPromise, promiseGroup, d, reject;
type = mapLegacyToken( type );
promiseGroup = promises[ this.defaults.ajax.url ];
d = promiseGroup && promiseGroup[ type + 'Token' ];
type: type,
assert: assert
} );
+ reject = function () {
+ // Clear promise. Do not cache errors.
+ delete promiseGroup[ type + 'Token' ];
+
+ // Let caller handle the error code
+ return $.Deferred().rejectWith( this, arguments );
+ };
d = apiPromise
.then( function ( res ) {
+ if ( !res.query ) {
+ return reject( 'query-missing', res );
+ }
// If token type is unknown, it is omitted from the response
if ( !res.query.tokens[ type + 'token' ] ) {
return $.Deferred().reject( 'token-missing', res );
}
-
return res.query.tokens[ type + 'token' ];
- }, function () {
- // Clear promise. Do not cache errors.
- delete promiseGroup[ type + 'Token' ];
-
- // Let caller handle the error code
- return $.Deferred().rejectWith( this, arguments );
- } )
+ }, reject )
// Attach abort handler
.promise( { abort: apiPromise.abort } );
} );
} );
+ QUnit.test( 'getToken() - no query', function ( assert ) {
+ var api = new mw.Api(),
+ // Same-origin warning and missing query in response.
+ serverRsp = {
+ warnings: {
+ tokens: {
+ '*': 'Tokens may not be obtained when the same-origin policy is not applied.'
+ }
+ }
+ };
+
+ this.server.respondWith( /type=testnoquery/, [ 200, { 'Content-Type': 'application/json' },
+ JSON.stringify( serverRsp )
+ ] );
+
+ return api.getToken( 'testnoquery' )
+ .then( function () { assert.fail( 'Expected response missing a query to be rejected' ); } )
+ .catch( function ( err, rsp ) {
+ assert.equal( err, 'query-missing', 'Expected no query error code' );
+ assert.deepEqual( rsp, serverRsp );
+ } );
+ } );
+
QUnit.test( 'getToken() - deprecated', function ( assert ) {
// Cache API endpoint from default to avoid cachehit in mw.user.tokens
var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ),