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

Commit 84e1d28

Browse files
feat($http): provide a config object as an argument to header functions
1 parent c66b4b6 commit 84e1d28

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-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

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

784+
it('should expose a copy of a config object to header functions', function () {
785+
var config = {
786+
foo: 'Rewritten',
787+
headers: {'Accept': function(config) {
788+
config.bar = 'baz';
789+
return config.foo;
790+
}}
791+
};
792+
793+
function checkHeaders(headers) {
794+
return headers['Accept'] == 'Rewritten';
795+
}
796+
797+
$httpBackend.expect('GET', '/url', undefined, checkHeaders).respond('');
798+
$http.get('/url', config);
799+
$httpBackend.flush();
800+
801+
expect(config.bar).toBeUndefined();
802+
});
803+
784804
it('should check the cache before checking the XSRF cookie', inject(function($browser, $cacheFactory) {
785805
var testCache = $cacheFactory('testCache'),
786806
executionOrder = [];

0 commit comments

Comments
 (0)