From 209b67df6a49fe1646ce63c5e7d11ed26e8abbc1 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 29 Jul 2012 17:52:19 -0700 Subject: [PATCH] feat($http): Allow setting withCredentials on defaults Closes #1095. --- src/ng/http.js | 18 +++++++++++------- test/ng/httpSpec.js | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index ef55f8787bf4..aa0eca740642 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -88,7 +88,7 @@ function $HttpProvider() { JSON_END = /[\}\]]\s*$/, PROTECTION_PREFIX = /^\)\]\}',?\n/; - var $config = this.defaults = { + var defaults = this.defaults = { // transform incoming response data transformResponse: [function(data) { if (isString(data)) { @@ -475,9 +475,9 @@ function $HttpProvider() { function $http(config) { config.method = uppercase(config.method); - var reqTransformFn = config.transformRequest || $config.transformRequest, - respTransformFn = config.transformResponse || $config.transformResponse, - defHeaders = $config.headers, + var reqTransformFn = config.transformRequest || defaults.transformRequest, + respTransformFn = config.transformResponse || defaults.transformResponse, + defHeaders = defaults.headers, reqHeaders = extend({'X-XSRF-TOKEN': $browser.cookies()['XSRF-TOKEN']}, defHeaders.common, defHeaders[lowercase(config.method)], config.headers), reqData = transformData(config.data, headersGetter(reqHeaders), reqTransformFn), @@ -488,6 +488,10 @@ function $HttpProvider() { delete reqHeaders['Content-Type']; } + if (isUndefined(config.withCredentials) && !isUndefined(defaults.withCredentials)) { + config.withCredentials = defaults.withCredentials; + } + // send request promise = sendReq(config, reqData, reqHeaders); @@ -619,11 +623,11 @@ function $HttpProvider() { * * @description * Runtime equivalent of the `$httpProvider.defaults` property. Allows configuration of - * default headers as well as request and response transformations. + * default headers, withCredentials as well as request and response transformations. * * See "Setting HTTP Headers" and "Transforming Requests and Responses" sections above. */ - $http.defaults = $config; + $http.defaults = defaults; return $http; @@ -658,7 +662,7 @@ function $HttpProvider() { * Makes the request * * !!! ACCESSES CLOSURE VARS: - * $httpBackend, $config, $log, $rootScope, defaultCache, $http.pendingRequests + * $httpBackend, defaults, $log, $rootScope, defaultCache, $http.pendingRequests */ function sendReq(config, reqData, reqHeaders) { var deferred = $q.defer(), diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index f7df3d4b06c3..45d31eef914b 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -981,4 +981,27 @@ describe('$http', function() { $httpBackend.verifyNoOutstandingExpectation = noop; }); + + + it('should use withCredentials from default', function() { + var $httpBackend = jasmine.createSpy('$httpBackend'); + + $httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) { + expect(withCredentials).toBe(true); + }); + + module(function($provide) { + $provide.value('$httpBackend', $httpBackend); + }); + + inject(function($http) { + $http.defaults.withCredentials = true; + $http({ + method: 'GET', url: 'some.html', timeout: 12345, responseType: 'json' + }); + expect($httpBackend).toHaveBeenCalledOnce(); + }); + + $httpBackend.verifyNoOutstandingExpectation = noop; + }); });