mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-19 12:01:07 +00:00
feat($resource): support HTTP PATCH method
Properly serialize data into request body instead of url. Closes #887
This commit is contained in:
parent
ce15a3e049
commit
e61fd1b43a
4 changed files with 50 additions and 5 deletions
30
src/ngMock/angular-mocks.js
vendored
30
src/ngMock/angular-mocks.js
vendored
|
|
@ -1111,6 +1111,20 @@ function createHttpBackendMock($delegate, $browser) {
|
||||||
* request is handled.
|
* request is handled.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc method
|
||||||
|
* @name angular.module.ngMock.$httpBackend#expectPATCH
|
||||||
|
* @methodOf angular.module.ngMock.$httpBackend
|
||||||
|
* @description
|
||||||
|
* Creates a new request expectation for PATCH requests. For more info see `expect()`.
|
||||||
|
*
|
||||||
|
* @param {string|RegExp} url HTTP url.
|
||||||
|
* @param {(string|RegExp)=} data HTTP request body.
|
||||||
|
* @param {Object=} headers HTTP headers.
|
||||||
|
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
|
||||||
|
* request is handled.
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ngdoc method
|
* @ngdoc method
|
||||||
* @name angular.module.ngMock.$httpBackend#expectJSONP
|
* @name angular.module.ngMock.$httpBackend#expectJSONP
|
||||||
|
|
@ -1220,7 +1234,7 @@ function createHttpBackendMock($delegate, $browser) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
angular.forEach(['PUT', 'POST'], function(method) {
|
angular.forEach(['PUT', 'POST', 'PATCH'], function(method) {
|
||||||
$httpBackend[prefix + method] = function(url, data, headers) {
|
$httpBackend[prefix + method] = function(url, data, headers) {
|
||||||
return $httpBackend[prefix](method, url, data, headers)
|
return $httpBackend[prefix](method, url, data, headers)
|
||||||
}
|
}
|
||||||
|
|
@ -1483,6 +1497,20 @@ angular.module('ngMockE2E', ['ng']).config(function($provide) {
|
||||||
* control how a matched request is handled.
|
* control how a matched request is handled.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc method
|
||||||
|
* @name angular.module.ngMockE2E.$httpBackend#whenPATCH
|
||||||
|
* @methodOf angular.module.ngMockE2E.$httpBackend
|
||||||
|
* @description
|
||||||
|
* Creates a new backend definition for PATCH requests. For more info see `when()`.
|
||||||
|
*
|
||||||
|
* @param {string|RegExp} url HTTP url.
|
||||||
|
* @param {(string|RegExp)=} data HTTP request body.
|
||||||
|
* @param {(Object|function(Object))=} headers HTTP headers.
|
||||||
|
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
|
||||||
|
* control how a matched request is handled.
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ngdoc method
|
* @ngdoc method
|
||||||
* @name angular.module.ngMockE2E.$httpBackend#whenJSONP
|
* @name angular.module.ngMockE2E.$httpBackend#whenJSONP
|
||||||
|
|
|
||||||
|
|
@ -318,7 +318,7 @@ angular.module('ngResource', ['ng']).
|
||||||
}
|
}
|
||||||
|
|
||||||
forEach(actions, function(action, name) {
|
forEach(actions, function(action, name) {
|
||||||
var isPostOrPut = action.method == 'POST' || action.method == 'PUT';
|
var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH';
|
||||||
Resource[name] = function(a1, a2, a3, a4) {
|
Resource[name] = function(a1, a2, a3, a4) {
|
||||||
var params = {};
|
var params = {};
|
||||||
var data;
|
var data;
|
||||||
|
|
@ -349,7 +349,7 @@ angular.module('ngResource', ['ng']).
|
||||||
}
|
}
|
||||||
case 1:
|
case 1:
|
||||||
if (isFunction(a1)) success = a1;
|
if (isFunction(a1)) success = a1;
|
||||||
else if (isPostOrPut) data = a1;
|
else if (hasBody) data = a1;
|
||||||
else params = a1;
|
else params = a1;
|
||||||
break;
|
break;
|
||||||
case 0: break;
|
case 0: break;
|
||||||
|
|
@ -409,7 +409,7 @@ angular.module('ngResource', ['ng']).
|
||||||
throw "Expected between 1-3 arguments [params, success, error], got " +
|
throw "Expected between 1-3 arguments [params, success, error], got " +
|
||||||
arguments.length + " arguments.";
|
arguments.length + " arguments.";
|
||||||
}
|
}
|
||||||
var data = isPostOrPut ? this : undefined;
|
var data = hasBody ? this : undefined;
|
||||||
Resource[name].call(this, params, data, success, error);
|
Resource[name].call(this, params, data, success, error);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
2
test/ngMock/angular-mocksSpec.js
vendored
2
test/ngMock/angular-mocksSpec.js
vendored
|
|
@ -814,7 +814,7 @@ describe('ngMock', function() {
|
||||||
|
|
||||||
describe('expect/when shortcuts', function() {
|
describe('expect/when shortcuts', function() {
|
||||||
angular.forEach(['expect', 'when'], function(prefix) {
|
angular.forEach(['expect', 'when'], function(prefix) {
|
||||||
angular.forEach(['GET', 'POST', 'PUT', 'DELETE', 'JSONP'], function(method) {
|
angular.forEach(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'JSONP'], function(method) {
|
||||||
var shortcut = prefix + method;
|
var shortcut = prefix + method;
|
||||||
it('should provide ' + shortcut + ' shortcut method', function() {
|
it('should provide ' + shortcut + ' shortcut method', function() {
|
||||||
hb[shortcut]('/foo').respond('bar');
|
hb[shortcut]('/foo').respond('bar');
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,9 @@ describe("resource", function() {
|
||||||
charge:{
|
charge:{
|
||||||
method:'POST',
|
method:'POST',
|
||||||
params:{verb:'!charge'}
|
params:{verb:'!charge'}
|
||||||
|
},
|
||||||
|
patch: {
|
||||||
|
method: 'PATCH'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
callback = jasmine.createSpy();
|
callback = jasmine.createSpy();
|
||||||
|
|
@ -235,6 +238,20 @@ describe("resource", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it("should patch a resource", function() {
|
||||||
|
$httpBackend.expectPATCH('/CreditCard/123', '{"name":"igor"}').
|
||||||
|
respond({id: 123, name: 'rama'});
|
||||||
|
|
||||||
|
var card = CreditCard.patch({id: 123}, {name: 'igor'}, callback);
|
||||||
|
|
||||||
|
expect(card).toEqualData({name: 'igor'});
|
||||||
|
expect(callback).not.toHaveBeenCalled();
|
||||||
|
$httpBackend.flush();
|
||||||
|
expect(callback).toHaveBeenCalled();
|
||||||
|
expect(card).toEqualData({id: 123, name: 'rama'});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should create on save', function() {
|
it('should create on save', function() {
|
||||||
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123}, {header1: 'a'});
|
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123}, {header1: 'a'});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue