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 // Utility to make inline use with an assert easier
28 function match( text
, pattern
) {
29 var m
= text
.match( pattern
);
30 return m
&& m
[ 1 ] || null;
33 QUnit
.test( 'get()', function ( assert
) {
34 var api
= new mw
.Api();
36 this.server
.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
38 return api
.get( {} ).then( function ( data
) {
39 assert
.deepEqual( data
, [], 'If request succeeds without errors, resolve deferred' );
43 QUnit
.test( 'post()', function ( assert
) {
44 var api
= new mw
.Api();
46 this.server
.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
48 return api
.post( {} ).then( function ( data
) {
49 assert
.deepEqual( data
, [], 'Simple POST request' );
53 QUnit
.test( 'API error errorformat=bc', function ( assert
) {
54 var api
= new mw
.Api();
56 this.server
.respond( [ 200, { 'Content-Type': 'application/json' },
57 '{ "error": { "code": "unknown_action" } }'
60 api
.get( { action
: 'doesntexist' } )
61 .fail( function ( errorCode
) {
62 assert
.equal( errorCode
, 'unknown_action', 'API error should reject the deferred' );
64 .always( assert
.async() );
67 QUnit
.test( 'API error errorformat!=bc', function ( assert
) {
68 var api
= new mw
.Api();
70 this.server
.respond( [ 200, { 'Content-Type': 'application/json' },
71 '{ "errors": [ { "code": "unknown_action", "key": "unknown-error", "params": [] } ] }'
74 api
.get( { action
: 'doesntexist' } )
75 .fail( function ( errorCode
) {
76 assert
.equal( errorCode
, 'unknown_action', 'API error should reject the deferred' );
78 .always( assert
.async() );
81 QUnit
.test( 'FormData support', function ( assert
) {
82 var api
= new mw
.Api();
84 this.server
.respond( function ( request
) {
85 if ( window
.FormData
) {
86 assert
.ok( !request
.url
.match( /action=/ ), 'Request has no query string' );
87 assert
.ok( request
.requestBody
instanceof FormData
, 'Request uses FormData body' );
89 assert
.ok( !request
.url
.match( /action=test/ ), 'Request has no query string' );
90 assert
.equal( request
.requestBody
, 'action=test&format=json', 'Request uses query string body' );
92 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
95 return api
.post( { action
: 'test' }, { contentType
: 'multipart/form-data' } );
98 QUnit
.test( 'Converting arrays to pipe-separated (string)', function ( assert
) {
99 var api
= new mw
.Api();
101 this.server
.respond( function ( request
) {
102 assert
.equal( match( request
.url
, /test=([^&]+)/ ), 'foo%7Cbar%7Cbaz', 'Pipe-separated value was submitted' );
103 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
106 return api
.get( { test
: [ 'foo', 'bar', 'baz' ] } );
109 QUnit
.test( 'Converting arrays to pipe-separated (mw.Title)', function ( assert
) {
110 var api
= new mw
.Api();
112 this.server
.respond( function ( request
) {
113 assert
.equal( match( request
.url
, /test=([^&]+)/ ), 'Foo%7CBar', 'Pipe-separated value was submitted' );
114 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
117 return api
.get( { test
: [ new mw
.Title( 'Foo' ), new mw
.Title( 'Bar' ) ] } );
120 QUnit
.test( 'Converting arrays to pipe-separated (misc primitives)', function ( assert
) {
121 var api
= new mw
.Api();
123 this.server
.respond( function ( request
) {
124 assert
.equal( match( request
.url
, /test=([^&]+)/ ), 'true%7Cfalse%7C%7C%7C0%7C1%2E2', 'Pipe-separated value was submitted' );
125 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
128 // undefined/null will become empty string
129 return api
.get( { test
: [ true, false, undefined, null, 0, 1.2 ] } );
132 QUnit
.test( 'Omitting false booleans', function ( assert
) {
133 var api
= new mw
.Api();
135 this.server
.respond( function ( request
) {
136 assert
.ok( !request
.url
.match( /foo/ ), 'foo query parameter is not present' );
137 assert
.ok( request
.url
.match( /bar=true/ ), 'bar query parameter is present with value true' );
138 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
141 return api
.get( { foo
: false, bar
: true } );
144 QUnit
.test( 'getToken() - cached', function ( assert
) {
145 var api
= new mw
.Api(),
148 // Get csrfToken for local wiki, this should not make
149 // a request as it should be retrieved from mw.user.tokens.
150 return api
.getToken( 'csrf' )
151 .then( function ( token
) {
152 assert
.ok( token
.length
, 'Got a token' );
153 }, function ( err
) {
154 assert
.equal( '', err
, 'API error' );
157 assert
.equal( test
.server
.requests
.length
, 0, 'Requests made' );
161 QUnit
.test( 'getToken() - uncached', function ( assert
) {
162 var api
= new mw
.Api(),
163 firstDone
= assert
.async(),
164 secondDone
= assert
.async();
166 this.server
.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
167 '{ "query": { "tokens": { "testuncachedtoken": "good" } } }'
170 // Get a token of a type that isn't prepopulated by user.tokens.
171 // Could use "block" or "delete" here, but those could in theory
172 // be added to user.tokens, use a fake one instead.
173 api
.getToken( 'testuncached' )
174 .done( function ( token
) {
175 assert
.equal( token
, 'good', 'The token' );
177 .fail( function ( err
) {
178 assert
.equal( err
, '', 'API error' );
180 .always( firstDone
);
182 api
.getToken( 'testuncached' )
183 .done( function ( token
) {
184 assert
.equal( token
, 'good', 'The cached token' );
186 .fail( function ( err
) {
187 assert
.equal( err
, '', 'API error' );
189 .always( secondDone
);
191 assert
.equal( this.server
.requests
.length
, 1, 'Requests made' );
194 QUnit
.test( 'getToken() - error', function ( assert
) {
195 var api
= new mw
.Api();
197 this.server
.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
199 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
200 '{ "query": { "tokens": { "testerrortoken": "good" } } }'
204 // Don't cache error (T67268)
205 return api
.getToken( 'testerror' )
206 .then( null, function ( err
) {
207 assert
.equal( err
, 'bite-me', 'Expected error' );
209 return api
.getToken( 'testerror' );
211 .then( function ( token
) {
212 assert
.equal( token
, 'good', 'The token' );
216 QUnit
.test( 'getToken() - deprecated', function ( assert
) {
217 // Cache API endpoint from default to avoid cachehit in mw.user.tokens
218 var api
= new mw
.Api( { ajax
: { url
: '/postWithToken/api.php' } } ),
221 this.server
.respondWith( /type=csrf/, [ 200, { 'Content-Type': 'application/json' },
222 '{ "query": { "tokens": { "csrftoken": "csrfgood" } } }'
225 // Get a token of a type that is in the legacy map.
226 return api
.getToken( 'email' )
227 .done( function ( token
) {
228 assert
.equal( token
, 'csrfgood', 'Token' );
230 .fail( function ( err
) {
231 assert
.equal( err
, '', 'API error' );
233 .always( function () {
234 assert
.equal( test
.server
.requests
.length
, 1, 'Requests made' );
238 QUnit
.test( 'badToken()', function ( assert
) {
239 var api
= new mw
.Api(),
242 this.server
.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
244 '{ "query": { "tokens": { "testbadtoken": "bad" } } }',
245 '{ "query": { "tokens": { "testbadtoken": "good" } } }'
249 return api
.getToken( 'testbad' )
251 api
.badToken( 'testbad' );
252 return api
.getToken( 'testbad' );
254 .then( function ( token
) {
255 assert
.equal( token
, 'good', 'The token' );
256 assert
.equal( test
.server
.requests
.length
, 2, 'Requests made' );
261 QUnit
.test( 'badToken( legacy )', function ( assert
) {
262 var api
= new mw
.Api( { ajax
: { url
: '/badTokenLegacy/api.php' } } ),
265 this.server
.respondWith( /type=csrf/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
267 '{ "query": { "tokens": { "csrftoken": "badlegacy" } } }',
268 '{ "query": { "tokens": { "csrftoken": "goodlegacy" } } }'
272 return api
.getToken( 'options' )
274 api
.badToken( 'options' );
275 return api
.getToken( 'options' );
277 .then( function ( token
) {
278 assert
.equal( token
, 'goodlegacy', 'The token' );
279 assert
.equal( test
.server
.requests
.length
, 2, 'Request made' );
284 QUnit
.test( 'postWithToken( tokenType, params )', function ( assert
) {
285 var api
= new mw
.Api( { ajax
: { url
: '/postWithToken/api.php' } } );
287 this.server
.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
288 '{ "query": { "tokens": { "testposttoken": "good" } } }'
290 this.server
.respondWith( 'POST', /api/, function ( request
) {
291 if ( request
.requestBody
.match( /token=good/ ) ) {
292 request
.respond( 200, { 'Content-Type': 'application/json' },
293 '{ "example": { "foo": "quux" } }'
298 return api
.postWithToken( 'testpost', { action
: 'example', key
: 'foo' } )
299 .then( function ( data
) {
300 assert
.deepEqual( data
, { example
: { foo
: 'quux' } } );
304 QUnit
.test( 'postWithToken( tokenType, params with assert )', function ( assert
) {
305 var api
= new mw
.Api( { ajax
: { url
: '/postWithToken/api.php' } } ),
308 this.server
.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
309 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
312 return api
.postWithToken( 'testassertpost', { action
: 'example', key
: 'foo', assert
: 'user' } )
313 // Cast error to success and vice versa
315 return $.Deferred().reject( 'Unexpected success' );
316 }, function ( errorCode
) {
317 assert
.equal( errorCode
, 'assertuserfailed', 'getToken fails assert' );
318 return $.Deferred().resolve();
321 assert
.equal( test
.server
.requests
.length
, 1, 'Requests made' );
325 QUnit
.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert
) {
326 var api
= new mw
.Api(),
329 this.server
.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
331 return api
.postWithToken( 'csrf',
332 { action
: 'example' },
338 ).then( function () {
339 assert
.equal( test
.server
.requests
[ 0 ].requestHeaders
[ 'X-Foo' ], 'Bar', 'Header sent' );
341 return api
.postWithToken( 'csrf',
342 { action
: 'example' },
344 assert
.ok( false, 'This parameter cannot be a callback' );
347 } ).then( function ( data
) {
348 assert
.equal( data
.example
, 'quux' );
350 assert
.equal( test
.server
.requests
.length
, 2, 'Request made' );
354 QUnit
.test( 'postWithToken() - badtoken', function ( assert
) {
355 var api
= new mw
.Api();
357 this.server
.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
359 '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }',
360 '{ "query": { "tokens": { "testbadtokentoken": "good" } } }'
363 this.server
.respondWith( 'POST', /api/, function ( request
) {
364 if ( request
.requestBody
.match( /token=bad/ ) ) {
365 request
.respond( 200, { 'Content-Type': 'application/json' },
366 '{ "error": { "code": "badtoken" } }'
369 if ( request
.requestBody
.match( /token=good/ ) ) {
370 request
.respond( 200, { 'Content-Type': 'application/json' },
371 '{ "example": { "foo": "quux" } }'
376 // - Request: new token -> bad
377 // - Request: action=example -> badtoken error
378 // - Request: new token -> good
379 // - Request: action=example -> success
380 return api
.postWithToken( 'testbadtoken', { action
: 'example', key
: 'foo' } )
381 .then( function ( data
) {
382 assert
.deepEqual( data
, { example
: { foo
: 'quux' } } );
386 QUnit
.test( 'postWithToken() - badtoken-cached', function ( assert
) {
390 this.server
.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
392 '{ "query": { "tokens": { "testoncetoken": "good-A" } } }',
393 '{ "query": { "tokens": { "testoncetoken": "good-B" } } }'
396 sequenceA
= sequenceBodies( 200, { 'Content-Type': 'application/json' },
398 '{ "example": { "value": "A" } }',
399 '{ "error": { "code": "badtoken" } }'
402 this.server
.respondWith( 'POST', /api/, function ( request
) {
403 if ( request
.requestBody
.match( /token=good-A/ ) ) {
404 sequenceA( request
);
405 } else if ( request
.requestBody
.match( /token=good-B/ ) ) {
406 request
.respond( 200, { 'Content-Type': 'application/json' },
407 '{ "example": { "value": "B" } }'
412 // - Request: new token -> A
413 // - Request: action=example
414 return api
.postWithToken( 'testonce', { action
: 'example', key
: 'foo' } )
415 .then( function ( data
) {
416 assert
.deepEqual( data
, { example
: { value
: 'A' } } );
418 // - Request: action=example w/ token A -> badtoken error
419 // - Request: new token -> B
420 // - Request: action=example w/ token B -> success
421 return api
.postWithToken( 'testonce', { action
: 'example', key
: 'bar' } );
423 .then( function ( data
) {
424 assert
.deepEqual( data
, { example
: { value
: 'B' } } );
428 QUnit
.module( 'mediawiki.api (2)', {
431 requests
= this.requests
= [];
432 this.api
= new mw
.Api();
433 this.sandbox
.stub( jQuery
, 'ajax', function () {
434 var request
= $.extend( {
435 abort
: self
.sandbox
.spy()
437 requests
.push( request
);
443 QUnit
.test( '#abort', function ( assert
) {
451 assert
.ok( this.requests
.length
=== 2, 'Check both requests triggered' );
452 $.each( this.requests
, function ( i
, request
) {
453 assert
.ok( request
.abort
.calledOnce
, 'abort request number ' + i
);
456 }( mediaWiki
, jQuery
) );