Skip to content

Commit c159b3d

Browse files
committed
Fix for issue angular#736: ngResource now accepts headers
1 parent 301d8f2 commit c159b3d

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/ngResource/resource.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
3737
* default set of resource actions. The declaration should be created in the following format:
3838
*
39-
* {action1: {method:?, params:?, isArray:?},
40-
* action2: {method:?, params:?, isArray:?},
39+
* {action1: {method:?, params:?, isArray:?, headers:?},
40+
* action2: {method:?, params:?, isArray:?, headers:?},
4141
* ...}
4242
*
4343
* Where:
@@ -49,6 +49,7 @@
4949
* - `params` – {object=} – Optional set of pre-bound parameters for this action.
5050
* - isArray – {boolean=} – If true then the returned object for this action is an array, see
5151
* `returns` section.
52+
* - `headers` – {object=} – Optional HTTP headers to send
5253
*
5354
* @returns {Object} A resource "class" object with methods for the default set of resource actions
5455
* optionally extended with custom `actions`. The default set contains these actions:
@@ -130,7 +131,7 @@
130131
* The object returned from this function execution is a resource "class" which has "static" method
131132
* for each action in the definition.
132133
*
133-
* Calling these methods invoke `$http` on the `url` template with the given `method` and `params`.
134+
* Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and `headers`.
134135
* When the data is returned from the server then the object is an instance of the resource type and
135136
* all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD
136137
* operations (create, read, update, delete) on server-side data.
@@ -362,7 +363,8 @@ angular.module('ngResource', ['ng']).
362363
$http({
363364
method: action.method,
364365
url: route.url(extend({}, extractParams(data), action.params || {}, params)),
365-
data: data
366+
data: data,
367+
headers: extend({}, action.headers || {})
366368
}).then(function(response) {
367369
var data = response.data;
368370

@@ -417,4 +419,4 @@ angular.module('ngResource', ['ng']).
417419
}
418420

419421
return ResourceFactory;
420-
}]);
422+
}]);

test/ngResource/resourceSpec.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ describe("resource", function() {
1414
},
1515
patch: {
1616
method: 'PATCH'
17+
},
18+
conditionalPut: {
19+
method: 'PUT',
20+
headers: {
21+
'If-None-Match': '*'
22+
}
1723
}
24+
1825
});
1926
callback = jasmine.createSpy();
2027
}));
@@ -56,7 +63,6 @@ describe("resource", function() {
5663
R.get({a:4, b:5, c:6});
5764
});
5865

59-
6066
it('should support escaping colons in url template', function() {
6167
var R = $resource('http://localhost\\:8080/Path/:a/\\:stillPath/:b');
6268

@@ -145,7 +151,14 @@ describe("resource", function() {
145151
expect(callback.mostRecentCall.args[1]()).toEqual({});
146152
});
147153

148-
154+
it('should send correct headers', function() {
155+
$httpBackend.expectPUT('/CreditCard/123', undefined, function(headers) {
156+
return headers['If-None-Match'] == "*";
157+
}).respond({id:123});
158+
159+
CreditCard.conditionalPut({id: {key:123}});
160+
});
161+
149162
it("should read partial resource", function() {
150163
$httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]);
151164
var ccs = CreditCard.query();
@@ -340,4 +353,4 @@ describe("resource", function() {
340353
expect(callback).not.toHaveBeenCalled();
341354
});
342355
});
343-
});
356+
});

0 commit comments

Comments
 (0)