diff --git a/src/ng/http.js b/src/ng/http.js index 0c54f5bb636e..e47216b44302 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -941,8 +941,7 @@ function $HttpProvider() { if (isDefined(cachedResp)) { if (cachedResp.then) { // cached request has already been sent, but there is no response yet - cachedResp.then(removePendingReq, removePendingReq); - return cachedResp; + cachedResp.then(cacheDone, cacheDone); } else { // serving from cache if (isArray(cachedResp)) { @@ -987,6 +986,18 @@ function $HttpProvider() { } + /** + * Callback to resolve based on an in-flight cache entry. + */ + function cacheDone(resolution) { + if (isUndefined(resolution.status)) { + resolvePromise(resolution, 200, {}); + } else { + resolvePromise(resolution.data, resolution.status, resolution.headers); + } + } + + /** * Resolves the raw $http promise. */ diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index a7b244831f6f..8da64a5f546f 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -1267,6 +1267,30 @@ describe('$http', function() { }); + it('should resolve with correct config if second request was made before the first returned', + function() { + $httpBackend.expect('GET', '/url').respond(201, 'fake-response'); + + callback.andCallFake(function(response) { + expect(response.data).toBe('fake-response'); + expect(response.status).toBe(201); + return response; + }); + + $http({method: 'GET', url: '/url', cache: cache}).then(callback); + $http({method: 'GET', url: '/url', cache: cache, extraData: 'some-data'}) + .then(callback) + .then(function(response) { + expect(response.config.extraData).toBe('some-data'); + }); + + $httpBackend.flush(); + expect(callback).toHaveBeenCalled(); + expect(callback.callCount).toBe(2); + } + ); + + it('should allow the cached value to be an empty string', function () { cache.put('/abc', '');