2 QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
4 this.server = this.sandbox.useFakeServer();
5 this.server.respondImmediately = true;
6 this.clock = this.sandbox.useFakeTimers();
8 teardown: function () {
9 // https://github.com/jquery/jquery/issues/2453
14 function sequence( responses ) {
16 return function ( request ) {
17 var response = responses[ i ];
20 request.respond.apply( request, response );
25 function sequenceBodies( status, headers, bodies ) {
26 jQuery.each( bodies, function ( i, body ) {
27 bodies[ i ] = [ status, headers, body ];
29 return sequence( bodies );
32 QUnit.test( 'Basic functionality', function ( assert ) {
34 var api = new mw.Api();
36 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
39 .done( function ( data ) {
40 assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
44 .done( function ( data ) {
45 assert.deepEqual( data, [], 'Simple POST request' );
49 QUnit.test( 'API error', function ( assert ) {
51 var api = new mw.Api();
53 this.server.respond( [ 200, { 'Content-Type': 'application/json' },
54 '{ "error": { "code": "unknown_action" } }'
57 api.get( { action: 'doesntexist' } )
58 .fail( function ( errorCode ) {
59 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
63 QUnit.test( 'FormData support', function ( assert ) {
65 var api = new mw.Api();
67 this.server.respond( function ( request ) {
68 if ( window.FormData ) {
69 assert.ok( !request.url.match( /action=/ ), 'Request has no query string' );
70 assert.ok( request.requestBody instanceof FormData, 'Request uses FormData body' );
72 assert.ok( !request.url.match( /action=test/ ), 'Request has no query string' );
73 assert.equal( request.requestBody, 'action=test&format=json', 'Request uses query string body' );
75 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
78 api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
81 QUnit.test( 'Converting arrays to pipe-separated', function ( assert ) {
83 var api = new mw.Api();
85 this.server.respond( function ( request ) {
86 assert.ok( request.url.match( /test=foo%7Cbar%7Cbaz/ ), 'Pipe-separated value was submitted' );
87 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
90 api.get( { test: [ 'foo', 'bar', 'baz' ] } );
93 QUnit.test( 'Omitting false booleans', function ( assert ) {
95 var api = new mw.Api();
97 this.server.respond( function ( request ) {
98 assert.ok( !request.url.match( /foo/ ), 'foo query parameter is not present' );
99 assert.ok( request.url.match( /bar=true/ ), 'bar query parameter is present with value true' );
100 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
103 api.get( { foo: false, bar: true } );
106 QUnit.test( 'getToken() - cached', function ( assert ) {
108 var api = new mw.Api();
110 // Get editToken for local wiki, this should not make
111 // a request as it should be retrieved from mw.user.tokens.
112 api.getToken( 'edit' )
113 .done( function ( token ) {
114 assert.ok( token.length, 'Got a token' );
116 .fail( function ( err ) {
117 assert.equal( '', err, 'API error' );
120 assert.equal( this.server.requests.length, 0, 'Requests made' );
123 QUnit.test( 'getToken() - uncached', function ( assert ) {
125 var api = new mw.Api();
127 this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
128 '{ "query": { "tokens": { "testuncachedtoken": "good" } } }'
131 // Get a token of a type that isn't prepopulated by user.tokens.
132 // Could use "block" or "delete" here, but those could in theory
133 // be added to user.tokens, use a fake one instead.
134 api.getToken( 'testuncached' )
135 .done( function ( token ) {
136 assert.equal( token, 'good', 'The token' );
138 .fail( function ( err ) {
139 assert.equal( err, '', 'API error' );
142 api.getToken( 'testuncached' )
143 .done( function ( token ) {
144 assert.equal( token, 'good', 'The cached token' );
146 .fail( function ( err ) {
147 assert.equal( err, '', 'API error' );
150 assert.equal( this.server.requests.length, 1, 'Requests made' );
153 QUnit.test( 'getToken() - error', function ( assert ) {
155 var api = new mw.Api();
157 this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
159 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
160 '{ "query": { "tokens": { "testerrortoken": "good" } } }'
164 // Don't cache error (bug 65268)
165 api.getToken( 'testerror' ).fail( function ( err ) {
166 assert.equal( err, 'bite-me', 'Expected error' );
168 // Make this request after the first one has finished.
169 // If we make it simultaneously we still want it to share
170 // the cache, but as soon as it is fulfilled as error we
171 // reject it so that the next one tries fresh.
172 api.getToken( 'testerror' ).done( function ( token ) {
173 assert.equal( token, 'good', 'The token' );
178 QUnit.test( 'getToken() - deprecated', function ( assert ) {
180 // Cache API endpoint from default to avoid cachehit in mw.user.tokens
181 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
183 this.server.respondWith( /type=csrf/, [ 200, { 'Content-Type': 'application/json' },
184 '{ "query": { "tokens": { "csrftoken": "csrfgood" } } }'
187 // Get a token of a type that is in the legacy map.
188 api.getToken( 'email' )
189 .done( function ( token ) {
190 assert.equal( token, 'csrfgood', 'Token' );
192 .fail( function ( err ) {
193 assert.equal( err, '', 'API error' );
196 assert.equal( this.server.requests.length, 1, 'Requests made' );
199 QUnit.test( 'badToken()', function ( assert ) {
201 var api = new mw.Api(),
204 this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
206 '{ "query": { "tokens": { "testbadtoken": "bad" } } }',
207 '{ "query": { "tokens": { "testbadtoken": "good" } } }'
211 api.getToken( 'testbad' )
213 api.badToken( 'testbad' );
214 return api.getToken( 'testbad' );
216 .then( function ( token ) {
217 assert.equal( token, 'good', 'The token' );
218 assert.equal( test.server.requests.length, 2, 'Requests made' );
223 QUnit.test( 'badToken( legacy )', function ( assert ) {
225 var api = new mw.Api( { ajax: { url: '/badTokenLegacy/api.php' } } ),
228 this.server.respondWith( /type=csrf/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
230 '{ "query": { "tokens": { "csrftoken": "badlegacy" } } }',
231 '{ "query": { "tokens": { "csrftoken": "goodlegacy" } } }'
235 api.getToken( 'options' )
237 api.badToken( 'options' );
238 return api.getToken( 'options' );
240 .then( function ( token ) {
241 assert.equal( token, 'goodlegacy', 'The token' );
242 assert.equal( test.server.requests.length, 2, 'Request made' );
247 QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
249 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
251 this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
252 '{ "query": { "tokens": { "testposttoken": "good" } } }'
254 this.server.respondWith( 'POST', /api/, function ( request ) {
255 if ( request.requestBody.match( /token=good/ ) ) {
256 request.respond( 200, { 'Content-Type': 'application/json' },
257 '{ "example": { "foo": "quux" } }'
262 api.postWithToken( 'testpost', { action: 'example', key: 'foo' } )
263 .done( function ( data ) {
264 assert.deepEqual( data, { example: { foo: 'quux' } } );
268 QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
270 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
272 this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
273 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
276 api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } )
277 .fail( function ( errorCode ) {
278 assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
281 assert.equal( this.server.requests.length, 1, 'Requests made' );
284 QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
286 var api = new mw.Api();
288 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
308 assert.ok( false, 'This parameter cannot be a callback' );
311 .always( function ( data ) {
312 assert.equal( data.example, 'quux' );
315 assert.equal( this.server.requests.length, 2, 'Request made' );
316 assert.equal( this.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' );
319 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
321 var api = new mw.Api();
323 this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
325 '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }',
326 '{ "query": { "tokens": { "testbadtokentoken": "good" } } }'
329 this.server.respondWith( 'POST', /api/, function ( request ) {
330 if ( request.requestBody.match( /token=bad/ ) ) {
331 request.respond( 200, { 'Content-Type': 'application/json' },
332 '{ "error": { "code": "badtoken" } }'
335 if ( request.requestBody.match( /token=good/ ) ) {
336 request.respond( 200, { 'Content-Type': 'application/json' },
337 '{ "example": { "foo": "quux" } }'
342 // - Request: new token -> bad
343 // - Request: action=example -> badtoken error
344 // - Request: new token -> good
345 // - Request: action=example -> success
346 api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
347 .done( function ( data ) {
348 assert.deepEqual( data, { example: { foo: 'quux' } } );
352 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
357 this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
359 '{ "query": { "tokens": { "testoncetoken": "good-A" } } }',
360 '{ "query": { "tokens": { "testoncetoken": "good-B" } } }'
363 sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
365 '{ "example": { "value": "A" } }',
366 '{ "error": { "code": "badtoken" } }'
369 this.server.respondWith( 'POST', /api/, function ( request ) {
370 if ( request.requestBody.match( /token=good-A/ ) ) {
371 sequenceA( request );
372 } else if ( request.requestBody.match( /token=good-B/ ) ) {
373 request.respond( 200, { 'Content-Type': 'application/json' },
374 '{ "example": { "value": "B" } }'
379 // - Request: new token -> A
380 // - Request: action=example
381 api.postWithToken( 'testonce', { action: 'example', key: 'foo' } )
382 .done( function ( data ) {
383 assert.deepEqual( data, { example: { value: 'A' } } );
386 // - Request: action=example w/ token A -> badtoken error
387 // - Request: new token -> B
388 // - Request: action=example w/ token B -> success
389 api.postWithToken( 'testonce', { action: 'example', key: 'bar' } )
390 .done( function ( data ) {
391 assert.deepEqual( data, { example: { value: 'B' } } );
395 QUnit.module( 'mediawiki.api (2)', {
398 requests = this.requests = [];
399 this.api = new mw.Api();
400 this.sandbox.stub( jQuery, 'ajax', function () {
401 var request = $.extend( {
402 abort: self.sandbox.spy()
404 requests.push( request );
410 QUnit.test( '#abort', 3, function ( assert ) {
418 assert.ok( this.requests.length === 2, 'Check both requests triggered' );
419 $.each( this.requests, function ( i, request ) {
420 assert.ok( request.abort.calledOnce, 'abort request number ' + i );
423 }( mediaWiki, jQuery ) );