feat($resource): support custom headers per action

Closes #736
This commit is contained in:
Max Martinsson 2012-05-17 17:11:25 +02:00 committed by Misko Hevery
parent f2b7fffdc0
commit fbdab513dd
2 changed files with 22 additions and 4 deletions

View file

@ -36,8 +36,8 @@
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
* default set of resource actions. The declaration should be created in the following format:
*
* {action1: {method:?, params:?, isArray:?},
* action2: {method:?, params:?, isArray:?},
* {action1: {method:?, params:?, isArray:?, headers:?},
* action2: {method:?, params:?, isArray:?, headers:?},
* ...}
*
* Where:
@ -49,6 +49,7 @@
* - `params` {object=} Optional set of pre-bound parameters for this action.
* - isArray {boolean=} If true then the returned object for this action is an array, see
* `returns` section.
* - `headers` {object=} Optional HTTP headers to send
*
* @returns {Object} A resource "class" object with methods for the default set of resource actions
* optionally extended with custom `actions`. The default set contains these actions:
@ -130,7 +131,7 @@
* The object returned from this function execution is a resource "class" which has "static" method
* for each action in the definition.
*
* Calling these methods invoke `$http` on the `url` template with the given `method` and `params`.
* Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and `headers`.
* When the data is returned from the server then the object is an instance of the resource type and
* all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD
* operations (create, read, update, delete) on server-side data.
@ -362,7 +363,8 @@ angular.module('ngResource', ['ng']).
$http({
method: action.method,
url: route.url(extend({}, extractParams(data), action.params || {}, params)),
data: data
data: data,
headers: extend({}, action.headers || {})
}).then(function(response) {
var data = response.data;

View file

@ -14,7 +14,14 @@ describe("resource", function() {
},
patch: {
method: 'PATCH'
},
conditionalPut: {
method: 'PUT',
headers: {
'If-None-Match': '*'
}
}
});
callback = jasmine.createSpy();
}));
@ -146,6 +153,15 @@ describe("resource", function() {
});
it('should send correct headers', function() {
$httpBackend.expectPUT('/CreditCard/123', undefined, function(headers) {
return headers['If-None-Match'] == "*";
}).respond({id:123});
CreditCard.conditionalPut({id: {key:123}});
});
it("should read partial resource", function() {
$httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]);
var ccs = CreditCard.query();