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

Commit

Permalink
feat($http): Allow setting withCredentials on defaults
Browse files Browse the repository at this point in the history
Closes #1095.
  • Loading branch information
thughes authored and mhevery committed Sep 12, 2012
1 parent 2e15393 commit 209b67d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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),
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down
23 changes: 23 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

0 comments on commit 209b67d

Please sign in to comment.