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

Commit d435464

Browse files
feat($http): provide a config object as an argument to header functions
Closes #7235 Closes #10622
1 parent c2031b1 commit d435464

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/ng/http.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ function $HttpProvider() {
615615
* - **data** – `{string|Object}` – Data to be sent as the request message data.
616616
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
617617
* HTTP headers to send to the server. If the return value of a function is null, the
618-
* header will not be sent.
618+
* header will not be sent. Functions accept a config object as an argument.
619619
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
620620
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
621621
* - **transformRequest** –
@@ -834,12 +834,12 @@ function $HttpProvider() {
834834
: $q.reject(resp);
835835
}
836836

837-
function executeHeaderFns(headers) {
837+
function executeHeaderFns(headers, config) {
838838
var headerContent, processedHeaders = {};
839839

840840
forEach(headers, function(headerFn, header) {
841841
if (isFunction(headerFn)) {
842-
headerContent = headerFn();
842+
headerContent = headerFn(config);
843843
if (headerContent != null) {
844844
processedHeaders[header] = headerContent;
845845
}
@@ -873,7 +873,7 @@ function $HttpProvider() {
873873
}
874874

875875
// execute if header value is a function for merged headers
876-
return executeHeaderFns(reqHeaders);
876+
return executeHeaderFns(reqHeaders, shallowCopy(config));
877877
}
878878
}
879879

test/ng/httpSpec.js

+28
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,34 @@ describe('$http', function() {
781781
$httpBackend.flush();
782782
});
783783

784+
it('should expose a config object to header functions', function() {
785+
var config = {
786+
foo: 'Rewritten',
787+
headers: {'Accept': function(config) {
788+
return config.foo;
789+
}}
790+
};
791+
792+
$httpBackend.expect('GET', '/url', undefined, {Accept: 'Rewritten'}).respond('');
793+
$http.get('/url', config);
794+
$httpBackend.flush();
795+
});
796+
797+
it('should not allow modifications to a config object in header functions', function() {
798+
var config = {
799+
headers: {'Accept': function(config) {
800+
config.foo = 'bar';
801+
return 'Rewritten';
802+
}}
803+
};
804+
805+
$httpBackend.expect('GET', '/url', undefined, {Accept: 'Rewritten'}).respond('');
806+
$http.get('/url', config);
807+
$httpBackend.flush();
808+
809+
expect(config.foo).toBeUndefined();
810+
});
811+
784812
it('should check the cache before checking the XSRF cookie', inject(function($browser, $cacheFactory) {
785813
var testCache = $cacheFactory('testCache'),
786814
executionOrder = [];

0 commit comments

Comments
 (0)