Skip to content

Commit facfec9

Browse files
shahatapkozlowski-opensource
authored andcommitted
fix(http): preserve config object when resolving from cache
Fixes angular#9004 Closes angular#9030
1 parent e2d1969 commit facfec9

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/ng/http.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,7 @@ function $HttpProvider() {
10271027
if (isDefined(cachedResp)) {
10281028
if (isPromiseLike(cachedResp)) {
10291029
// cached request has already been sent, but there is no response yet
1030-
cachedResp.then(removePendingReq, removePendingReq);
1031-
return cachedResp;
1030+
cachedResp.then(resolvePromiseWithResult, resolvePromiseWithResult);
10321031
} else {
10331032
// serving from cache
10341033
if (isArray(cachedResp)) {
@@ -1106,6 +1105,9 @@ function $HttpProvider() {
11061105
});
11071106
}
11081107

1108+
function resolvePromiseWithResult(result) {
1109+
resolvePromise(result.data, result.status, shallowCopy(result.headers()), result.statusText);
1110+
}
11091111

11101112
function removePendingReq() {
11111113
var idx = $http.pendingRequests.indexOf(config);

test/ng/httpSpec.js

+52
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,24 @@ describe('$http', function() {
13521352
}));
13531353

13541354

1355+
it('should not share the pending cached headers object instance', inject(function($rootScope) {
1356+
var firstResult;
1357+
callback.andCallFake(function(result) {
1358+
expect(result.headers()).toEqual(firstResult.headers());
1359+
expect(result.headers()).not.toBe(firstResult.headers());
1360+
});
1361+
1362+
$httpBackend.expect('GET', '/url').respond(200, 'content', {'content-encoding': 'gzip', 'server': 'Apache'});
1363+
$http({method: 'GET', url: '/url', cache: cache}).then(function(result) {
1364+
firstResult = result;
1365+
});
1366+
$http({method: 'GET', url: '/url', cache: cache}).then(callback);
1367+
$httpBackend.flush();
1368+
1369+
expect(callback).toHaveBeenCalledOnce();
1370+
}));
1371+
1372+
13551373
it('should cache status code as well', inject(function($rootScope) {
13561374
doFirstCacheRequest('GET', 201);
13571375
callback.andCallFake(function(r, status, h) {
@@ -1381,6 +1399,40 @@ describe('$http', function() {
13811399
});
13821400

13831401

1402+
it('should preserve config object when resolving from cache', function() {
1403+
$httpBackend.expect('GET', '/url').respond(200, 'content');
1404+
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'bar'}});
1405+
$httpBackend.flush();
1406+
1407+
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'baz'}}).then(callback);
1408+
$rootScope.$digest();
1409+
1410+
expect(callback.mostRecentCall.args[0].config.headers.foo).toBe('baz');
1411+
});
1412+
1413+
1414+
it('should preserve config object when resolving from pending cache', function() {
1415+
$httpBackend.expect('GET', '/url').respond(200, 'content');
1416+
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'bar'}});
1417+
1418+
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'baz'}}).then(callback);
1419+
$httpBackend.flush();
1420+
1421+
expect(callback.mostRecentCall.args[0].config.headers.foo).toBe('baz');
1422+
});
1423+
1424+
1425+
it('should preserve config object when rejecting from pending cache', function() {
1426+
$httpBackend.expect('GET', '/url').respond(404, 'content');
1427+
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'bar'}});
1428+
1429+
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'baz'}}).catch(callback);
1430+
$httpBackend.flush();
1431+
1432+
expect(callback.mostRecentCall.args[0].config.headers.foo).toBe('baz');
1433+
});
1434+
1435+
13841436
it('should allow the cached value to be an empty string', function() {
13851437
cache.put('/abc', '');
13861438

0 commit comments

Comments
 (0)