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( 'postWithToken( tokenType, params )', function ( assert
) {
225 var api
= new mw
.Api( { ajax
: { url
: '/postWithToken/api.php' } } );
227 this.server
.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
228 '{ "query": { "tokens": { "testposttoken": "good" } } }'
230 this.server
.respondWith( 'POST', /api/, function ( request
) {
231 if ( request
.requestBody
.match( /token=good/ ) ) {
232 request
.respond( 200, { 'Content-Type': 'application/json' },
233 '{ "example": { "foo": "quux" } }'
238 api
.postWithToken( 'testpost', { action
: 'example', key
: 'foo' } )
239 .done( function ( data
) {
240 assert
.deepEqual( data
, { example
: { foo
: 'quux' } } );
244 QUnit
.test( 'postWithToken( tokenType, params with assert )', function ( assert
) {
246 var api
= new mw
.Api( { ajax
: { url
: '/postWithToken/api.php' } } );
248 this.server
.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
249 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
252 api
.postWithToken( 'testassertpost', { action
: 'example', key
: 'foo', assert
: 'user' } )
253 .fail( function ( errorCode
) {
254 assert
.equal( errorCode
, 'assertuserfailed', 'getToken fails assert' );
257 assert
.equal( this.server
.requests
.length
, 1, 'Requests made' );
260 QUnit
.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert
) {
262 var api
= new mw
.Api();
264 this.server
.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
284 assert
.ok( false, 'This parameter cannot be a callback' );
287 .always( function ( data
) {
288 assert
.equal( data
.example
, 'quux' );
291 assert
.equal( this.server
.requests
.length
, 2, 'Request made' );
292 assert
.equal( this.server
.requests
[ 0 ].requestHeaders
[ 'X-Foo' ], 'Bar', 'Header sent' );
295 QUnit
.test( 'postWithToken() - badtoken', function ( assert
) {
297 var api
= new mw
.Api();
299 this.server
.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
301 '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }',
302 '{ "query": { "tokens": { "testbadtokentoken": "good" } } }'
305 this.server
.respondWith( 'POST', /api/, function ( request
) {
306 if ( request
.requestBody
.match( /token=bad/ ) ) {
307 request
.respond( 200, { 'Content-Type': 'application/json' },
308 '{ "error": { "code": "badtoken" } }'
311 if ( request
.requestBody
.match( /token=good/ ) ) {
312 request
.respond( 200, { 'Content-Type': 'application/json' },
313 '{ "example": { "foo": "quux" } }'
318 // - Request: new token -> bad
319 // - Request: action=example -> badtoken error
320 // - Request: new token -> good
321 // - Request: action=example -> success
322 api
.postWithToken( 'testbadtoken', { action
: 'example', key
: 'foo' } )
323 .done( function ( data
) {
324 assert
.deepEqual( data
, { example
: { foo
: 'quux' } } );
328 QUnit
.test( 'postWithToken() - badtoken-cached', function ( assert
) {
333 this.server
.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
335 '{ "query": { "tokens": { "testoncetoken": "good-A" } } }',
336 '{ "query": { "tokens": { "testoncetoken": "good-B" } } }'
339 sequenceA
= sequenceBodies( 200, { 'Content-Type': 'application/json' },
341 '{ "example": { "value": "A" } }',
342 '{ "error": { "code": "badtoken" } }'
345 this.server
.respondWith( 'POST', /api/, function ( request
) {
346 if ( request
.requestBody
.match( /token=good-A/ ) ) {
347 sequenceA( request
);
348 } else if ( request
.requestBody
.match( /token=good-B/ ) ) {
349 request
.respond( 200, { 'Content-Type': 'application/json' },
350 '{ "example": { "value": "B" } }'
355 // - Request: new token -> A
356 // - Request: action=example
357 api
.postWithToken( 'testonce', { action
: 'example', key
: 'foo' } )
358 .done( function ( data
) {
359 assert
.deepEqual( data
, { example
: { value
: 'A' } } );
362 // - Request: action=example w/ token A -> badtoken error
363 // - Request: new token -> B
364 // - Request: action=example w/ token B -> success
365 api
.postWithToken( 'testonce', { action
: 'example', key
: 'bar' } )
366 .done( function ( data
) {
367 assert
.deepEqual( data
, { example
: { value
: 'B' } } );
371 QUnit
.module( 'mediawiki.api (2)', {
374 requests
= this.requests
= [];
375 this.api
= new mw
.Api();
376 this.sandbox
.stub( jQuery
, 'ajax', function () {
377 var request
= $.extend( {
378 abort
: self
.sandbox
.spy()
380 requests
.push( request
);
386 QUnit
.test( '#abort', 3, function ( assert
) {
394 assert
.ok( this.requests
.length
=== 2, 'Check both requests triggered' );
395 $.each( this.requests
, function ( i
, request
) {
396 assert
.ok( request
.abort
.calledOnce
, 'abort request number ' + i
);
399 }( mediaWiki
, jQuery
) );