Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

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 #887
  • Loading branch information
simpulton authored and IgorMinar committed Apr 20, 2012
1 parent ce15a3e commit e61fd1b
Show file tree
Hide file tree
Showing 4 changed files with 50 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
17 changes: 17 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe("resource", function() {
charge:{
method:'POST',
params:{verb:'!charge'}
},
patch: {
method: 'PATCH'
}
});
callback = jasmine.createSpy();
Expand Down Expand Up @@ -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() {
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123}, {header1: 'a'});

Expand Down

0 comments on commit e61fd1b

Please sign in to comment.