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( 'Converting arrays to pipe-separated', function ( assert ) {
67 var api = new mw.Api();
68 api.get( { test: [ 'foo', 'bar', 'baz' ] } );
70 this.server.respond( function ( request ) {
71 assert.ok( request.url.match( /test=foo%7Cbar%7Cbaz/ ), 'Pipe-separated value was submitted' );
72 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
76 QUnit.test( 'getToken( pre-populated )', function ( assert ) {
79 var api = new mw.Api();
81 // Get editToken for local wiki, this should not make
82 // a request as it should be retrieved from user.tokens.
83 api.getToken( 'edit' )
84 .done( function ( token ) {
85 assert.ok( token.length, 'Got a token' );
87 .fail( function ( err ) {
88 assert.equal( '', err, 'API error' );
91 assert.equal( this.server.requests.length, 0, 'Requests made' );
94 QUnit.test( 'getToken()', function ( assert ) {
100 // Get a token of a type that isn't prepopulated by user.tokens.
101 // Could use "block" or "delete" here, but those could in theory
102 // be added to user.tokens, use a fake one instead.
103 api.getToken( 'testaction' )
104 .done( function ( token ) {
105 assert.ok( token.length, 'Got testaction token' );
107 .fail( function ( err ) {
108 assert.equal( err, '', 'API error' );
110 api.getToken( 'testaction' )
111 .done( function ( token ) {
112 assert.ok( token.length, 'Got testaction token (cached)' );
114 .fail( function ( err ) {
115 assert.equal( err, '', 'API error' );
118 // Don't cache error (bug 65268)
119 api.getToken( 'testaction2' )
120 .fail( function ( err ) {
121 assert.equal( err, 'bite-me', 'Expected error' );
123 .always( function () {
124 // Make this request after the first one has finished.
125 // If we make it simultaneously we still want it to share
126 // the cache, but as soon as it is fulfilled as error we
127 // reject it so that the next one tries fresh.
128 api.getToken( 'testaction2' )
129 .done( function ( token ) {
130 assert.ok( token.length, 'Got testaction2 token (error was not be cached)' );
132 .fail( function ( err ) {
133 assert.equal( err, '', 'API error' );
136 assert.equal( test.server.requests.length, 3, 'Requests made' );
138 test.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
139 '{ "tokens": { "testaction2token": "0123abc" } }'
143 this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
144 '{ "tokens": { "testactiontoken": "0123abc" } }'
147 this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
148 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }'
152 QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
155 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
158 // - Performs action=example
159 api.postWithToken( 'testsimpletoken', { action: 'example', key: 'foo' } )
160 .done( function ( data ) {
161 assert.deepEqual( data, { example: { foo: 'quux' } } );
164 this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
165 '{ "tokens": { "testsimpletokentoken": "a-bad-token" } }'
168 this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
169 '{ "example": { "foo": "quux" } }'
173 QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
176 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
178 api.postWithToken( 'testasserttoken', { action: 'example', key: 'foo', assert: 'user' } )
179 .fail( function ( errorCode ) {
180 assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
183 assert.equal( this.server.requests.length, 1, 'Request for token made' );
184 this.server.respondWith( /assert=user/, function ( request ) {
187 { 'Content-Type': 'application/json' },
188 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
192 this.server.respond();
195 QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
198 var api = new mw.Api();
218 assert.ok( false, 'This parameter cannot be a callback' );
221 .always( function ( data ) {
222 assert.equal( data.example, 'quux' );
225 assert.equal( this.server.requests.length, 2, 'Request made' );
226 assert.equal( this.server.requests[0].requestHeaders['X-Foo'], 'Bar', 'Header sent' );
228 this.server.respond( function ( request ) {
229 request.respond( 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' );
233 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
236 var api = new mw.Api();
239 // - Request: action=example -> badtoken error
240 // - Request: new token
241 // - Request: action=example
242 api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
243 .done( function ( data ) {
244 assert.deepEqual( data, { example: { foo: 'quux' } } );
247 this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
248 '{ "tokens": { "testbadtokentoken": "a-bad-token" } }'
251 this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
252 '{ "error": { "code": "badtoken" } }'
255 this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
256 '{ "tokens": { "testbadtokentoken": "a-good-token" } }'
259 this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' },
260 '{ "example": { "foo": "quux" } }'
265 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
268 var api = new mw.Api();
271 // - Request: action=example
272 api.postWithToken( 'testbadtokencache', { action: 'example', key: 'foo' } )
273 .done( function ( data ) {
274 assert.deepEqual( data, { example: { foo: 'quux' } } );
277 // - Cache: Try previously cached token
278 // - Request: action=example -> badtoken error
279 // - Request: new token
280 // - Request: action=example
281 api.postWithToken( 'testbadtokencache', { action: 'example', key: 'bar' } )
282 .done( function ( data ) {
283 assert.deepEqual( data, { example: { bar: 'quux' } } );
286 this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
287 '{ "tokens": { "testbadtokencachetoken": "a-good-token-once" } }'
290 this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
291 '{ "example": { "foo": "quux" } }'
294 this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
295 '{ "error": { "code": "badtoken" } }'
298 this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' },
299 '{ "tokens": { "testbadtokencachetoken": "a-good-new-token" } }'
302 this.server.requests[4].respond( 200, { 'Content-Type': 'application/json' },
303 '{ "example": { "bar": "quux" } }'