From 514dc0eb16a8fe3fa7c44094d743714f73754321 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 12 Jul 2013 17:42:37 -0700 Subject: [PATCH] fix($http): allow interceptors to completely override headers Closes #2770 --- src/ng/http.js | 1 + test/ng/httpSpec.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/ng/http.js b/src/ng/http.js index d81b7912a3de..a44da3a431b0 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -666,6 +666,7 @@ function $HttpProvider() { var serverRequest = function(config) { + headers = config.headers; var reqData = transformData(config.data, headersGetter(headers), config.transformRequest); // strip content-type if data is undefined diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index c21c6c23728a..cefc88e1cec0 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -284,6 +284,27 @@ describe('$http', function() { }); }); + + it('should allow replacement of the headers object', function() { + module(function($httpProvider) { + $httpProvider.interceptors.push(function() { + return { + request: function(config) { + config.headers = {foo: 'intercepted'}; + return config; + } + }; + }); + }); + inject(function($http, $httpBackend, $rootScope) { + $httpBackend.expect('GET', '/url', null, function (headers) { + return angular.equals(headers, {foo: 'intercepted'}); + }).respond(''); + $http.get('/url'); + $rootScope.$apply(); + }); + }); + it('should reject the http promise if an interceptor fails', function() { var reason = new Error('interceptor failed'); module(function($httpProvider) { @@ -752,7 +773,7 @@ describe('$http', function() { $httpBackend.expect('POST', '/url2', undefined, function(headers) { return !headers.hasOwnProperty('content-type'); }).respond(''); - + $http({url: '/url', method: 'POST'}); $http({url: '/url2', method: 'POST', headers: {'content-type': 'Rewritten'}}); $httpBackend.flush();