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

Commit 86182a9

Browse files
committed
feat($http): add withCredentials config option
1 parent 15ecc6f commit 86182a9

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/ng/http.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ function $HttpProvider() {
375375
* {@link angular.module.ng.$cacheFactory $cacheFactory}, this cache will be used for
376376
* caching.
377377
* - **timeout** – `{number}` – timeout in milliseconds.
378+
* - **withCredentials** - `{boolean}` - whether to to set the `withCredentials` flag on the
379+
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
380+
* requests with credentials} for more information.
378381
*
379382
* @returns {HttpPromise} Returns a {@link angular.module.ng.$q promise} object with the
380383
* standard `then` method and two http specific methods: `success` and `error`. The `then`
@@ -674,7 +677,8 @@ function $HttpProvider() {
674677

675678
// if we won't have the response in cache, send the request to the backend
676679
if (!cachedResp) {
677-
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout);
680+
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
681+
config.withCredentials);
678682
}
679683

680684
return promise;

src/ng/httpBackend.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function $HttpBackendProvider() {
3232

3333
function createHttpBackend($browser, XHR, $browserDefer, callbacks, body, locationProtocol) {
3434
// TODO(vojta): fix the signature
35-
return function(method, url, post, callback, headers, timeout) {
35+
return function(method, url, post, callback, headers, timeout, withCredentials) {
3636
$browser.$$incOutstandingRequestCount();
3737
url = url || $browser.url();
3838

@@ -71,6 +71,10 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, body, locati
7171
}
7272
};
7373

74+
if (withCredentials) {
75+
xhr.withCredentials = true;
76+
}
77+
7478
xhr.send(post || '');
7579

7680
if (timeout > 0) {

test/ng/httpBackendSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ describe('$httpBackend', function() {
113113
});
114114

115115

116+
it('should set withCredentials', function() {
117+
$backend('GET', '/some.url', null, callback, {}, null, true);
118+
expect(MockXhr.$$lastInstance.withCredentials).toBe(true);
119+
});
120+
121+
116122
describe('JSONP', function() {
117123

118124
var SCRIPT_URL = /([^\?]*)\?cb=angular\.callbacks\.(.*)/;

test/ng/httpSpec.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe('$http', function() {
77
beforeEach(function() {
88
callback = jasmine.createSpy('done');
99
});
10+
1011
beforeEach(module(function($exceptionHandlerProvider) {
1112
$exceptionHandlerProvider.mode('log');
1213
}));
@@ -129,9 +130,6 @@ describe('$http', function() {
129130
}));
130131

131132

132-
// TODO(vojta): test passing timeout
133-
134-
135133
describe('params', function() {
136134
it('should do basic request with params and encode', inject(function($httpBackend, $http) {
137135
$httpBackend.expect('GET', '/url?a%3D=%3F%26&b=2').respond('');
@@ -943,4 +941,25 @@ describe('$http', function() {
943941
});
944942
});
945943
});
944+
945+
946+
it('should pass timeout and withCredentials', function() {
947+
var $httpBackend = jasmine.createSpy('$httpBackend');
948+
949+
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials) {
950+
expect(timeout).toBe(12345);
951+
expect(withCredentials).toBe(true);
952+
});
953+
954+
module(function($provide) {
955+
$provide.value('$httpBackend', $httpBackend);
956+
});
957+
958+
inject(function($http) {
959+
$http({method: 'GET', url: 'some.html', timeout: 12345, withCredentials: true});
960+
expect($httpBackend).toHaveBeenCalledOnce();
961+
});
962+
963+
$httpBackend.verifyNoOutstandingExpectation = noop;
964+
});
946965
});

0 commit comments

Comments
 (0)