From 86182a9415b9209662b16c25c180b958ba7e6cf9 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Tue, 3 Apr 2012 11:00:52 -0700 Subject: [PATCH] feat($http): add withCredentials config option --- src/ng/http.js | 6 +++++- src/ng/httpBackend.js | 6 +++++- test/ng/httpBackendSpec.js | 6 ++++++ test/ng/httpSpec.js | 25 ++++++++++++++++++++++--- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index 9c4dafd4f0f5..3942c5c7f897 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -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` @@ -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; diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index abe1d8f594fb..b2c14b3f2ae6 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -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(); @@ -71,6 +71,10 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, body, locati } }; + if (withCredentials) { + xhr.withCredentials = true; + } + xhr.send(post || ''); if (timeout > 0) { diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index 820099e86ff2..91c0574dc445 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -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\.(.*)/; diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index ab50827c2272..24ff50b4b5f3 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -7,6 +7,7 @@ describe('$http', function() { beforeEach(function() { callback = jasmine.createSpy('done'); }); + beforeEach(module(function($exceptionHandlerProvider) { $exceptionHandlerProvider.mode('log'); })); @@ -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(''); @@ -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; + }); });