From: Fomafix Date: Tue, 13 May 2014 18:44:36 +0000 (+0000) Subject: mediawiki.api: Do not cache errors in getToken() X-Git-Tag: 1.31.0-rc.0~15661^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/journal.php?a=commitdiff_plain;h=5b915d721c75695c776af71f74687f12e48a2290;p=lhc%2Fweb%2Fwiklou.git mediawiki.api: Do not cache errors in getToken() Bug: 65268 Change-Id: I8d0e509b735dcf6a55ded29f0eb272bc1fdc2bc7 --- diff --git a/resources/src/mediawiki.api/mediawiki.api.js b/resources/src/mediawiki.api/mediawiki.api.js index 6444d93f2a..5f0b004550 100644 --- a/resources/src/mediawiki.api/mediawiki.api.js +++ b/resources/src/mediawiki.api/mediawiki.api.js @@ -303,7 +303,11 @@ d.reject( 'token-missing', data ); } } ) - .fail( d.reject ); + .fail( function ( code, result ) { + // Delete promise. Do not cache errors. + delete deferredGroup[ type + 'Token' ]; + d.reject( code, result ); + } ); // Attach abort handler d.abort = apiPromise.abort; 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..83f5dd5663 100644 --- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js @@ -99,7 +99,7 @@ this.server.respond(); } ); - QUnit.test( 'getToken( cached )', function ( assert ) { + QUnit.test( 'getToken( pre-populated )', function ( assert ) { QUnit.expect( 2 ); var api = new mw.Api(); @@ -117,29 +117,62 @@ assert.equal( this.server.requests.length, 0, 'Requests made' ); } ); - QUnit.test( 'getToken( uncached )', function ( assert ) { - QUnit.expect( 2 ); + QUnit.test( 'getToken()', function ( assert ) { + QUnit.expect( 5 ); - var api = new mw.Api(); + var test = this, + api = new mw.Api(); // Get a token of a type that isn't prepopulated by user.tokens. // Could use "block" or "delete" here, but those could in theory // be added to user.tokens, use a fake one instead. api.getToken( 'testaction' ) .done( function ( token ) { - assert.ok( token.length, 'Got a token' ); + assert.ok( token.length, 'Got testaction token' ); } ) .fail( function ( err ) { - assert.equal( '', err, 'API error' ); + assert.equal( err, '', 'API error' ); + } ); + api.getToken( 'testaction' ) + .done( function ( token ) { + assert.ok( token.length, 'Got testaction token (cached)' ); + } ) + .fail( function ( err ) { + assert.equal( err, '', 'API error' ); } ); - assert.equal( this.server.requests.length, 1, 'Requests made' ); + // Don't cache error (bug 65268) + api.getToken( 'testaction2' ) + .fail( function ( err ) { + assert.equal( err, 'bite-me', 'Expected error' ); + } ) + .always( function () { + // Make this request after the first one has finished. + // If we make it simultaneously we still want it to share + // the cache, but as soon as it is fulfilled as error we + // reject it so that the next one tries fresh. + api.getToken( 'testaction2' ) + .done( function ( token ) { + assert.ok( token.length, 'Got testaction2 token (error was not be cached)' ); + } ) + .fail( function ( err ) { + assert.equal( err, '', 'API error' ); + } ); + + assert.equal( test.server.requests.length, 3, 'Requests made' ); + + test.server.requests[2].respond( 200, { 'Content-Type': 'application/json' }, + '{ "tokens": { "testaction2token": "0123abc" } }' + ); + } ); - this.server.respond( function ( request ) { - request.respond( 200, { 'Content-Type': 'application/json' }, - '{ "tokens": { "testactiontoken": "0123abc" } }' - ); - } ); + this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' }, + '{ "tokens": { "testactiontoken": "0123abc" } }' + ); + + this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' }, + '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }' + ); } ); QUnit.test( 'postWithToken()', function ( assert ) {