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

fix($http): resolve cached requests with the proper config when hitting a cached promise #6534

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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.
*/
Expand Down
24 changes: 24 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', '');

Expand Down