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

Commit 209b67d

Browse files
thughesmhevery
authored andcommitted
feat($http): Allow setting withCredentials on defaults
Closes #1095.
1 parent 2e15393 commit 209b67d

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/ng/http.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function $HttpProvider() {
8888
JSON_END = /[\}\]]\s*$/,
8989
PROTECTION_PREFIX = /^\)\]\}',?\n/;
9090

91-
var $config = this.defaults = {
91+
var defaults = this.defaults = {
9292
// transform incoming response data
9393
transformResponse: [function(data) {
9494
if (isString(data)) {
@@ -475,9 +475,9 @@ function $HttpProvider() {
475475
function $http(config) {
476476
config.method = uppercase(config.method);
477477

478-
var reqTransformFn = config.transformRequest || $config.transformRequest,
479-
respTransformFn = config.transformResponse || $config.transformResponse,
480-
defHeaders = $config.headers,
478+
var reqTransformFn = config.transformRequest || defaults.transformRequest,
479+
respTransformFn = config.transformResponse || defaults.transformResponse,
480+
defHeaders = defaults.headers,
481481
reqHeaders = extend({'X-XSRF-TOKEN': $browser.cookies()['XSRF-TOKEN']},
482482
defHeaders.common, defHeaders[lowercase(config.method)], config.headers),
483483
reqData = transformData(config.data, headersGetter(reqHeaders), reqTransformFn),
@@ -488,6 +488,10 @@ function $HttpProvider() {
488488
delete reqHeaders['Content-Type'];
489489
}
490490

491+
if (isUndefined(config.withCredentials) && !isUndefined(defaults.withCredentials)) {
492+
config.withCredentials = defaults.withCredentials;
493+
}
494+
491495
// send request
492496
promise = sendReq(config, reqData, reqHeaders);
493497

@@ -619,11 +623,11 @@ function $HttpProvider() {
619623
*
620624
* @description
621625
* Runtime equivalent of the `$httpProvider.defaults` property. Allows configuration of
622-
* default headers as well as request and response transformations.
626+
* default headers, withCredentials as well as request and response transformations.
623627
*
624628
* See "Setting HTTP Headers" and "Transforming Requests and Responses" sections above.
625629
*/
626-
$http.defaults = $config;
630+
$http.defaults = defaults;
627631

628632

629633
return $http;
@@ -658,7 +662,7 @@ function $HttpProvider() {
658662
* Makes the request
659663
*
660664
* !!! ACCESSES CLOSURE VARS:
661-
* $httpBackend, $config, $log, $rootScope, defaultCache, $http.pendingRequests
665+
* $httpBackend, defaults, $log, $rootScope, defaultCache, $http.pendingRequests
662666
*/
663667
function sendReq(config, reqData, reqHeaders) {
664668
var deferred = $q.defer(),

test/ng/httpSpec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,4 +981,27 @@ describe('$http', function() {
981981

982982
$httpBackend.verifyNoOutstandingExpectation = noop;
983983
});
984+
985+
986+
it('should use withCredentials from default', function() {
987+
var $httpBackend = jasmine.createSpy('$httpBackend');
988+
989+
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) {
990+
expect(withCredentials).toBe(true);
991+
});
992+
993+
module(function($provide) {
994+
$provide.value('$httpBackend', $httpBackend);
995+
});
996+
997+
inject(function($http) {
998+
$http.defaults.withCredentials = true;
999+
$http({
1000+
method: 'GET', url: 'some.html', timeout: 12345, responseType: 'json'
1001+
});
1002+
expect($httpBackend).toHaveBeenCalledOnce();
1003+
});
1004+
1005+
$httpBackend.verifyNoOutstandingExpectation = noop;
1006+
});
9841007
});

0 commit comments

Comments
 (0)