Merge "Add put() and delete() shorthands to rest.js"
This commit is contained in:
commit
254bc1fb1f
2 changed files with 84 additions and 0 deletions
|
|
@ -119,6 +119,48 @@
|
|||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Perform REST API PUT request.
|
||||
*
|
||||
* Note: only sending application/json is currently supported.
|
||||
*
|
||||
* @method
|
||||
* @param {string} path
|
||||
* @param {Object} body
|
||||
* @param {Object} [headers]
|
||||
* @return {jQuery.Promise}
|
||||
*/
|
||||
put: function ( path, body, headers ) {
|
||||
headers = objectKeysToLowerCase( headers );
|
||||
return this.ajax( path, {
|
||||
type: 'PUT',
|
||||
headers: $.extend( headers, { 'content-type': 'application/json' } ),
|
||||
data: JSON.stringify( body ),
|
||||
dataType: 'json'
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Perform REST API DELETE request.
|
||||
*
|
||||
* Note: only sending application/json is currently supported.
|
||||
*
|
||||
* @method
|
||||
* @param {string} path
|
||||
* @param {Object} body
|
||||
* @param {Object} [headers]
|
||||
* @return {jQuery.Promise}
|
||||
*/
|
||||
delete: function ( path, body, headers ) {
|
||||
headers = objectKeysToLowerCase( headers );
|
||||
return this.ajax( path, {
|
||||
type: 'DELETE',
|
||||
headers: $.extend( headers, { 'content-type': 'application/json' } ),
|
||||
data: JSON.stringify( body ),
|
||||
dataType: 'json'
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Perform the API call.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -66,6 +66,48 @@
|
|||
} );
|
||||
} );
|
||||
|
||||
QUnit.test( 'put()', function ( assert ) {
|
||||
var api = new mw.Rest();
|
||||
|
||||
this.server.respond( function ( request ) {
|
||||
assert.strictEqual( request.method, 'PUT', 'Method should be PUT' );
|
||||
assert.true( /rest.php\/test\/bla\/bla\/bla$/.test( request.url ), 'Url should be correct' );
|
||||
assert.true( /^application\/json/.test( request.requestHeaders[ 'Content-Type' ] ), 'Should set JSON content-type' );
|
||||
assert.strictEqual( request.requestHeaders.authorization, 'my_token', 'Should pass request header' );
|
||||
assert.deepEqual( JSON.parse( request.requestBody ), { param: 'value' }, 'Body should be correct' );
|
||||
request.respond( 201, { 'Content-Type': 'application/json' }, '{}' );
|
||||
} );
|
||||
|
||||
return api.put( '/test/bla/bla/bla', {
|
||||
param: 'value'
|
||||
}, {
|
||||
authorization: 'my_token'
|
||||
} ).then( function ( data ) {
|
||||
assert.deepEqual( data, {}, 'If request succeeds without errors, resolve deferred' );
|
||||
} );
|
||||
} );
|
||||
|
||||
QUnit.test( 'delete()', function ( assert ) {
|
||||
var api = new mw.Rest();
|
||||
|
||||
this.server.respond( function ( request ) {
|
||||
assert.strictEqual( request.method, 'DELETE', 'Method should be DELETE' );
|
||||
assert.true( /rest.php\/test\/bla\/bla\/bla$/.test( request.url ), 'Url should be correct' );
|
||||
assert.true( /^application\/json/.test( request.requestHeaders[ 'Content-Type' ] ), 'Should set JSON content-type' );
|
||||
assert.strictEqual( request.requestHeaders.authorization, 'my_token', 'Should pass request header' );
|
||||
assert.deepEqual( JSON.parse( request.requestBody ), { param: 'value' }, 'Body should be correct' );
|
||||
request.respond( 201, { 'Content-Type': 'application/json' }, '{}' );
|
||||
} );
|
||||
|
||||
return api.delete( '/test/bla/bla/bla', {
|
||||
param: 'value'
|
||||
}, {
|
||||
authorization: 'my_token'
|
||||
} ).then( function ( data ) {
|
||||
assert.deepEqual( data, {}, 'If request succeeds without errors, resolve deferred' );
|
||||
} );
|
||||
} );
|
||||
|
||||
QUnit.test( 'http error', function ( assert ) {
|
||||
var api = new mw.Rest();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue