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

Commit f7a4b48

Browse files
maxthyenlgalfaso
authored andcommitted
fix($http): do not modify the config object passed into $http short methods
Update $http's createShortMethods and createShortMethodsWithData to extend an empty object instead of the passed-in config. Previously, since $http was extending the passed-in config, the changes to the config object persisted even after the call to $http's get/post/etc. returned. This causes unexpected behavior if that config object is reused in subsequent calls to $http. The existing test in httpSpec was not properly testing this situation. Closes: #11764
1 parent 9804867 commit f7a4b48

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/ng/http.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ function $HttpProvider() {
10941094
function createShortMethods(names) {
10951095
forEach(arguments, function(name) {
10961096
$http[name] = function(url, config) {
1097-
return $http(extend(config || {}, {
1097+
return $http(extend({}, config || {}, {
10981098
method: name,
10991099
url: url
11001100
}));
@@ -1106,7 +1106,7 @@ function $HttpProvider() {
11061106
function createShortMethodsWithData(name) {
11071107
forEach(arguments, function(name) {
11081108
$http[name] = function(url, data, config) {
1109-
return $http(extend(config || {}, {
1109+
return $http(extend({}, config || {}, {
11101110
method: name,
11111111
url: url,
11121112
data: data

test/ng/httpSpec.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,16 @@ describe('$http', function() {
230230
});
231231
});
232232
inject(function($http, $httpBackend, $rootScope) {
233-
var config = { method: 'get', url: '/url', headers: { foo: 'bar'} };
233+
var config = { headers: { foo: 'bar'} };
234+
var configCopy = angular.copy(config);
234235
$httpBackend.expect('GET', '/intercepted').respond('');
235-
$http.get('/url');
236+
$http.get('/url', config);
237+
$rootScope.$apply();
238+
expect(config).toEqual(configCopy);
239+
$httpBackend.expect('POST', '/intercepted').respond('');
240+
$http.post('/url', {bar: 'baz'}, config);
236241
$rootScope.$apply();
237-
expect(config.method).toEqual('get');
238-
expect(config.url).toEqual('/url');
239-
expect(config.headers.foo).toEqual('bar');
242+
expect(config).toEqual(configCopy);
240243
});
241244
});
242245

0 commit comments

Comments
 (0)