Permalink
Browse files

feat($http): provide a config object as an argument to header functions

Closes #7235
Closes #10622
  • Loading branch information...
1 parent c2031b1 commit d435464c51d3912f56cfc830d86bfc64a1578327 @pkozlowski-opensource pkozlowski-opensource committed Jan 2, 2015
Showing with 32 additions and 4 deletions.
  1. +4 −4 src/ng/http.js
  2. +28 −0 test/ng/httpSpec.js
View
@@ -615,7 +615,7 @@ function $HttpProvider() {
* - **data** – `{string|Object}` – Data to be sent as the request message data.
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
* HTTP headers to send to the server. If the return value of a function is null, the
- * header will not be sent.
+ * header will not be sent. Functions accept a config object as an argument.
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
* - **transformRequest** –
@@ -834,12 +834,12 @@ function $HttpProvider() {
: $q.reject(resp);
}
- function executeHeaderFns(headers) {
+ function executeHeaderFns(headers, config) {
var headerContent, processedHeaders = {};
forEach(headers, function(headerFn, header) {
if (isFunction(headerFn)) {
- headerContent = headerFn();
+ headerContent = headerFn(config);
if (headerContent != null) {
processedHeaders[header] = headerContent;
}
@@ -873,7 +873,7 @@ function $HttpProvider() {
}
// execute if header value is a function for merged headers
- return executeHeaderFns(reqHeaders);
+ return executeHeaderFns(reqHeaders, shallowCopy(config));
}
}
View
@@ -781,6 +781,34 @@ describe('$http', function() {
$httpBackend.flush();
});
+ it('should expose a config object to header functions', function() {
+ var config = {
+ foo: 'Rewritten',
+ headers: {'Accept': function(config) {
+ return config.foo;
+ }}
+ };
+
+ $httpBackend.expect('GET', '/url', undefined, {Accept: 'Rewritten'}).respond('');
+ $http.get('/url', config);
+ $httpBackend.flush();
+ });
+
+ it('should not allow modifications to a config object in header functions', function() {
+ var config = {
+ headers: {'Accept': function(config) {
+ config.foo = 'bar';
+ return 'Rewritten';
+ }}
+ };
+
+ $httpBackend.expect('GET', '/url', undefined, {Accept: 'Rewritten'}).respond('');
+ $http.get('/url', config);
+ $httpBackend.flush();
+
+ expect(config.foo).toBeUndefined();
+ });
+
it('should check the cache before checking the XSRF cookie', inject(function($browser, $cacheFactory) {
var testCache = $cacheFactory('testCache'),
executionOrder = [];

0 comments on commit d435464

Please sign in to comment.