| 1 |
QUnit.module( 'mediawiki.api', ( hooks ) => { |
| 2 |
hooks.beforeEach( function () { |
| 3 |
this.server = this.sandbox.useFakeServer(); |
| 4 |
this.server.respondImmediately = true; |
| 5 |
} ); |
| 6 |
|
| 7 |
function sequence( responses ) { |
| 8 |
let i = 0; |
| 9 |
return function ( request ) { |
| 10 |
const response = responses[ i ]; |
| 11 |
if ( response ) { |
| 12 |
i++; |
| 13 |
request.respond( ...response ); |
| 14 |
} |
| 15 |
}; |
| 16 |
} |
| 17 |
|
| 18 |
function sequenceBodies( status, headers, bodies ) { |
| 19 |
bodies.forEach( ( body, i ) => { |
| 20 |
bodies[ i ] = [ status, headers, body ]; |
| 21 |
} ); |
| 22 |
return sequence( bodies ); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
function match( text, pattern ) { |
| 27 |
const m = text.match( pattern ); |
| 28 |
return m && m[ 1 ] || null; |
| 29 |
} |
| 30 |
|
| 31 |
QUnit.test( 'get()', function ( assert ) { |
| 32 |
const api = new mw.Api(); |
| 33 |
|
| 34 |
this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] ); |
| 35 |
|
| 36 |
return api.get( {} ).then( ( data ) => { |
| 37 |
assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' ); |
| 38 |
} ); |
| 39 |
} ); |
| 40 |
|
| 41 |
QUnit.test( 'post()', function ( assert ) { |
| 42 |
const api = new mw.Api(); |
| 43 |
|
| 44 |
this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] ); |
| 45 |
|
| 46 |
return api.post( {} ).then( ( data ) => { |
| 47 |
assert.deepEqual( data, [], 'Simple POST request' ); |
| 48 |
} ); |
| 49 |
} ); |
| 50 |
|
| 51 |
QUnit.test( 'API error errorformat=bc', function ( assert ) { |
| 52 |
const api = new mw.Api(); |
| 53 |
|
| 54 |
this.server.respond( [ 200, { 'Content-Type': 'application/json' }, |
| 55 |
'{ "error": { "code": "unknown_action" } }' |
| 56 |
] ); |
| 57 |
|
| 58 |
api.get( { action: 'doesntexist' } ) |
| 59 |
.fail( ( errorCode ) => { |
| 60 |
assert.strictEqual( errorCode, 'unknown_action', 'API error should reject the deferred' ); |
| 61 |
} ) |
| 62 |
.always( assert.async() ); |
| 63 |
} ); |
| 64 |
|
| 65 |
QUnit.test( 'API error errorformat!=bc', function ( assert ) { |
| 66 |
const api = new mw.Api(); |
| 67 |
|
| 68 |
this.server.respond( [ 200, { 'Content-Type': 'application/json' }, |
| 69 |
'{ "errors": [ { "code": "unknown_action", "key": "unknown-error", "params": [] } ] }' |
| 70 |
] ); |
| 71 |
|
| 72 |
api.get( { action: 'doesntexist' } ) |
| 73 |
.fail( ( errorCode ) => { |
| 74 |
assert.strictEqual( errorCode, 'unknown_action', 'API error should reject the deferred' ); |
| 75 |
} ) |
| 76 |
.always( assert.async() ); |
| 77 |
} ); |
| 78 |
|
| 79 |
QUnit.test( 'FormData support', function ( assert ) { |
| 80 |
const api = new mw.Api(); |
| 81 |
|
| 82 |
this.server.respond( ( request ) => { |
| 83 |
if ( window.FormData ) { |
| 84 |
assert.false( /action=/.test( request.url ), 'Request has no query string' ); |
| 85 |
assert.true( request.requestBody instanceof FormData, 'Request uses FormData body' ); |
| 86 |
} else { |
| 87 |
assert.false( /action=test/.test( request.url ), 'Request has no query string' ); |
| 88 |
assert.strictEqual( request.requestBody, 'action=test&format=json', 'Request uses query string body' ); |
| 89 |
} |
| 90 |
request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); |
| 91 |
} ); |
| 92 |
|
| 93 |
return api.post( { action: 'test' }, { contentType: 'multipart/form-data' } ); |
| 94 |
} ); |
| 95 |
|
| 96 |
QUnit.test( 'Converting arrays to pipe-separated (string)', function ( assert ) { |
| 97 |
const api = new mw.Api(); |
| 98 |
|
| 99 |
this.server.respond( ( request ) => { |
| 100 |
assert.strictEqual( match( request.url, /test=([^&]+)/ ), 'foo%7Cbar%7Cbaz', 'Pipe-separated value was submitted' ); |
| 101 |
request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); |
| 102 |
} ); |
| 103 |
|
| 104 |
return api.get( { test: [ 'foo', 'bar', 'baz' ] } ); |
| 105 |
} ); |
| 106 |
|
| 107 |
QUnit.test( 'Converting arrays to pipe-separated (mw.Title)', function ( assert ) { |
| 108 |
const api = new mw.Api(); |
| 109 |
|
| 110 |
this.server.respond( ( request ) => { |
| 111 |
assert.strictEqual( match( request.url, /test=([^&]+)/ ), 'Foo%7CBar', 'Pipe-separated value was submitted' ); |
| 112 |
request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); |
| 113 |
} ); |
| 114 |
|
| 115 |
return api.get( { test: [ new mw.Title( 'Foo' ), new mw.Title( 'Bar' ) ] } ); |
| 116 |
} ); |
| 117 |
|
| 118 |
QUnit.test( 'Converting arrays to pipe-separated (misc primitives)', function ( assert ) { |
| 119 |
const api = new mw.Api(); |
| 120 |
|
| 121 |
this.server.respond( ( request ) => { |
| 122 |
assert.strictEqual( match( request.url, /test=([^&]+)/ ), 'true%7Cfalse%7C%7C%7C0%7C1.2', 'Pipe-separated value was submitted' ); |
| 123 |
request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); |
| 124 |
} ); |
| 125 |
|
| 126 |
|
| 127 |
return api.get( { test: [ true, false, undefined, null, 0, 1.2 ] } ); |
| 128 |
} ); |
| 129 |
|
| 130 |
QUnit.test( 'Omitting false booleans', function ( assert ) { |
| 131 |
const api = new mw.Api(); |
| 132 |
|
| 133 |
this.server.respond( ( request ) => { |
| 134 |
assert.false( /foo/.test( request.url ), 'foo query parameter is not present' ); |
| 135 |
assert.true( /bar=true/.test( request.url ), 'bar query parameter is present with value true' ); |
| 136 |
request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); |
| 137 |
} ); |
| 138 |
|
| 139 |
return api.get( { foo: false, bar: true } ); |
| 140 |
} ); |
| 141 |
|
| 142 |
QUnit.test( 'getToken() - cached', async function ( assert ) { |
| 143 |
mw.Api.resetTokenCacheForTest(); |
| 144 |
|
| 145 |
const api = new mw.Api(); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
const token = await api.getToken( 'csrf' ); |
| 150 |
assert.true( token.length > 0, 'token' ); |
| 151 |
|
| 152 |
assert.strictEqual( this.server.requests.length, 0, 'Requests made' ); |
| 153 |
} ); |
| 154 |
|
| 155 |
QUnit.test( 'getToken() - uncached', async function ( assert ) { |
| 156 |
const api = new mw.Api(); |
| 157 |
|
| 158 |
this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' }, |
| 159 |
'{ "query": { "tokens": { "testuncachedtoken": "good" } } }' |
| 160 |
] ); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
const token = await api.getToken( 'testuncached' ); |
| 166 |
assert.strictEqual( token, 'good', 'token' ); |
| 167 |
|
| 168 |
const token2 = await api.getToken( 'testuncached' ); |
| 169 |
assert.strictEqual( token2, 'good', 'token2' ); |
| 170 |
|
| 171 |
assert.strictEqual( this.server.requests.length, 1, 'Requests made' ); |
| 172 |
} ); |
| 173 |
|
| 174 |
QUnit.test( 'getToken() - error', function ( assert ) { |
| 175 |
const api = new mw.Api(); |
| 176 |
|
| 177 |
this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, |
| 178 |
[ |
| 179 |
'{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }', |
| 180 |
'{ "query": { "tokens": { "testerrortoken": "good" } } }' |
| 181 |
] |
| 182 |
) ); |
| 183 |
|
| 184 |
|
| 185 |
return api.getToken( 'testerror' ) |
| 186 |
.catch( ( err ) => { |
| 187 |
assert.strictEqual( err, 'bite-me', 'Expected error' ); |
| 188 |
|
| 189 |
return api.getToken( 'testerror' ); |
| 190 |
} ) |
| 191 |
.then( ( token ) => { |
| 192 |
assert.strictEqual( token, 'good', 'The token' ); |
| 193 |
} ); |
| 194 |
} ); |
| 195 |
|
| 196 |
QUnit.test( 'getToken() - no query', function ( assert ) { |
| 197 |
const api = new mw.Api(), |
| 198 |
|
| 199 |
serverRsp = { |
| 200 |
warnings: { |
| 201 |
tokens: { |
| 202 |
'*': 'Tokens may not be obtained when the same-origin policy is not applied.' |
| 203 |
} |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
this.server.respondWith( /type=testnoquery/, [ 200, { 'Content-Type': 'application/json' }, |
| 208 |
JSON.stringify( serverRsp ) |
| 209 |
] ); |
| 210 |
|
| 211 |
return api.getToken( 'testnoquery' ) |
| 212 |
.then( () => { |
| 213 |
assert.fail( 'Expected response missing a query to be rejected' ); |
| 214 |
} ) |
| 215 |
.catch( ( err, rsp ) => { |
| 216 |
assert.strictEqual( err, 'query-missing', 'Expected no query error code' ); |
| 217 |
assert.deepEqual( rsp, serverRsp ); |
| 218 |
} ); |
| 219 |
} ); |
| 220 |
|
| 221 |
QUnit.test( 'getToken() - deprecated', async function ( assert ) { |
| 222 |
|
| 223 |
const api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ); |
| 224 |
|
| 225 |
this.server.respondWith( /type=csrf/, [ 200, { 'Content-Type': 'application/json' }, |
| 226 |
'{ "query": { "tokens": { "csrftoken": "csrfgood" } } }' |
| 227 |
] ); |
| 228 |
|
| 229 |
|
| 230 |
const token = await api.getToken( 'email' ); |
| 231 |
assert.strictEqual( token, 'csrfgood', 'Token' ); |
| 232 |
|
| 233 |
assert.strictEqual( this.server.requests.length, 1, 'Requests made' ); |
| 234 |
} ); |
| 235 |
|
| 236 |
QUnit.test( 'badToken()', async function ( assert ) { |
| 237 |
const api = new mw.Api(); |
| 238 |
|
| 239 |
this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, |
| 240 |
[ |
| 241 |
'{ "query": { "tokens": { "testbadtoken": "bad" } } }', |
| 242 |
'{ "query": { "tokens": { "testbadtoken": "good" } } }' |
| 243 |
] |
| 244 |
) ); |
| 245 |
|
| 246 |
await api.getToken( 'testbad' ); |
| 247 |
api.badToken( 'testbad' ); |
| 248 |
|
| 249 |
const token = await api.getToken( 'testbad' ); |
| 250 |
assert.strictEqual( token, 'good', 'The token' ); |
| 251 |
assert.strictEqual( this.server.requests.length, 2, 'Requests made' ); |
| 252 |
} ); |
| 253 |
|
| 254 |
QUnit.test( 'badToken( legacy )', async function ( assert ) { |
| 255 |
const api = new mw.Api( { ajax: { url: '/badTokenLegacy/api.php' } } ); |
| 256 |
|
| 257 |
this.server.respondWith( /type=csrf/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, |
| 258 |
[ |
| 259 |
'{ "query": { "tokens": { "csrftoken": "badlegacy" } } }', |
| 260 |
'{ "query": { "tokens": { "csrftoken": "goodlegacy" } } }' |
| 261 |
] |
| 262 |
) ); |
| 263 |
|
| 264 |
await api.getToken( 'options' ); |
| 265 |
api.badToken( 'options' ); |
| 266 |
|
| 267 |
const token = await api.getToken( 'options' ); |
| 268 |
assert.strictEqual( token, 'goodlegacy', 'The token' ); |
| 269 |
assert.strictEqual( this.server.requests.length, 2, 'Request made' ); |
| 270 |
} ); |
| 271 |
|
| 272 |
QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) { |
| 273 |
const api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ); |
| 274 |
|
| 275 |
this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' }, |
| 276 |
'{ "query": { "tokens": { "testposttoken": "good" } } }' |
| 277 |
] ); |
| 278 |
this.server.respondWith( 'POST', /api/, ( request ) => { |
| 279 |
if ( request.requestBody.includes( 'token=good' ) ) { |
| 280 |
request.respond( 200, { 'Content-Type': 'application/json' }, |
| 281 |
'{ "example": { "foo": "quux" } }' |
| 282 |
); |
| 283 |
} |
| 284 |
} ); |
| 285 |
|
| 286 |
return api.postWithToken( 'testpost', { action: 'example', key: 'foo' } ) |
| 287 |
.then( ( data ) => { |
| 288 |
assert.deepEqual( data, { example: { foo: 'quux' } } ); |
| 289 |
} ); |
| 290 |
} ); |
| 291 |
|
| 292 |
QUnit.test( 'postWithToken( tokenType, params with assert )', async function ( assert ) { |
| 293 |
const api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ); |
| 294 |
|
| 295 |
this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' }, |
| 296 |
'{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }' |
| 297 |
] ); |
| 298 |
|
| 299 |
await assert.rejects( |
| 300 |
api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } ), |
| 301 |
/assertuserfailed/, |
| 302 |
'errorcode' |
| 303 |
); |
| 304 |
} ); |
| 305 |
|
| 306 |
QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', async function ( assert ) { |
| 307 |
const api = new mw.Api(); |
| 308 |
|
| 309 |
this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] ); |
| 310 |
|
| 311 |
await api.postWithToken( 'csrf', |
| 312 |
{ action: 'example' }, |
| 313 |
{ |
| 314 |
headers: { |
| 315 |
'X-Foo': 'Bar' |
| 316 |
} |
| 317 |
} |
| 318 |
); |
| 319 |
assert.strictEqual( this.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' ); |
| 320 |
|
| 321 |
let called = 0; |
| 322 |
const data = await api.postWithToken( 'csrf', |
| 323 |
{ action: 'example' }, |
| 324 |
() => { |
| 325 |
called++; |
| 326 |
} |
| 327 |
); |
| 328 |
assert.strictEqual( called, 0, 'parameter cannot be a callback' ); |
| 329 |
assert.propEqual( data, { example: 'quux' }, 'data' ); |
| 330 |
assert.strictEqual( this.server.requests.length, 2, 'Request made' ); |
| 331 |
} ); |
| 332 |
|
| 333 |
QUnit.test( 'postWithToken() - badtoken', function ( assert ) { |
| 334 |
const api = new mw.Api(); |
| 335 |
|
| 336 |
this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, |
| 337 |
[ |
| 338 |
'{ "query": { "tokens": { "testbadtokentoken": "bad" } } }', |
| 339 |
'{ "query": { "tokens": { "testbadtokentoken": "good" } } }' |
| 340 |
] |
| 341 |
) ); |
| 342 |
this.server.respondWith( 'POST', /api/, ( request ) => { |
| 343 |
if ( request.requestBody.includes( 'token=bad' ) ) { |
| 344 |
request.respond( 200, { 'Content-Type': 'application/json' }, |
| 345 |
'{ "error": { "code": "badtoken" } }' |
| 346 |
); |
| 347 |
} |
| 348 |
if ( request.requestBody.includes( 'token=good' ) ) { |
| 349 |
request.respond( 200, { 'Content-Type': 'application/json' }, |
| 350 |
'{ "example": { "foo": "quux" } }' |
| 351 |
); |
| 352 |
} |
| 353 |
} ); |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
return api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } ) |
| 360 |
.then( ( data ) => { |
| 361 |
assert.deepEqual( data, { example: { foo: 'quux' } } ); |
| 362 |
} ); |
| 363 |
} ); |
| 364 |
|
| 365 |
QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) { |
| 366 |
const api = new mw.Api(); |
| 367 |
|
| 368 |
this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, |
| 369 |
[ |
| 370 |
'{ "query": { "tokens": { "testoncetoken": "good-A" } } }', |
| 371 |
'{ "query": { "tokens": { "testoncetoken": "good-B" } } }' |
| 372 |
] |
| 373 |
) ); |
| 374 |
const sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' }, |
| 375 |
[ |
| 376 |
'{ "example": { "value": "A" } }', |
| 377 |
'{ "error": { "code": "badtoken" } }' |
| 378 |
] |
| 379 |
); |
| 380 |
this.server.respondWith( 'POST', /api/, ( request ) => { |
| 381 |
if ( request.requestBody.includes( 'token=good-A' ) ) { |
| 382 |
sequenceA( request ); |
| 383 |
} else if ( request.requestBody.includes( 'token=good-B' ) ) { |
| 384 |
request.respond( 200, { 'Content-Type': 'application/json' }, |
| 385 |
'{ "example": { "value": "B" } }' |
| 386 |
); |
| 387 |
} |
| 388 |
} ); |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
return api.postWithToken( 'testonce', { action: 'example', key: 'foo' } ) |
| 393 |
.then( ( data ) => { |
| 394 |
assert.deepEqual( data, { example: { value: 'A' } } ); |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
return api.postWithToken( 'testonce', { action: 'example', key: 'bar' } ); |
| 400 |
} ) |
| 401 |
.then( ( data ) => { |
| 402 |
assert.deepEqual( data, { example: { value: 'B' } } ); |
| 403 |
} ); |
| 404 |
} ); |
| 405 |
|
| 406 |
QUnit.test( '#abort', function ( assert ) { |
| 407 |
this.sandbox.stub( $, 'ajax', ( options ) => $.Deferred().promise( { |
| 408 |
abort: function () { |
| 409 |
assert.step( options.type + ' aborted' ); |
| 410 |
} |
| 411 |
} ) ); |
| 412 |
const api = new mw.Api(); |
| 413 |
api.get( { a: 1 } ); |
| 414 |
api.post( { b: 2 } ); |
| 415 |
api.abort(); |
| 416 |
|
| 417 |
assert.verifySteps( [ |
| 418 |
'GET aborted', |
| 419 |
'POST aborted' |
| 420 |
] ); |
| 421 |
} ); |
| 422 |
} ); |