Merge "Implement HTMLComboboxField"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.test.js
1 ( function ( mw, $ ) {
2 QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 this.server.respondImmediately = true;
6 this.clock = this.sandbox.useFakeTimers();
7 },
8 teardown: function () {
9 // https://github.com/jquery/jquery/issues/2453
10 this.clock.tick();
11 }
12 } ) );
13
14 function sequence( responses ) {
15 var i = 0;
16 return function ( request ) {
17 var response = responses[ i ];
18 if ( response ) {
19 i++;
20 request.respond.apply( request, response );
21 }
22 };
23 }
24
25 function sequenceBodies( status, headers, bodies ) {
26 jQuery.each( bodies, function ( i, body ) {
27 bodies[ i ] = [ status, headers, body ];
28 } );
29 return sequence( bodies );
30 }
31
32 QUnit.test( 'Basic functionality', function ( assert ) {
33 QUnit.expect( 2 );
34 var api = new mw.Api();
35
36 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
37
38 api.get( {} )
39 .done( function ( data ) {
40 assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
41 } );
42
43 api.post( {} )
44 .done( function ( data ) {
45 assert.deepEqual( data, [], 'Simple POST request' );
46 } );
47 } );
48
49 QUnit.test( 'API error', function ( assert ) {
50 QUnit.expect( 1 );
51 var api = new mw.Api();
52
53 this.server.respond( [ 200, { 'Content-Type': 'application/json' },
54 '{ "error": { "code": "unknown_action" } }'
55 ] );
56
57 api.get( { action: 'doesntexist' } )
58 .fail( function ( errorCode ) {
59 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
60 } );
61 } );
62
63 QUnit.test( 'FormData support', function ( assert ) {
64 QUnit.expect( 2 );
65 var api = new mw.Api();
66
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' );
71 } else {
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' );
74 }
75 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
76 } );
77
78 api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
79 } );
80
81 QUnit.test( 'Converting arrays to pipe-separated', function ( assert ) {
82 QUnit.expect( 1 );
83 var api = new mw.Api();
84
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' }, '[]' );
88 } );
89
90 api.get( { test: [ 'foo', 'bar', 'baz' ] } );
91 } );
92
93 QUnit.test( 'Omitting false booleans', function ( assert ) {
94 QUnit.expect( 2 );
95 var api = new mw.Api();
96
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' }, '[]' );
101 } );
102
103 api.get( { foo: false, bar: true } );
104 } );
105
106 QUnit.test( 'getToken() - cached', function ( assert ) {
107 QUnit.expect( 2 );
108 var api = new mw.Api();
109
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' );
115 } )
116 .fail( function ( err ) {
117 assert.equal( '', err, 'API error' );
118 } );
119
120 assert.equal( this.server.requests.length, 0, 'Requests made' );
121 } );
122
123 QUnit.test( 'getToken() - uncached', function ( assert ) {
124 QUnit.expect( 3 );
125 var api = new mw.Api();
126
127 this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
128 '{ "query": { "tokens": { "testuncachedtoken": "good" } } }'
129 ] );
130
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' );
137 } )
138 .fail( function ( err ) {
139 assert.equal( err, '', 'API error' );
140 } );
141
142 api.getToken( 'testuncached' )
143 .done( function ( token ) {
144 assert.equal( token, 'good', 'The cached token' );
145 } )
146 .fail( function ( err ) {
147 assert.equal( err, '', 'API error' );
148 } );
149
150 assert.equal( this.server.requests.length, 1, 'Requests made' );
151 } );
152
153 QUnit.test( 'getToken() - error', function ( assert ) {
154 QUnit.expect( 2 );
155 var api = new mw.Api();
156
157 this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
158 [
159 '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
160 '{ "query": { "tokens": { "testerrortoken": "good" } } }'
161 ]
162 ) );
163
164 // Don't cache error (bug 65268)
165 api.getToken( 'testerror' ).fail( function ( err ) {
166 assert.equal( err, 'bite-me', 'Expected error' );
167
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' );
174 } );
175 } );
176 } );
177
178 QUnit.test( 'getToken() - deprecated', function ( assert ) {
179 QUnit.expect( 2 );
180 // Cache API endpoint from default to avoid cachehit in mw.user.tokens
181 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
182
183 this.server.respondWith( /type=csrf/, [ 200, { 'Content-Type': 'application/json' },
184 '{ "query": { "tokens": { "csrftoken": "csrfgood" } } }'
185 ] );
186
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' );
191 } )
192 .fail( function ( err ) {
193 assert.equal( err, '', 'API error' );
194 } );
195
196 assert.equal( this.server.requests.length, 1, 'Requests made' );
197 } );
198
199 QUnit.test( 'badToken()', function ( assert ) {
200 QUnit.expect( 2 );
201 var api = new mw.Api(),
202 test = this;
203
204 this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
205 [
206 '{ "query": { "tokens": { "testbadtoken": "bad" } } }',
207 '{ "query": { "tokens": { "testbadtoken": "good" } } }'
208 ]
209 ) );
210
211 api.getToken( 'testbad' )
212 .then( function () {
213 api.badToken( 'testbad' );
214 return api.getToken( 'testbad' );
215 } )
216 .then( function ( token ) {
217 assert.equal( token, 'good', 'The token' );
218 assert.equal( test.server.requests.length, 2, 'Requests made' );
219 } );
220
221 } );
222
223 QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
224 QUnit.expect( 1 );
225 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
226
227 this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
228 '{ "query": { "tokens": { "testposttoken": "good" } } }'
229 ] );
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" } }'
234 );
235 }
236 } );
237
238 api.postWithToken( 'testpost', { action: 'example', key: 'foo' } )
239 .done( function ( data ) {
240 assert.deepEqual( data, { example: { foo: 'quux' } } );
241 } );
242 } );
243
244 QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
245 QUnit.expect( 2 );
246 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
247
248 this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
249 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
250 ] );
251
252 api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } )
253 .fail( function ( errorCode ) {
254 assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
255 } );
256
257 assert.equal( this.server.requests.length, 1, 'Requests made' );
258 } );
259
260 QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
261 QUnit.expect( 3 );
262 var api = new mw.Api();
263
264 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
265
266 api.postWithToken(
267 'edit',
268 {
269 action: 'example'
270 },
271 {
272 headers: {
273 'X-Foo': 'Bar'
274 }
275 }
276 );
277
278 api.postWithToken(
279 'edit',
280 {
281 action: 'example'
282 },
283 function () {
284 assert.ok( false, 'This parameter cannot be a callback' );
285 }
286 )
287 .always( function ( data ) {
288 assert.equal( data.example, 'quux' );
289 } );
290
291 assert.equal( this.server.requests.length, 2, 'Request made' );
292 assert.equal( this.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' );
293 } );
294
295 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
296 QUnit.expect( 1 );
297 var api = new mw.Api();
298
299 this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
300 [
301 '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }',
302 '{ "query": { "tokens": { "testbadtokentoken": "good" } } }'
303 ]
304 ) );
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" } }'
309 );
310 }
311 if ( request.requestBody.match( /token=good/ ) ) {
312 request.respond( 200, { 'Content-Type': 'application/json' },
313 '{ "example": { "foo": "quux" } }'
314 );
315 }
316 } );
317
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' } } );
325 } );
326 } );
327
328 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
329 QUnit.expect( 2 );
330 var sequenceA,
331 api = new mw.Api();
332
333 this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
334 [
335 '{ "query": { "tokens": { "testoncetoken": "good-A" } } }',
336 '{ "query": { "tokens": { "testoncetoken": "good-B" } } }'
337 ]
338 ) );
339 sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
340 [
341 '{ "example": { "value": "A" } }',
342 '{ "error": { "code": "badtoken" } }'
343 ]
344 );
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" } }'
351 );
352 }
353 } );
354
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' } } );
360 } );
361
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' } } );
368 } );
369 } );
370
371 QUnit.module( 'mediawiki.api (2)', {
372 setup: function () {
373 var self = this,
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()
379 }, $.Deferred() );
380 requests.push( request );
381 return request;
382 } );
383 }
384 } );
385
386 QUnit.test( '#abort', 3, function ( assert ) {
387 this.api.get( {
388 a: 1
389 } );
390 this.api.post( {
391 b: 2
392 } );
393 this.api.abort();
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 );
397 } );
398 } );
399 }( mediaWiki, jQuery ) );