Merge "Comment out getSubpagesForPrefixSearch of Special:Tags"
[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( 'badToken( legacy )', function ( assert ) {
224 QUnit.expect( 2 );
225 var api = new mw.Api( { ajax: { url: '/badTokenLegacy/api.php' } } ),
226 test = this;
227
228 this.server.respondWith( /type=csrf/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
229 [
230 '{ "query": { "tokens": { "csrftoken": "badlegacy" } } }',
231 '{ "query": { "tokens": { "csrftoken": "goodlegacy" } } }'
232 ]
233 ) );
234
235 api.getToken( 'options' )
236 .then( function () {
237 api.badToken( 'options' );
238 return api.getToken( 'options' );
239 } )
240 .then( function ( token ) {
241 assert.equal( token, 'goodlegacy', 'The token' );
242 assert.equal( test.server.requests.length, 2, 'Request made' );
243 } );
244
245 } );
246
247 QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
248 QUnit.expect( 1 );
249 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
250
251 this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
252 '{ "query": { "tokens": { "testposttoken": "good" } } }'
253 ] );
254 this.server.respondWith( 'POST', /api/, function ( request ) {
255 if ( request.requestBody.match( /token=good/ ) ) {
256 request.respond( 200, { 'Content-Type': 'application/json' },
257 '{ "example": { "foo": "quux" } }'
258 );
259 }
260 } );
261
262 api.postWithToken( 'testpost', { action: 'example', key: 'foo' } )
263 .done( function ( data ) {
264 assert.deepEqual( data, { example: { foo: 'quux' } } );
265 } );
266 } );
267
268 QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
269 QUnit.expect( 2 );
270 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
271
272 this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
273 '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
274 ] );
275
276 api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } )
277 .fail( function ( errorCode ) {
278 assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
279 } );
280
281 assert.equal( this.server.requests.length, 1, 'Requests made' );
282 } );
283
284 QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
285 QUnit.expect( 3 );
286 var api = new mw.Api();
287
288 this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
289
290 api.postWithToken(
291 'edit',
292 {
293 action: 'example'
294 },
295 {
296 headers: {
297 'X-Foo': 'Bar'
298 }
299 }
300 );
301
302 api.postWithToken(
303 'edit',
304 {
305 action: 'example'
306 },
307 function () {
308 assert.ok( false, 'This parameter cannot be a callback' );
309 }
310 )
311 .always( function ( data ) {
312 assert.equal( data.example, 'quux' );
313 } );
314
315 assert.equal( this.server.requests.length, 2, 'Request made' );
316 assert.equal( this.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' );
317 } );
318
319 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
320 QUnit.expect( 1 );
321 var api = new mw.Api();
322
323 this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
324 [
325 '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }',
326 '{ "query": { "tokens": { "testbadtokentoken": "good" } } }'
327 ]
328 ) );
329 this.server.respondWith( 'POST', /api/, function ( request ) {
330 if ( request.requestBody.match( /token=bad/ ) ) {
331 request.respond( 200, { 'Content-Type': 'application/json' },
332 '{ "error": { "code": "badtoken" } }'
333 );
334 }
335 if ( request.requestBody.match( /token=good/ ) ) {
336 request.respond( 200, { 'Content-Type': 'application/json' },
337 '{ "example": { "foo": "quux" } }'
338 );
339 }
340 } );
341
342 // - Request: new token -> bad
343 // - Request: action=example -> badtoken error
344 // - Request: new token -> good
345 // - Request: action=example -> success
346 api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
347 .done( function ( data ) {
348 assert.deepEqual( data, { example: { foo: 'quux' } } );
349 } );
350 } );
351
352 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
353 QUnit.expect( 2 );
354 var sequenceA,
355 api = new mw.Api();
356
357 this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
358 [
359 '{ "query": { "tokens": { "testoncetoken": "good-A" } } }',
360 '{ "query": { "tokens": { "testoncetoken": "good-B" } } }'
361 ]
362 ) );
363 sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
364 [
365 '{ "example": { "value": "A" } }',
366 '{ "error": { "code": "badtoken" } }'
367 ]
368 );
369 this.server.respondWith( 'POST', /api/, function ( request ) {
370 if ( request.requestBody.match( /token=good-A/ ) ) {
371 sequenceA( request );
372 } else if ( request.requestBody.match( /token=good-B/ ) ) {
373 request.respond( 200, { 'Content-Type': 'application/json' },
374 '{ "example": { "value": "B" } }'
375 );
376 }
377 } );
378
379 // - Request: new token -> A
380 // - Request: action=example
381 api.postWithToken( 'testonce', { action: 'example', key: 'foo' } )
382 .done( function ( data ) {
383 assert.deepEqual( data, { example: { value: 'A' } } );
384 } );
385
386 // - Request: action=example w/ token A -> badtoken error
387 // - Request: new token -> B
388 // - Request: action=example w/ token B -> success
389 api.postWithToken( 'testonce', { action: 'example', key: 'bar' } )
390 .done( function ( data ) {
391 assert.deepEqual( data, { example: { value: 'B' } } );
392 } );
393 } );
394
395 QUnit.module( 'mediawiki.api (2)', {
396 setup: function () {
397 var self = this,
398 requests = this.requests = [];
399 this.api = new mw.Api();
400 this.sandbox.stub( jQuery, 'ajax', function () {
401 var request = $.extend( {
402 abort: self.sandbox.spy()
403 }, $.Deferred() );
404 requests.push( request );
405 return request;
406 } );
407 }
408 } );
409
410 QUnit.test( '#abort', 3, function ( assert ) {
411 this.api.get( {
412 a: 1
413 } );
414 this.api.post( {
415 b: 2
416 } );
417 this.api.abort();
418 assert.ok( this.requests.length === 2, 'Check both requests triggered' );
419 $.each( this.requests, function ( i, request ) {
420 assert.ok( request.abort.calledOnce, 'abort request number ' + i );
421 } );
422 } );
423 }( mediaWiki, jQuery ) );