aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Hughes2012-07-29 17:52:19 -0700
committerMisko Hevery2012-09-11 21:59:31 -0700
commit209b67df6a49fe1646ce63c5e7d11ed26e8abbc1 (patch)
treecb02a4916b99150eef5758c845ce33074e9f2cdb
parent2e1539356a7ace9512110acea9808d497ed0789f (diff)
downloadangular.js-209b67df6a49fe1646ce63c5e7d11ed26e8abbc1.tar.bz2
feat($http): Allow setting withCredentials on defaults
Closes #1095.
-rw-r--r--src/ng/http.js18
-rw-r--r--test/ng/httpSpec.js23
2 files changed, 34 insertions, 7 deletions
diff --git a/src/ng/http.js b/src/ng/http.js
index ef55f878..aa0eca74 100644
--- a/src/ng/http.js
+++ b/src/ng/http.js
@@ -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)) {
@@ -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),
@@ -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);
@@ -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;
@@ -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(),
diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js
index f7df3d4b..45d31eef 100644
--- a/test/ng/httpSpec.js
+++ b/test/ng/httpSpec.js
@@ -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;
+ });
});