summaryrefslogtreecommitdiff
path: root/tests/qunit/suites/resources/mediawiki.api
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-05-01 15:32:59 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-05-01 15:32:59 -0400
commit6dc1997577fab2c366781fd7048144935afa0012 (patch)
tree8918d28c7ab4342f0738985e37af1dfc42d0e93a /tests/qunit/suites/resources/mediawiki.api
parent150f94f051128f367bc89f6b7e5f57eb2a69fc62 (diff)
parentfa89acd685cb09cdbe1c64cbb721ec64975bbbc1 (diff)
Merge commit 'fa89acd'
# Conflicts: # .gitignore # extensions/ArchInterWiki.sql
Diffstat (limited to 'tests/qunit/suites/resources/mediawiki.api')
-rw-r--r--tests/qunit/suites/resources/mediawiki.api/mediawiki.ForeignApi.test.js39
-rw-r--r--tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js346
-rw-r--r--tests/qunit/suites/resources/mediawiki.api/mediawiki.api.upload.test.js35
-rw-r--r--tests/qunit/suites/resources/mediawiki.api/mediawiki.api.watch.test.js6
4 files changed, 285 insertions, 141 deletions
diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.ForeignApi.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.ForeignApi.test.js
new file mode 100644
index 00000000..9d0fdf54
--- /dev/null
+++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.ForeignApi.test.js
@@ -0,0 +1,39 @@
+( function ( mw ) {
+ QUnit.module( 'mediawiki.ForeignApi', QUnit.newMwEnvironment( {
+ setup: function () {
+ this.server = this.sandbox.useFakeServer();
+ this.server.respondImmediately = true;
+ this.clock = this.sandbox.useFakeTimers();
+ },
+ teardown: function () {
+ // https://github.com/jquery/jquery/issues/2453
+ this.clock.tick();
+ }
+ } ) );
+
+ QUnit.test( 'origin is included in GET requests', function ( assert ) {
+ QUnit.expect( 1 );
+ var api = new mw.ForeignApi( '//localhost:4242/w/api.php' );
+
+ this.server.respond( function ( request ) {
+ assert.ok( request.url.match( /origin=/ ), 'origin is included in GET requests' );
+ request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
+ } );
+
+ api.get( {} );
+ } );
+
+ QUnit.test( 'origin is included in POST requests', function ( assert ) {
+ QUnit.expect( 2 );
+ var api = new mw.ForeignApi( '//localhost:4242/w/api.php' );
+
+ this.server.respond( function ( request ) {
+ assert.ok( request.requestBody.match( /origin=/ ), 'origin is included in POST request body' );
+ assert.ok( request.url.match( /origin=/ ), 'origin is included in POST request URL, too' );
+ request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
+ } );
+
+ api.post( {} );
+ } );
+
+}( mediaWiki ) );
diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
index b89526fb..56a346fb 100644
--- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
+++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
@@ -1,15 +1,40 @@
-( function ( mw ) {
+( function ( mw, $ ) {
QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
setup: function () {
this.server = this.sandbox.useFakeServer();
+ this.server.respondImmediately = true;
+ this.clock = this.sandbox.useFakeTimers();
+ },
+ teardown: function () {
+ // https://github.com/jquery/jquery/issues/2453
+ this.clock.tick();
}
} ) );
+ function sequence( responses ) {
+ var i = 0;
+ return function ( request ) {
+ var response = responses[ i ];
+ if ( response ) {
+ i++;
+ request.respond.apply( request, response );
+ }
+ };
+ }
+
+ function sequenceBodies( status, headers, bodies ) {
+ jQuery.each( bodies, function ( i, body ) {
+ bodies[ i ] = [ status, headers, body ];
+ } );
+ return sequence( bodies );
+ }
+
QUnit.test( 'Basic functionality', function ( assert ) {
QUnit.expect( 2 );
-
var api = new mw.Api();
+ this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] );
+
api.get( {} )
.done( function ( data ) {
assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
@@ -19,36 +44,26 @@
.done( function ( data ) {
assert.deepEqual( data, [], 'Simple POST request' );
} );
-
- this.server.respond( function ( request ) {
- request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
- } );
} );
QUnit.test( 'API error', function ( assert ) {
QUnit.expect( 1 );
-
var api = new mw.Api();
+ this.server.respond( [ 200, { 'Content-Type': 'application/json' },
+ '{ "error": { "code": "unknown_action" } }'
+ ] );
+
api.get( { action: 'doesntexist' } )
.fail( function ( errorCode ) {
assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
} );
-
- this.server.respond( function ( request ) {
- request.respond( 200, { 'Content-Type': 'application/json' },
- '{ "error": { "code": "unknown_action" } }'
- );
- } );
} );
QUnit.test( 'FormData support', function ( assert ) {
QUnit.expect( 2 );
-
var api = new mw.Api();
- api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
-
this.server.respond( function ( request ) {
if ( window.FormData ) {
assert.ok( !request.url.match( /action=/ ), 'Request has no query string' );
@@ -59,27 +74,41 @@
}
request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
} );
+
+ api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
} );
QUnit.test( 'Converting arrays to pipe-separated', function ( assert ) {
QUnit.expect( 1 );
-
var api = new mw.Api();
- api.get( { test: [ 'foo', 'bar', 'baz' ] } );
this.server.respond( function ( request ) {
assert.ok( request.url.match( /test=foo%7Cbar%7Cbaz/ ), 'Pipe-separated value was submitted' );
request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
} );
+
+ api.get( { test: [ 'foo', 'bar', 'baz' ] } );
} );
- QUnit.test( 'getToken( pre-populated )', function ( assert ) {
+ QUnit.test( 'Omitting false booleans', function ( assert ) {
QUnit.expect( 2 );
+ var api = new mw.Api();
+
+ this.server.respond( function ( request ) {
+ assert.ok( !request.url.match( /foo/ ), 'foo query parameter is not present' );
+ assert.ok( request.url.match( /bar=true/ ), 'bar query parameter is present with value true' );
+ request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
+ } );
+ api.get( { foo: false, bar: true } );
+ } );
+
+ QUnit.test( 'getToken() - cached', function ( assert ) {
+ QUnit.expect( 2 );
var api = new mw.Api();
// Get editToken for local wiki, this should not make
- // a request as it should be retrieved from user.tokens.
+ // a request as it should be retrieved from mw.user.tokens.
api.getToken( 'edit' )
.done( function ( token ) {
assert.ok( token.length, 'Got a token' );
@@ -91,112 +120,128 @@
assert.equal( this.server.requests.length, 0, 'Requests made' );
} );
- QUnit.test( 'getToken()', function ( assert ) {
- QUnit.expect( 5 );
+ QUnit.test( 'getToken() - uncached', function ( assert ) {
+ QUnit.expect( 3 );
+ var api = new mw.Api();
- var test = this,
- api = new mw.Api();
+ this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' },
+ '{ "tokens": { "testuncachedtoken": "good" } }'
+ ] );
// Get a token of a type that isn't prepopulated by user.tokens.
// Could use "block" or "delete" here, but those could in theory
// be added to user.tokens, use a fake one instead.
- api.getToken( 'testaction' )
+ api.getToken( 'testuncached' )
.done( function ( token ) {
- assert.ok( token.length, 'Got testaction token' );
+ assert.equal( token, 'good', 'The token' );
} )
.fail( function ( err ) {
assert.equal( err, '', 'API error' );
} );
- api.getToken( 'testaction' )
+
+ api.getToken( 'testuncached' )
.done( function ( token ) {
- assert.ok( token.length, 'Got testaction token (cached)' );
+ assert.equal( token, 'good', 'The cached token' );
} )
.fail( function ( err ) {
assert.equal( err, '', 'API error' );
} );
+ assert.equal( this.server.requests.length, 1, 'Requests made' );
+ } );
+
+ QUnit.test( 'getToken() - error', function ( assert ) {
+ QUnit.expect( 2 );
+ var api = new mw.Api();
+
+ this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
+ [
+ '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }',
+ '{ "tokens": { "testerrortoken": "good" } }'
+ ]
+ ) );
+
// Don't cache error (bug 65268)
- api.getToken( 'testaction2' )
- .fail( function ( err ) {
- assert.equal( err, 'bite-me', 'Expected error' );
- } )
- .always( function () {
- // Make this request after the first one has finished.
- // If we make it simultaneously we still want it to share
- // the cache, but as soon as it is fulfilled as error we
- // reject it so that the next one tries fresh.
- api.getToken( 'testaction2' )
- .done( function ( token ) {
- assert.ok( token.length, 'Got testaction2 token (error was not be cached)' );
- } )
- .fail( function ( err ) {
- assert.equal( err, '', 'API error' );
- } );
-
- assert.equal( test.server.requests.length, 3, 'Requests made' );
-
- test.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
- '{ "tokens": { "testaction2token": "0123abc" } }'
- );
+ api.getToken( 'testerror' ).fail( function ( err ) {
+ assert.equal( err, 'bite-me', 'Expected error' );
+
+ // Make this request after the first one has finished.
+ // If we make it simultaneously we still want it to share
+ // the cache, but as soon as it is fulfilled as error we
+ // reject it so that the next one tries fresh.
+ api.getToken( 'testerror' ).done( function ( token ) {
+ assert.equal( token, 'good', 'The token' );
} );
+ } );
+ } );
- this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
- '{ "tokens": { "testactiontoken": "0123abc" } }'
- );
+ QUnit.test( 'badToken()', function ( assert ) {
+ QUnit.expect( 2 );
+ var api = new mw.Api(),
+ test = this;
+
+ this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
+ [
+ '{ "tokens": { "testbadtoken": "bad" } }',
+ '{ "tokens": { "testbadtoken": "good" } }'
+ ]
+ ) );
+
+ api.getToken( 'testbad' )
+ .then( function () {
+ api.badToken( 'testbad' );
+ return api.getToken( 'testbad' );
+ } )
+ .then( function ( token ) {
+ assert.equal( token, 'good', 'The token' );
+ assert.equal( test.server.requests.length, 2, 'Requests made' );
+ } );
- this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
- '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }'
- );
} );
QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) {
QUnit.expect( 1 );
-
var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
- // - Requests token
- // - Performs action=example
- api.postWithToken( 'testsimpletoken', { action: 'example', key: 'foo' } )
+ this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' },
+ '{ "tokens": { "testposttoken": "good" } }'
+ ] );
+ this.server.respondWith( 'POST', /api/, function ( request ) {
+ if ( request.requestBody.match( /token=good/ ) ) {
+ request.respond( 200, { 'Content-Type': 'application/json' },
+ '{ "example": { "foo": "quux" } }'
+ );
+ }
+ } );
+
+ api.postWithToken( 'testpost', { action: 'example', key: 'foo' } )
.done( function ( data ) {
assert.deepEqual( data, { example: { foo: 'quux' } } );
} );
-
- this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
- '{ "tokens": { "testsimpletokentoken": "a-bad-token" } }'
- );
-
- this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
- '{ "example": { "foo": "quux" } }'
- );
} );
QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) {
QUnit.expect( 2 );
-
var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
- api.postWithToken( 'testasserttoken', { action: 'example', key: 'foo', assert: 'user' } )
+ this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' },
+ '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
+ ] );
+
+ api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } )
.fail( function ( errorCode ) {
assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' );
} );
- assert.equal( this.server.requests.length, 1, 'Request for token made' );
- this.server.respondWith( /assert=user/, function ( request ) {
- request.respond(
- 200,
- { 'Content-Type': 'application/json' },
- '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }'
- );
- } );
-
- this.server.respond();
+ assert.equal( this.server.requests.length, 1, 'Requests made' );
} );
QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) {
QUnit.expect( 3 );
-
var api = new mw.Api();
+ this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] );
+
api.postWithToken(
'edit',
{
@@ -223,86 +268,111 @@
} );
assert.equal( this.server.requests.length, 2, 'Request made' );
- assert.equal( this.server.requests[0].requestHeaders['X-Foo'], 'Bar', 'Header sent' );
-
- this.server.respond( function ( request ) {
- request.respond( 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' );
- } );
+ assert.equal( this.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' );
} );
QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
QUnit.expect( 1 );
-
var api = new mw.Api();
- // - Request: token
+ this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
+ [
+ '{ "tokens": { "testbadtokentoken": "bad" } }',
+ '{ "tokens": { "testbadtokentoken": "good" } }'
+ ]
+ ) );
+ this.server.respondWith( 'POST', /api/, function ( request ) {
+ if ( request.requestBody.match( /token=bad/ ) ) {
+ request.respond( 200, { 'Content-Type': 'application/json' },
+ '{ "error": { "code": "badtoken" } }'
+ );
+ }
+ if ( request.requestBody.match( /token=good/ ) ) {
+ request.respond( 200, { 'Content-Type': 'application/json' },
+ '{ "example": { "foo": "quux" } }'
+ );
+ }
+ } );
+
+ // - Request: new token -> bad
// - Request: action=example -> badtoken error
- // - Request: new token
- // - Request: action=example
+ // - Request: new token -> good
+ // - Request: action=example -> success
api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
.done( function ( data ) {
assert.deepEqual( data, { example: { foo: 'quux' } } );
} );
-
- this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
- '{ "tokens": { "testbadtokentoken": "a-bad-token" } }'
- );
-
- this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
- '{ "error": { "code": "badtoken" } }'
- );
-
- this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
- '{ "tokens": { "testbadtokentoken": "a-good-token" } }'
- );
-
- this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' },
- '{ "example": { "foo": "quux" } }'
- );
-
} );
QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
QUnit.expect( 2 );
+ var sequenceA,
+ api = new mw.Api();
- var api = new mw.Api();
+ this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' },
+ [
+ '{ "tokens": { "testoncetoken": "good-A" } }',
+ '{ "tokens": { "testoncetoken": "good-B" } }'
+ ]
+ ) );
+ sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' },
+ [
+ '{ "example": { "value": "A" } }',
+ '{ "error": { "code": "badtoken" } }'
+ ]
+ );
+ this.server.respondWith( 'POST', /api/, function ( request ) {
+ if ( request.requestBody.match( /token=good-A/ ) ) {
+ sequenceA( request );
+ } else if ( request.requestBody.match( /token=good-B/ ) ) {
+ request.respond( 200, { 'Content-Type': 'application/json' },
+ '{ "example": { "value": "B" } }'
+ );
+ }
+ } );
- // - Request: token
+ // - Request: new token -> A
// - Request: action=example
- api.postWithToken( 'testbadtokencache', { action: 'example', key: 'foo' } )
+ api.postWithToken( 'testonce', { action: 'example', key: 'foo' } )
.done( function ( data ) {
- assert.deepEqual( data, { example: { foo: 'quux' } } );
+ assert.deepEqual( data, { example: { value: 'A' } } );
} );
- // - Cache: Try previously cached token
- // - Request: action=example -> badtoken error
- // - Request: new token
- // - Request: action=example
- api.postWithToken( 'testbadtokencache', { action: 'example', key: 'bar' } )
+ // - Request: action=example w/ token A -> badtoken error
+ // - Request: new token -> B
+ // - Request: action=example w/ token B -> success
+ api.postWithToken( 'testonce', { action: 'example', key: 'bar' } )
.done( function ( data ) {
- assert.deepEqual( data, { example: { bar: 'quux' } } );
+ assert.deepEqual( data, { example: { value: 'B' } } );
} );
+ } );
- this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
- '{ "tokens": { "testbadtokencachetoken": "a-good-token-once" } }'
- );
-
- this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
- '{ "example": { "foo": "quux" } }'
- );
-
- this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
- '{ "error": { "code": "badtoken" } }'
- );
-
- this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' },
- '{ "tokens": { "testbadtokencachetoken": "a-good-new-token" } }'
- );
-
- this.server.requests[4].respond( 200, { 'Content-Type': 'application/json' },
- '{ "example": { "bar": "quux" } }'
- );
-
+ QUnit.module( 'mediawiki.api (2)', {
+ setup: function () {
+ var self = this,
+ requests = this.requests = [];
+ this.api = new mw.Api();
+ this.sandbox.stub( jQuery, 'ajax', function () {
+ var request = $.extend( {
+ abort: self.sandbox.spy()
+ }, $.Deferred() );
+ requests.push( request );
+ return request;
+ } );
+ }
} );
-}( mediaWiki ) );
+ QUnit.test( '#abort', 3, function ( assert ) {
+ this.api.get( {
+ a: 1
+ } );
+ this.api.post( {
+ b: 2
+ } );
+ this.api.abort();
+ assert.ok( this.requests.length === 2, 'Check both requests triggered' );
+ $.each( this.requests, function ( i, request ) {
+ assert.ok( request.abort.calledOnce, 'abort request number ' + i );
+ } );
+ } );
+}( mediaWiki, jQuery ) );
diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.upload.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.upload.test.js
new file mode 100644
index 00000000..10fcd5da
--- /dev/null
+++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.upload.test.js
@@ -0,0 +1,35 @@
+( function ( mw, $ ) {
+ QUnit.module( 'mediawiki.api.upload', QUnit.newMwEnvironment( {} ) );
+
+ QUnit.test( 'Basic functionality', function ( assert ) {
+ QUnit.expect( 2 );
+ var api = new mw.Api();
+ assert.ok( api.upload );
+ assert.throws( function () {
+ api.upload();
+ } );
+ } );
+
+ QUnit.test( 'Set up iframe upload', function ( assert ) {
+ QUnit.expect( 5 );
+ var $iframe, $form, $input,
+ api = new mw.Api();
+
+ this.sandbox.stub( api, 'getEditToken', function () {
+ return $.Deferred().promise();
+ } );
+
+ api.uploadWithIframe( $( '<input>' )[ 0 ], { filename: 'Testing API upload.jpg' } );
+
+ $iframe = $( 'iframe' );
+ $form = $( 'form.mw-api-upload-form' );
+ $input = $form.find( 'input[name=filename]' );
+
+ assert.ok( $form.length > 0 );
+ assert.ok( $input.length > 0 );
+ assert.ok( $iframe.length > 0 );
+ assert.strictEqual( $form.prop( 'target' ), $iframe.prop( 'id' ) );
+ assert.strictEqual( $input.val(), 'Testing API upload.jpg' );
+ } );
+
+}( mediaWiki, jQuery ) );
diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.watch.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.watch.test.js
index 5965ab7b..64a51847 100644
--- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.watch.test.js
+++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.watch.test.js
@@ -17,12 +17,12 @@
} );
api.watch( [ 'Foo' ] ).done( function ( items ) {
- assert.equal( items[0].title, 'Foo' );
+ assert.equal( items[ 0 ].title, 'Foo' );
} );
api.watch( [ 'Foo', 'Bar' ] ).done( function ( items ) {
- assert.equal( items[0].title, 'Foo' );
- assert.equal( items[1].title, 'Bar' );
+ assert.equal( items[ 0 ].title, 'Foo' );
+ assert.equal( items[ 1 ].title, 'Bar' );
} );
// Requests are POST, match requestBody instead of url