2 QUnit
.module( 'mediawiki.api', QUnit
.newMwEnvironment( {
4 this.server
= this.sandbox
.useFakeServer();
8 QUnit
.test( 'Basic functionality', function ( assert
) {
11 var api
= new mw
.Api();
14 .done( function ( data
) {
15 assert
.deepEqual( data
, [], 'If request succeeds without errors, resolve deferred' );
19 .done( function ( data
) {
20 assert
.deepEqual( data
, [], 'Simple POST request' );
23 this.server
.respond( function ( request
) {
24 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
28 QUnit
.test( 'API error', function ( assert
) {
31 var api
= new mw
.Api();
33 api
.get( { action
: 'doesntexist' } )
34 .fail( function ( errorCode
) {
35 assert
.equal( errorCode
, 'unknown_action', 'API error should reject the deferred' );
38 this.server
.respond( function ( request
) {
39 request
.respond( 200, { 'Content-Type': 'application/json' },
40 '{ "error": { "code": "unknown_action" } }'
45 QUnit
.test( 'FormData support', function ( assert
) {
48 var api
= new mw
.Api();
50 api
.post( { action
: 'test' }, { contentType
: 'multipart/form-data' } );
52 this.server
.respond( function ( request
) {
53 if ( window
.FormData
) {
54 assert
.ok( !request
.url
.match( /action=/), 'Request has no query string' );
55 assert
.ok( request
.requestBody
instanceof FormData
, 'Request uses FormData body' );
57 assert
.ok( !request
.url
.match( /action=test/), 'Request has no query string' );
58 assert
.equal( request
.requestBody
, 'action=test&format=json', 'Request uses query string body' );
60 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
64 QUnit
.test( 'Deprecated callback methods', function ( assert
) {
67 var api
= new mw
.Api();
69 this.suppressWarnings();
71 api
.get( {}, function () {
72 assert
.ok( true, 'Function argument treated as success callback.' );
77 assert
.ok( true, '"ok" property treated as success callback.' );
81 api
.get( { action
: 'doesntexist' }, {
83 assert
.ok( true, '"err" property treated as error callback.' );
87 this.restoreWarnings();
89 this.server
.respondWith( /action=query/, function ( request
) {
90 request
.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
93 this.server
.respondWith( /action=doesntexist/, function ( request
) {
94 request
.respond( 200, { 'Content-Type': 'application/json' },
95 '{ "error": { "code": "unknown_action" } }'
99 this.server
.respond();
102 QUnit
.test( 'getToken( pre-populated )', function ( assert
) {
105 var api
= new mw
.Api();
107 // Get editToken for local wiki, this should not make
108 // a request as it should be retrieved from user.tokens.
109 api
.getToken( 'edit' )
110 .done( function ( token
) {
111 assert
.ok( token
.length
, 'Got a token' );
113 .fail( function ( err
) {
114 assert
.equal( '', err
, 'API error' );
117 assert
.equal( this.server
.requests
.length
, 0, 'Requests made' );
120 QUnit
.test( 'getToken()', function ( assert
) {
126 // Get a token of a type that isn't prepopulated by user.tokens.
127 // Could use "block" or "delete" here, but those could in theory
128 // be added to user.tokens, use a fake one instead.
129 api
.getToken( 'testaction' )
130 .done( function ( token
) {
131 assert
.ok( token
.length
, 'Got testaction token' );
133 .fail( function ( err
) {
134 assert
.equal( err
, '', 'API error' );
136 api
.getToken( 'testaction' )
137 .done( function ( token
) {
138 assert
.ok( token
.length
, 'Got testaction token (cached)' );
140 .fail( function ( err
) {
141 assert
.equal( err
, '', 'API error' );
144 // Don't cache error (bug 65268)
145 api
.getToken( 'testaction2' )
146 .fail( function ( err
) {
147 assert
.equal( err
, 'bite-me', 'Expected error' );
149 .always( function () {
150 // Make this request after the first one has finished.
151 // If we make it simultaneously we still want it to share
152 // the cache, but as soon as it is fulfilled as error we
153 // reject it so that the next one tries fresh.
154 api
.getToken( 'testaction2' )
155 .done( function ( token
) {
156 assert
.ok( token
.length
, 'Got testaction2 token (error was not be cached)' );
158 .fail( function ( err
) {
159 assert
.equal( err
, '', 'API error' );
162 assert
.equal( test
.server
.requests
.length
, 3, 'Requests made' );
164 test
.server
.requests
[2].respond( 200, { 'Content-Type': 'application/json' },
165 '{ "tokens": { "testaction2token": "0123abc" } }'
169 this.server
.requests
[0].respond( 200, { 'Content-Type': 'application/json' },
170 '{ "tokens": { "testactiontoken": "0123abc" } }'
173 this.server
.requests
[1].respond( 200, { 'Content-Type': 'application/json' },
174 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }'
178 QUnit
.test( 'postWithToken( tokenType, params )', function ( assert
) {
181 var api
= new mw
.Api( { ajax
: { url
: '/postWithToken/api.php' } } );
184 // - Performs action=example
185 api
.postWithToken( 'testsimpletoken', { action
: 'example', key
: 'foo' } )
186 .done( function ( data
) {
187 assert
.deepEqual( data
, { example
: { foo
: 'quux' } } );
190 this.server
.requests
[0].respond( 200, { 'Content-Type': 'application/json' },
191 '{ "tokens": { "testsimpletokentoken": "a-bad-token" } }'
194 this.server
.requests
[1].respond( 200, { 'Content-Type': 'application/json' },
195 '{ "example": { "foo": "quux" } }'
199 QUnit
.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert
) {
202 var api
= new mw
.Api();
222 assert
.ok( false, 'This parameter cannot be a callback' );
225 .always( function ( data
) {
226 assert
.equal( data
.example
, 'quux' );
229 assert
.equal( this.server
.requests
.length
, 2, 'Request made' );
230 assert
.equal( this.server
.requests
[0].requestHeaders
['X-Foo'], 'Bar', 'Header sent' );
232 this.server
.respond( function ( request
) {
233 request
.respond( 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' );
237 QUnit
.test( 'postWithToken() - badtoken', function ( assert
) {
240 var api
= new mw
.Api();
243 // - Request: action=example -> badtoken error
244 // - Request: new token
245 // - Request: action=example
246 api
.postWithToken( 'testbadtoken', { action
: 'example', key
: 'foo' } )
247 .done( function ( data
) {
248 assert
.deepEqual( data
, { example
: { foo
: 'quux' } } );
251 this.server
.requests
[0].respond( 200, { 'Content-Type': 'application/json' },
252 '{ "tokens": { "testbadtokentoken": "a-bad-token" } }'
255 this.server
.requests
[1].respond( 200, { 'Content-Type': 'application/json' },
256 '{ "error": { "code": "badtoken" } }'
259 this.server
.requests
[2].respond( 200, { 'Content-Type': 'application/json' },
260 '{ "tokens": { "testbadtokentoken": "a-good-token" } }'
263 this.server
.requests
[3].respond( 200, { 'Content-Type': 'application/json' },
264 '{ "example": { "foo": "quux" } }'
269 QUnit
.test( 'postWithToken() - badtoken-cached', function ( assert
) {
272 var api
= new mw
.Api();
275 // - Request: action=example
276 api
.postWithToken( 'testbadtokencache', { action
: 'example', key
: 'foo' } )
277 .done( function ( data
) {
278 assert
.deepEqual( data
, { example
: { foo
: 'quux' } } );
281 // - Cache: Try previously cached token
282 // - Request: action=example -> badtoken error
283 // - Request: new token
284 // - Request: action=example
285 api
.postWithToken( 'testbadtokencache', { action
: 'example', key
: 'bar' } )
286 .done( function ( data
) {
287 assert
.deepEqual( data
, { example
: { bar
: 'quux' } } );
290 this.server
.requests
[0].respond( 200, { 'Content-Type': 'application/json' },
291 '{ "tokens": { "testbadtokencachetoken": "a-good-token-once" } }'
294 this.server
.requests
[1].respond( 200, { 'Content-Type': 'application/json' },
295 '{ "example": { "foo": "quux" } }'
298 this.server
.requests
[2].respond( 200, { 'Content-Type': 'application/json' },
299 '{ "error": { "code": "badtoken" } }'
302 this.server
.requests
[3].respond( 200, { 'Content-Type': 'application/json' },
303 '{ "tokens": { "testbadtokencachetoken": "a-good-new-token" } }'
306 this.server
.requests
[4].respond( 200, { 'Content-Type': 'application/json' },
307 '{ "example": { "bar": "quux" } }'