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

Commit a7150f1

Browse files
bolasblackIgorMinar
authored andcommitted
feat($http): accept function as headers value
So we can request with dynamic header value. module.factory('Res', [ '$resource' '$routeParams' 'globalConfig' function($resource, $routeParams, globalConfig) { resource('/url/:id', {id: "@id"}, { patch: { method: 'patch', headers: { 'Authorization': function() { return "token " + globalConfig.token; } } } }); }]);
1 parent 0d124e1 commit a7150f1

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/ng/http.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,9 @@ function $HttpProvider() {
535535
* - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be turned to
536536
* `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified.
537537
* - **data** – `{string|Object}` – Data to be sent as the request message data.
538-
* - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server.
538+
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
539+
* HTTP headers to send to the server. If the return value of a function is null, the header will
540+
* not be sent.
539541
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
540542
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
541543
* - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
@@ -736,6 +738,10 @@ function $HttpProvider() {
736738

737739
defHeaders = extend({}, defHeaders.common, defHeaders[lowercase(config.method)]);
738740

741+
// execute if header value is function
742+
execHeaders(defHeaders);
743+
execHeaders(reqHeaders);
744+
739745
// using for-in instead of forEach to avoid unecessary iteration after header has been found
740746
defaultHeadersIteration:
741747
for (defHeaderName in defHeaders) {
@@ -751,6 +757,21 @@ function $HttpProvider() {
751757
}
752758

753759
return reqHeaders;
760+
761+
function execHeaders(headers) {
762+
var headerContent;
763+
764+
forEach(headers, function(headerFn, header) {
765+
if (isFunction(headerFn)) {
766+
headerContent = headerFn();
767+
if (headerContent != null) {
768+
headers[header] = headerContent;
769+
} else {
770+
delete headers[header];
771+
}
772+
}
773+
});
774+
}
754775
}
755776
}
756777

test/ng/httpSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,28 @@ describe('$http', function() {
784784

785785
$httpBackend.flush();
786786
}));
787+
788+
it('should send execute result if header value is function', inject(function() {
789+
var headerConfig = {'Accept': function() { return 'Rewritten'; }};
790+
791+
function checkHeaders(headers) {
792+
return headers['Accept'] == 'Rewritten';
793+
}
794+
795+
$httpBackend.expect('GET', '/url', undefined, checkHeaders).respond('');
796+
$httpBackend.expect('POST', '/url', undefined, checkHeaders).respond('');
797+
$httpBackend.expect('PUT', '/url', undefined, checkHeaders).respond('');
798+
$httpBackend.expect('PATCH', '/url', undefined, checkHeaders).respond('');
799+
$httpBackend.expect('DELETE', '/url', undefined, checkHeaders).respond('');
800+
801+
$http({url: '/url', method: 'GET', headers: headerConfig});
802+
$http({url: '/url', method: 'POST', headers: headerConfig});
803+
$http({url: '/url', method: 'PUT', headers: headerConfig});
804+
$http({url: '/url', method: 'PATCH', headers: headerConfig});
805+
$http({url: '/url', method: 'DELETE', headers: headerConfig});
806+
807+
$httpBackend.flush();
808+
}));
787809
});
788810

789811

0 commit comments

Comments
 (0)