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

Commit e61fd1b

Browse files
simpultonIgorMinar
simpulton
authored andcommitted
feat($resource): support HTTP PATCH method
Properly serialize data into request body instead of url. Closes #887
1 parent ce15a3e commit e61fd1b

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/ngMock/angular-mocks.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,20 @@ function createHttpBackendMock($delegate, $browser) {
11111111
* request is handled.
11121112
*/
11131113

1114+
/**
1115+
* @ngdoc method
1116+
* @name angular.module.ngMock.$httpBackend#expectPATCH
1117+
* @methodOf angular.module.ngMock.$httpBackend
1118+
* @description
1119+
* Creates a new request expectation for PATCH requests. For more info see `expect()`.
1120+
*
1121+
* @param {string|RegExp} url HTTP url.
1122+
* @param {(string|RegExp)=} data HTTP request body.
1123+
* @param {Object=} headers HTTP headers.
1124+
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1125+
* request is handled.
1126+
*/
1127+
11141128
/**
11151129
* @ngdoc method
11161130
* @name angular.module.ngMock.$httpBackend#expectJSONP
@@ -1220,7 +1234,7 @@ function createHttpBackendMock($delegate, $browser) {
12201234
}
12211235
});
12221236

1223-
angular.forEach(['PUT', 'POST'], function(method) {
1237+
angular.forEach(['PUT', 'POST', 'PATCH'], function(method) {
12241238
$httpBackend[prefix + method] = function(url, data, headers) {
12251239
return $httpBackend[prefix](method, url, data, headers)
12261240
}
@@ -1483,6 +1497,20 @@ angular.module('ngMockE2E', ['ng']).config(function($provide) {
14831497
* control how a matched request is handled.
14841498
*/
14851499

1500+
/**
1501+
* @ngdoc method
1502+
* @name angular.module.ngMockE2E.$httpBackend#whenPATCH
1503+
* @methodOf angular.module.ngMockE2E.$httpBackend
1504+
* @description
1505+
* Creates a new backend definition for PATCH requests. For more info see `when()`.
1506+
*
1507+
* @param {string|RegExp} url HTTP url.
1508+
* @param {(string|RegExp)=} data HTTP request body.
1509+
* @param {(Object|function(Object))=} headers HTTP headers.
1510+
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1511+
* control how a matched request is handled.
1512+
*/
1513+
14861514
/**
14871515
* @ngdoc method
14881516
* @name angular.module.ngMockE2E.$httpBackend#whenJSONP

src/ngResource/resource.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ angular.module('ngResource', ['ng']).
318318
}
319319

320320
forEach(actions, function(action, name) {
321-
var isPostOrPut = action.method == 'POST' || action.method == 'PUT';
321+
var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH';
322322
Resource[name] = function(a1, a2, a3, a4) {
323323
var params = {};
324324
var data;
@@ -349,7 +349,7 @@ angular.module('ngResource', ['ng']).
349349
}
350350
case 1:
351351
if (isFunction(a1)) success = a1;
352-
else if (isPostOrPut) data = a1;
352+
else if (hasBody) data = a1;
353353
else params = a1;
354354
break;
355355
case 0: break;
@@ -409,7 +409,7 @@ angular.module('ngResource', ['ng']).
409409
throw "Expected between 1-3 arguments [params, success, error], got " +
410410
arguments.length + " arguments.";
411411
}
412-
var data = isPostOrPut ? this : undefined;
412+
var data = hasBody ? this : undefined;
413413
Resource[name].call(this, params, data, success, error);
414414
};
415415
});

test/ngMock/angular-mocksSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ describe('ngMock', function() {
814814

815815
describe('expect/when shortcuts', function() {
816816
angular.forEach(['expect', 'when'], function(prefix) {
817-
angular.forEach(['GET', 'POST', 'PUT', 'DELETE', 'JSONP'], function(method) {
817+
angular.forEach(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'JSONP'], function(method) {
818818
var shortcut = prefix + method;
819819
it('should provide ' + shortcut + ' shortcut method', function() {
820820
hb[shortcut]('/foo').respond('bar');

test/ngResource/resourceSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ describe("resource", function() {
1111
charge:{
1212
method:'POST',
1313
params:{verb:'!charge'}
14+
},
15+
patch: {
16+
method: 'PATCH'
1417
}
1518
});
1619
callback = jasmine.createSpy();
@@ -235,6 +238,20 @@ describe("resource", function() {
235238
});
236239

237240

241+
it("should patch a resource", function() {
242+
$httpBackend.expectPATCH('/CreditCard/123', '{"name":"igor"}').
243+
respond({id: 123, name: 'rama'});
244+
245+
var card = CreditCard.patch({id: 123}, {name: 'igor'}, callback);
246+
247+
expect(card).toEqualData({name: 'igor'});
248+
expect(callback).not.toHaveBeenCalled();
249+
$httpBackend.flush();
250+
expect(callback).toHaveBeenCalled();
251+
expect(card).toEqualData({id: 123, name: 'rama'});
252+
});
253+
254+
238255
it('should create on save', function() {
239256
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123}, {header1: 'a'});
240257

0 commit comments

Comments
 (0)