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

Commit

Permalink
feat($http): expose the defaults config as $http.defaults
Browse files Browse the repository at this point in the history
Often it is impossible to set the http defaults during the config phase,
because the config info is not available at this time.

A good example is authentication - often the app needs to bootstrap,
allow user to enter credentials and only then it gains access to
session token which then should be sent to the server with every request.
Without having the ability to set the defaults at runtime, the developer
either has to resort to hacks, or has to set the session token header
with every request made by the app.
  • Loading branch information
IgorMinar committed Apr 12, 2012
1 parent 0a5050e commit dceafd3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ function $HttpProvider() {
* with name equal to the lower-cased http method name, e.g.
* `$httpProvider.defaults.headers.get['My-Header']='value'`.
*
* Additionally, the defaults can be set at runtime via the `$http.defaults` object in a similar
* fassion as described above.
*
*
* # Transforming Requests and Responses
*
Expand Down Expand Up @@ -604,6 +607,19 @@ function $HttpProvider() {
*/
createShortMethodsWithData('post', 'put');

/**
* @ngdoc property
* @name angular.module.ng.$http#defaults
* @propertyOf angular.module.ng.$http
*
* @description
* Runtime equivalent of the `$httpProvider.defaults` property. Allows configuration of
* default headers as well as request and response transformations.
*
* See "Setting HTTP Headers" and "Transforming Requests and Responses" sections above.
*/
$http.defaults = $config;


return $http;

Expand Down
16 changes: 16 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,22 @@ describe('$http', function() {
$httpBackend.flush();
});
});


describe('defaults', function() {

it('should expose the defaults object at runtime', function() {
expect($http.defaults).toBeDefined();

$http.defaults.headers.common.foo = 'bar';
$httpBackend.expect('GET', '/url', undefined, function(headers) {
return headers['foo'] == 'bar';
}).respond('');

$http.get('/url');
$httpBackend.flush();
});
});
});


Expand Down

0 comments on commit dceafd3

Please sign in to comment.