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( 'getToken() - cached', function ( assert
) {
95 var api
= new mw
.Api();
97 // Get editToken for local wiki, this should not make
98 // a request as it should be retrieved from mw.user.tokens.
99 api
.getToken( 'edit' )
100 .done( function ( token
) {
101 assert
.ok( token
.length
, 'Got a token' );
103 .fail( function ( err
) {
104 assert
.equal( '', err
, 'API error' );
107 assert
.equal( this.server
.requests
.length
, 0, 'Requests made' );
110 QUnit
.test( 'getToken() - uncached', function ( assert
) {
112 var api
= new mw
.Api();
114 this.server
.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
115 '{ "tokens": { "testuncachedtoken": "good" } }'
118 // Get a token of a type that isn't prepopulated by user.tokens.
119 // Could use "block" or "delete" here, but those could in theory
120 // be added to user.tokens, use a fake one instead.
121 api
.getToken( 'testuncached' )
122 .done( function ( token
) {
123 assert
.equal( token
, 'good', 'The token' );
125 .fail( function ( err
) {
126 assert
.equal( err
, '', 'API error' );
129 api
.getToken( 'testuncached' )
130 .done( function ( token
) {
131 assert
.equal( token
, 'good', 'The cached token' );
133 .fail( function ( err
) {
134 assert
.equal( err
, '', 'API error' );
137 assert
.equal( this.server
.requests
.length
, 1, 'Requests made' );
140 QUnit
.test( 'getToken() - error', function ( assert
) {
142 var api
= new mw
.Api();
144 this.server
.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
146 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
147 '{ "tokens": { "testerrortoken": "good" } }'
151 // Don't cache error (bug 65268)
152 api
.getToken( 'testerror' ).fail( function ( err
) {
153 assert
.equal( err
, 'bite-me', 'Expected error' );
155 // Make this request after the first one has finished.
156 // If we make it simultaneously we still want it to share
157 // the cache, but as soon as it is fulfilled as error we
158 // reject it so that the next one tries fresh.
159 api
.getToken( 'testerror' ).done( function ( token
) {
160 assert
.equal( token
, 'good', 'The token' );
165 QUnit
.test( 'badToken()', function ( assert
) {
167 var api
= new mw
.Api(),
170 this.server
.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
172 '{ "tokens": { "testbadtoken": "bad" } }',
173 '{ "tokens": { "testbadtoken": "good" } }'
177 api
.getToken( 'testbad' )
179 api
.badToken( 'testbad' );
180 return api
.getToken( 'testbad' );
182 .then( function ( token
) {
183 assert
.equal( token
, 'good', 'The token' );
184 assert
.equal( test
.server
.requests
.length
, 2, 'Requests made' );
189 QUnit
.test( 'postWithToken( tokenType, params )', function ( assert
) {
191 var api
= new mw
.Api( { ajax
: { url
: '/postWithToken/api.php' } } );
193 this.server
.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
194 '{ "tokens": { "testposttoken": "good" } }'
196 this.server
.respondWith( 'POST', /api/, function ( request
) {
197 if ( request
.requestBody
.match( /token=good/ ) ) {
198 request
.respond( 200, { 'Content-Type': 'application/json' },
199 '{ "example": { "foo": "quux" } }'
204 api
.postWithToken( 'testpost', { action
: 'example', key
: 'foo' } )
205 .done( function ( data
) {
206 assert
.deepEqual( data
, { example
: { foo
: 'quux' } } );
210 QUnit
.test( 'postWithToken( tokenType, params with assert )', function ( assert
) {
212 var api
= new mw
.Api( { ajax
: { url
: '/postWithToken/api.php' } } );
214 this.server
.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
215 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
218 api
.postWithToken( 'testassertpost', { action
: 'example', key
: 'foo', assert
: 'user' } )
219 .fail( function ( errorCode
) {
220 assert
.equal( errorCode
, 'assertuserfailed', 'getToken fails assert' );
223 assert
.equal( this.server
.requests
.length
, 1, 'Requests made' );
226 QUnit
.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert
) {
228 var api
= new mw
.Api();
230 this.server
.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
250 assert
.ok( false, 'This parameter cannot be a callback' );
253 .always( function ( data
) {
254 assert
.equal( data
.example
, 'quux' );
257 assert
.equal( this.server
.requests
.length
, 2, 'Request made' );
258 assert
.equal( this.server
.requests
[0].requestHeaders
['X-Foo'], 'Bar', 'Header sent' );
261 QUnit
.test( 'postWithToken() - badtoken', function ( assert
) {
263 var api
= new mw
.Api();
265 this.server
.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
267 '{ "tokens": { "testbadtokentoken": "bad" } }',
268 '{ "tokens": { "testbadtokentoken": "good" } }'
271 this.server
.respondWith( 'POST', /api/, function ( request
) {
272 if ( request
.requestBody
.match( /token=bad/ ) ) {
273 request
.respond( 200, { 'Content-Type': 'application/json' },
274 '{ "error": { "code": "badtoken" } }'
277 if ( request
.requestBody
.match( /token=good/ ) ) {
278 request
.respond( 200, { 'Content-Type': 'application/json' },
279 '{ "example": { "foo": "quux" } }'
284 // - Request: new token -> bad
285 // - Request: action=example -> badtoken error
286 // - Request: new token -> good
287 // - Request: action=example -> success
288 api
.postWithToken( 'testbadtoken', { action
: 'example', key
: 'foo' } )
289 .done( function ( data
) {
290 assert
.deepEqual( data
, { example
: { foo
: 'quux' } } );
294 QUnit
.test( 'postWithToken() - badtoken-cached', function ( assert
) {
299 this.server
.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
301 '{ "tokens": { "testoncetoken": "good-A" } }',
302 '{ "tokens": { "testoncetoken": "good-B" } }'
305 sequenceA
= sequenceBodies( 200, { 'Content-Type': 'application/json' },
307 '{ "example": { "value": "A" } }',
308 '{ "error": { "code": "badtoken" } }'
311 this.server
.respondWith( 'POST', /api/, function ( request
) {
312 if ( request
.requestBody
.match( /token=good-A/ ) ) {
313 sequenceA( request
);
314 } else if ( request
.requestBody
.match( /token=good-B/ ) ) {
315 request
.respond( 200, { 'Content-Type': 'application/json' },
316 '{ "example": { "value": "B" } }'
321 // - Request: new token -> A
322 // - Request: action=example
323 api
.postWithToken( 'testonce', { action
: 'example', key
: 'foo' } )
324 .done( function ( data
) {
325 assert
.deepEqual( data
, { example
: { value
: 'A' } } );
328 // - Request: action=example w/ token A -> badtoken error
329 // - Request: new token -> B
330 // - Request: action=example w/ token B -> success
331 api
.postWithToken( 'testonce', { action
: 'example', key
: 'bar' } )
332 .done( function ( data
) {
333 assert
.deepEqual( data
, { example
: { value
: 'B' } } );