Skip to content
Permalink
Browse files

fix(http): preserve config object when resolving from cache

Fixes #9004
Closes #9030
  • Loading branch information
shahata authored and pkozlowski-opensource committed Sep 11, 2014
1 parent e2d1969 commit facfec98412c0bb8678d578bade05ffef06a9e84
Showing with 56 additions and 2 deletions.
  1. +4 −2 src/ng/http.js
  2. +52 −0 test/ng/httpSpec.js
@@ -1027,8 +1027,7 @@ function $HttpProvider() {
if (isDefined(cachedResp)) {
if (isPromiseLike(cachedResp)) {
// cached request has already been sent, but there is no response yet
cachedResp.then(removePendingReq, removePendingReq);
return cachedResp;
cachedResp.then(resolvePromiseWithResult, resolvePromiseWithResult);
} else {
// serving from cache
if (isArray(cachedResp)) {
@@ -1106,6 +1105,9 @@ function $HttpProvider() {
});
}

function resolvePromiseWithResult(result) {
resolvePromise(result.data, result.status, shallowCopy(result.headers()), result.statusText);
}

function removePendingReq() {
var idx = $http.pendingRequests.indexOf(config);
@@ -1352,6 +1352,24 @@ describe('$http', function() {
}));


it('should not share the pending cached headers object instance', inject(function($rootScope) {
var firstResult;
callback.andCallFake(function(result) {
expect(result.headers()).toEqual(firstResult.headers());
expect(result.headers()).not.toBe(firstResult.headers());
});

$httpBackend.expect('GET', '/url').respond(200, 'content', {'content-encoding': 'gzip', 'server': 'Apache'});
$http({method: 'GET', url: '/url', cache: cache}).then(function(result) {
firstResult = result;
});
$http({method: 'GET', url: '/url', cache: cache}).then(callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnce();
}));


it('should cache status code as well', inject(function($rootScope) {
doFirstCacheRequest('GET', 201);
callback.andCallFake(function(r, status, h) {
@@ -1381,6 +1399,40 @@ describe('$http', function() {
});


it('should preserve config object when resolving from cache', function() {
$httpBackend.expect('GET', '/url').respond(200, 'content');
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'bar'}});
$httpBackend.flush();

$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'baz'}}).then(callback);
$rootScope.$digest();

expect(callback.mostRecentCall.args[0].config.headers.foo).toBe('baz');
});


it('should preserve config object when resolving from pending cache', function() {
$httpBackend.expect('GET', '/url').respond(200, 'content');
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'bar'}});

$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'baz'}}).then(callback);
$httpBackend.flush();

expect(callback.mostRecentCall.args[0].config.headers.foo).toBe('baz');
});


it('should preserve config object when rejecting from pending cache', function() {
$httpBackend.expect('GET', '/url').respond(404, 'content');
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'bar'}});

$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'baz'}}).catch(callback);
$httpBackend.flush();

expect(callback.mostRecentCall.args[0].config.headers.foo).toBe('baz');
});


it('should allow the cached value to be an empty string', function() {
cache.put('/abc', '');

0 comments on commit facfec9

Please sign in to comment.
You can’t perform that action at this time.