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

Add hasBody to ngResource action configuration #12181

Closed
wants to merge 10 commits into from
Closed
4 changes: 3 additions & 1 deletion src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ function shallowClearAndCopy(src, dst) {
* - **`interceptor`** - `{Object=}` - The interceptor object has two optional methods -
* `response` and `responseError`. Both `response` and `responseError` interceptors get called
* with `http response` object. See {@link ng.$http $http interceptors}.
* - **`hasBody`** - `{boolean}` - allows to specify if a request body should be included or not.
* If not specified only POST, PUT and PATCH will have a request.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only POST, PUT and PATCH will have a request --> only POST, PUT and PATCH requests will have a body

*
* @param {Object} options Hash with custom settings that should extend the
* default `$resourceProvider` behavior. The supported options are:
Expand Down Expand Up @@ -640,7 +642,7 @@ angular.module('ngResource', ['ng']).
};

forEach(actions, function(action, name) {
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method);
var hasBody = action.hasBody === true || (action.hasBody !== false && /^(POST|PUT|PATCH)$/i.test(action.method));
var numericTimeout = action.timeout;
var cancellable = isDefined(action.cancellable) ?
action.cancellable : route.defaults.cancellable;
Expand Down
37 changes: 37 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,43 @@ describe('basic usage', function() {
$httpBackend.flush();
});

it('should include a request body when calling custom delete with hasBody is true', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

custom delete --> custom method

Can you also test with another method (in the same test), just to avoid giving the impression this is just about DELETE.

var condition = {at: '2038-01-19 03:14:08'};
$httpBackend.expect('DELETE', '/fooresource', condition).respond({});

var r = $resource('/fooresource', {}, {
delete: {method: 'DELETE', hasBody: true}
});

var deleteResponse = r.delete(condition);

$httpBackend.flush();

expect(deleteResponse.$resolved).toBe(true);
});

it('should expect a body if hasBody is true', function() {
var username = 'yathos';
var loginRequest = {name: username, password: 'Smile'};
var user = {id: 1, name: username};

$httpBackend.expect('LOGIN', '/user/me', loginRequest).respond(user);

$httpBackend.expect('LOGOUT', '/user/me', null).respond(null);

var UserService = $resource('/user/me', {}, {
login: {method: 'LOGIN', hasBody: true},
logout: {method: 'LOGOUT', hasBody: false}
});

var loginResponse = UserService.login(loginRequest);
var logoutResponse = UserService.logout();

$httpBackend.flush();

expect(loginResponse.id).toBe(user.id);
expect(logoutResponse.$resolved).toBe(true);
});

it('should build resource', function() {
expect(typeof CreditCard).toBe('function');
Expand Down