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

Commit

Permalink
feat($http): add withCredentials config option
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Apr 4, 2012
1 parent 15ecc6f commit 86182a9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/ng/http.js
Expand Up @@ -375,6 +375,9 @@ function $HttpProvider() {
* {@link angular.module.ng.$cacheFactory $cacheFactory}, this cache will be used for
* caching.
* - **timeout** – `{number}` – timeout in milliseconds.
* - **withCredentials** - `{boolean}` - whether to to set the `withCredentials` flag on the
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
* requests with credentials} for more information.
*
* @returns {HttpPromise} Returns a {@link angular.module.ng.$q promise} object with the
* standard `then` method and two http specific methods: `success` and `error`. The `then`
Expand Down Expand Up @@ -674,7 +677,8 @@ function $HttpProvider() {

// if we won't have the response in cache, send the request to the backend
if (!cachedResp) {
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout);
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
config.withCredentials);
}

return promise;
Expand Down
6 changes: 5 additions & 1 deletion src/ng/httpBackend.js
Expand Up @@ -32,7 +32,7 @@ function $HttpBackendProvider() {

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

Expand Down Expand Up @@ -71,6 +71,10 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, body, locati
}
};

if (withCredentials) {
xhr.withCredentials = true;
}

xhr.send(post || '');

if (timeout > 0) {
Expand Down
6 changes: 6 additions & 0 deletions test/ng/httpBackendSpec.js
Expand Up @@ -113,6 +113,12 @@ describe('$httpBackend', function() {
});


it('should set withCredentials', function() {
$backend('GET', '/some.url', null, callback, {}, null, true);
expect(MockXhr.$$lastInstance.withCredentials).toBe(true);
});


describe('JSONP', function() {

var SCRIPT_URL = /([^\?]*)\?cb=angular\.callbacks\.(.*)/;
Expand Down
25 changes: 22 additions & 3 deletions test/ng/httpSpec.js
Expand Up @@ -7,6 +7,7 @@ describe('$http', function() {
beforeEach(function() {
callback = jasmine.createSpy('done');
});

beforeEach(module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}));
Expand Down Expand Up @@ -129,9 +130,6 @@ describe('$http', function() {
}));


// TODO(vojta): test passing timeout


describe('params', function() {
it('should do basic request with params and encode', inject(function($httpBackend, $http) {
$httpBackend.expect('GET', '/url?a%3D=%3F%26&b=2').respond('');
Expand Down Expand Up @@ -943,4 +941,25 @@ describe('$http', function() {
});
});
});


it('should pass timeout and withCredentials', function() {
var $httpBackend = jasmine.createSpy('$httpBackend');

$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials) {
expect(timeout).toBe(12345);
expect(withCredentials).toBe(true);
});

module(function($provide) {
$provide.value('$httpBackend', $httpBackend);
});

inject(function($http) {
$http({method: 'GET', url: 'some.html', timeout: 12345, withCredentials: true});
expect($httpBackend).toHaveBeenCalledOnce();
});

$httpBackend.verifyNoOutstandingExpectation = noop;
});
});

0 comments on commit 86182a9

Please sign in to comment.