Skip to content

Commit

Permalink
feat($resource): support HTTP PATCH method
Browse files Browse the repository at this point in the history
Properly serialize data into request body instead of url.

Closes angular#887
  • Loading branch information
simpulton authored and IgorMinar committed Apr 20, 2012
1 parent ce15a3e commit 00e51fd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
30 changes: 29 additions & 1 deletion src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,20 @@ function createHttpBackendMock($delegate, $browser) {
* 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
* @name angular.module.ngMock.$httpBackend#expectJSONP
Expand Down Expand Up @@ -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) {
return $httpBackend[prefix](method, url, data, headers)
}
Expand Down Expand Up @@ -1483,6 +1497,20 @@ angular.module('ngMockE2E', ['ng']).config(function($provide) {
* 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
* @name angular.module.ngMockE2E.$httpBackend#whenJSONP
Expand Down
6 changes: 3 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ angular.module('ngResource', ['ng']).
}

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) {
var params = {};
var data;
Expand Down Expand Up @@ -349,7 +349,7 @@ angular.module('ngResource', ['ng']).
}
case 1:
if (isFunction(a1)) success = a1;
else if (isPostOrPut) data = a1;
else if (hasBody) data = a1;
else params = a1;
break;
case 0: break;
Expand Down Expand Up @@ -409,7 +409,7 @@ angular.module('ngResource', ['ng']).
throw "Expected between 1-3 arguments [params, success, error], got " +
arguments.length + " arguments.";
}
var data = isPostOrPut ? this : undefined;
var data = hasBody ? this : undefined;
Resource[name].call(this, params, data, success, error);
};
});
Expand Down
2 changes: 1 addition & 1 deletion test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ describe('ngMock', function() {

describe('expect/when shortcuts', function() {
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;
it('should provide ' + shortcut + ' shortcut method', function() {
hb[shortcut]('/foo').respond('bar');
Expand Down

0 comments on commit 00e51fd

Please sign in to comment.