2 QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
4 this.server = this.sandbox.useFakeServer();
5 this.server.respondImmediately = true;
9 function sequence( responses ) {
11 return function ( request ) {
12 var response = responses[ i ];
15 request.respond.apply( request, response );
20 function sequenceBodies( status, headers, bodies ) {
21 jQuery.each( bodies, function ( i, body ) {
22 bodies[ i ] = [ status, headers, body ];
24 return sequence( bodies );
27 QUnit.test( 'get()', function ( assert ) {
28 var api = new mw.Api();
30 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
32 return api.get( {} ).then( function ( data ) {
33 assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
37 QUnit.test( 'post()', function ( assert ) {
38 var api = new mw.Api();
40 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
42 return api.post( {} ).then( function ( data ) {
43 assert.deepEqual( data, [], 'Simple POST request' );
47 QUnit.test( 'API error errorformat=bc', function ( assert ) {
48 var api = new mw.Api();
50 this.server.respond( [ 200, { 'Content-Type': 'application/json' },
51 '{ "error": { "code": "unknown_action" } }'
54 api.get( { action: 'doesntexist' } )
55 .fail( function ( errorCode ) {
56 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
58 .always( assert.async() );
61 QUnit.test( 'API error errorformat!=bc', function ( assert ) {
62 var api = new mw.Api();
64 this.server.respond( [ 200, { 'Content-Type': 'application/json' },
65 '{ "errors": [ { "code": "unknown_action", "key": "unknown-error", "params": [] } ] }'
68 api.get( { action: 'doesntexist' } )
69 .fail( function ( errorCode ) {
70 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
72 .always( assert.async() );
75 QUnit.test( 'FormData support', function ( assert ) {
76 var api = new mw.Api();
78 this.server.respond( function ( request ) {
79 if ( window.FormData ) {
80 assert.ok( !request.url.match( /action=/ ), 'Request has no query string' );
81 assert.ok( request.requestBody instanceof FormData, 'Request uses FormData body' );
83 assert.ok( !request.url.match( /action=test/ ), 'Request has no query string' );
84 assert.equal( request.requestBody, 'action=test&format=json', 'Request uses query string body' );
86 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
89 return api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
92 QUnit.test( 'Converting arrays to pipe-separated', function ( assert ) {
93 var api = new mw.Api();
95 this.server.respond( function ( request ) {
96 assert.ok( request.url.match( /test=foo%7Cbar%7Cbaz/ ), 'Pipe-separated value was submitted' );
97 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
100 return api.get( { test: [ 'foo', 'bar', 'baz' ] } );
103 QUnit.test( 'Omitting false booleans', function ( assert ) {
104 var api = new mw.Api();
106 this.server.respond( function ( request ) {
107 assert.ok( !request.url.match( /foo/ ), 'foo query parameter is not present' );
108 assert.ok( request.url.match( /bar=true/ ), 'bar query parameter is present with value true' );
109 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
112 return api.get( { foo: false, bar: true } );
115 QUnit.test( 'getToken() - cached', function ( assert ) {
116 var api = new mw.Api(),
119 // Get csrfToken for local wiki, this should not make
120 // a request as it should be retrieved from mw.user.tokens.
121 return api.getToken( 'csrf' )
122 .then( function ( token ) {
123 assert.ok( token.length, 'Got a token' );
124 }, function ( err ) {
125 assert.equal( '', err, 'API error' );
128 assert.equal( test.server.requests.length, 0, 'Requests made' );
132 QUnit.test( 'getToken() - uncached', function ( assert ) {
133 var api = new mw.Api(),
134 firstDone = assert.async(),
135 secondDone = assert.async();
137 this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
138 '{ "query": { "tokens": { "testuncachedtoken": "good" } } }'
141 // Get a token of a type that isn't prepopulated by user.tokens.
142 // Could use "block" or "delete" here, but those could in theory
143 // be added to user.tokens, use a fake one instead.
144 api.getToken( 'testuncached' )
145 .done( function ( token ) {
146 assert.equal( token, 'good', 'The token' );
148 .fail( function ( err ) {
149 assert.equal( err, '', 'API error' );
151 .always( firstDone );
153 api.getToken( 'testuncached' )
154 .done( function ( token ) {
155 assert.equal( token, 'good', 'The cached token' );
157 .fail( function ( err ) {
158 assert.equal( err, '', 'API error' );
160 .always( secondDone );
162 assert.equal( this.server.requests.length, 1, 'Requests made' );
165 QUnit.test( 'getToken() - error', function ( assert ) {
166 var api = new mw.Api();
168 this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
170 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
171 '{ "query": { "tokens": { "testerrortoken": "good" } } }'
175 // Don't cache error (bug 65268)
176 return api.getToken( 'testerror' )
177 .then( null, function ( err ) {
178 assert.equal( err, 'bite-me', 'Expected error' );
180 return api.getToken( 'testerror' );
182 .then( function ( token ) {
183 assert.equal( token, 'good', 'The token' );
187 QUnit.test( 'getToken() - deprecated', function ( assert ) {
188 // Cache API endpoint from default to avoid cachehit in mw.user.tokens
189 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ),
192 this.server.respondWith( /type=csrf/, [ 200, { 'Content-Type': 'application/json' },
193 '{ "query": { "tokens": { "csrftoken": "csrfgood" } } }'
196 // Get a token of a type that is in the legacy map.
197 return api.getToken( 'email' )
198 .done( function ( token ) {
199 assert.equal( token, 'csrfgood', 'Token' );
201 .fail( function ( err ) {
202 assert.equal( err, '', 'API error' );
204 .always( function () {
205 assert.equal( test.server.requests.length, 1, 'Requests made' );
209 QUnit.test( 'badToken()', function ( assert ) {
210 var api = new mw.Api(),
213 this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
215 '{ "query": { "tokens": { "testbadtoken": "bad" } } }',
216 '{ "query": { "tokens": { "testbadtoken": "good" } } }'
220 return api.getToken( 'testbad' )
222 api.badToken( 'testbad' );
223 return api.getToken( 'testbad' );
225 .then( function ( token ) {
226 assert.equal( token, 'good', 'The token' );
227 assert.equal( test.server.requests.length, 2, 'Requests made' );
232 QUnit.test( 'badToken( legacy )', function ( assert ) {
233 var api = new mw.Api( { ajax: { url: '/badTokenLegacy/api.php' } } ),
236 this.server.respondWith( /type=csrf/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
238 '{ "query": { "tokens": { "csrftoken": "badlegacy" } } }',
239 '{ "query": { "tokens": { "csrftoken": "goodlegacy" } } }'
243 return api.getToken( 'options' )
245 api.badToken( 'options' );
246 return api.getToken( 'options' );
248 .then( function ( token ) {
249 assert.equal( token, 'goodlegacy', 'The token' );
250 assert.equal( test.server.requests.length, 2, 'Request made' );
255 QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
256 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
258 this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
259 '{ "query": { "tokens": { "testposttoken": "good" } } }'
261 this.server.respondWith( 'POST', /api/, function ( request ) {
262 if ( request.requestBody.match( /token=good/ ) ) {
263 request.respond( 200, { 'Content-Type': 'application/json' },
264 '{ "example": { "foo": "quux" } }'
269 return api.postWithToken( 'testpost', { action: 'example', key: 'foo' } )
270 .then( function ( data ) {
271 assert.deepEqual( data, { example: { foo: 'quux' } } );
275 QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
276 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ),
279 this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
280 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
283 return api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } )
284 // Cast error to success and vice versa
285 .then( function ( ) {
286 return $.Deferred().reject( 'Unexpected success' );
287 }, function ( errorCode ) {
288 assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
289 return $.Deferred().resolve();
292 assert.equal( test.server.requests.length, 1, 'Requests made' );
296 QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
297 var api = new mw.Api(),
300 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
302 return api.postWithToken( 'csrf',
303 { action: 'example' },
311 assert.equal( test.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' );
313 return api.postWithToken( 'csrf',
314 { action: 'example' },
316 assert.ok( false, 'This parameter cannot be a callback' );
320 .then( function ( data ) {
321 assert.equal( data.example, 'quux' );
323 assert.equal( test.server.requests.length, 2, 'Request made' );
327 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
328 var api = new mw.Api();
330 this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
332 '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }',
333 '{ "query": { "tokens": { "testbadtokentoken": "good" } } }'
336 this.server.respondWith( 'POST', /api/, function ( request ) {
337 if ( request.requestBody.match( /token=bad/ ) ) {
338 request.respond( 200, { 'Content-Type': 'application/json' },
339 '{ "error": { "code": "badtoken" } }'
342 if ( request.requestBody.match( /token=good/ ) ) {
343 request.respond( 200, { 'Content-Type': 'application/json' },
344 '{ "example": { "foo": "quux" } }'
349 // - Request: new token -> bad
350 // - Request: action=example -> badtoken error
351 // - Request: new token -> good
352 // - Request: action=example -> success
353 return api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
354 .then( function ( data ) {
355 assert.deepEqual( data, { example: { foo: 'quux' } } );
359 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
363 this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
365 '{ "query": { "tokens": { "testoncetoken": "good-A" } } }',
366 '{ "query": { "tokens": { "testoncetoken": "good-B" } } }'
369 sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
371 '{ "example": { "value": "A" } }',
372 '{ "error": { "code": "badtoken" } }'
375 this.server.respondWith( 'POST', /api/, function ( request ) {
376 if ( request.requestBody.match( /token=good-A/ ) ) {
377 sequenceA( request );
378 } else if ( request.requestBody.match( /token=good-B/ ) ) {
379 request.respond( 200, { 'Content-Type': 'application/json' },
380 '{ "example": { "value": "B" } }'
385 // - Request: new token -> A
386 // - Request: action=example
387 return api.postWithToken( 'testonce', { action: 'example', key: 'foo' } )
388 .then( function ( data ) {
389 assert.deepEqual( data, { example: { value: 'A' } } );
391 // - Request: action=example w/ token A -> badtoken error
392 // - Request: new token -> B
393 // - Request: action=example w/ token B -> success
394 return api.postWithToken( 'testonce', { action: 'example', key: 'bar' } );
396 .then( function ( data ) {
397 assert.deepEqual( data, { example: { value: 'B' } } );
401 QUnit.module( 'mediawiki.api (2)', {
404 requests = this.requests = [];
405 this.api = new mw.Api();
406 this.sandbox.stub( jQuery, 'ajax', function () {
407 var request = $.extend( {
408 abort: self.sandbox.spy()
410 requests.push( request );
416 QUnit.test( '#abort', 3, function ( assert ) {
424 assert.ok( this.requests.length === 2, 'Check both requests triggered' );
425 $.each( this.requests, function ( i, request ) {
426 assert.ok( request.abort.calledOnce, 'abort request number ' + i );
429 }( mediaWiki, jQuery ) );