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

fix($http): allow empty responses to be cached #3809

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
4 changes: 2 additions & 2 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ function $HttpProvider() {

if (cache) {
cachedResp = cache.get(url);
if (cachedResp) {
if (cachedResp || typeof cachedResp === 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use isString(cachedResp)

if (cachedResp.then) {
// cached request has already been sent, but there is no response yet
cachedResp.then(removePendingReq, removePendingReq);
Expand All @@ -912,7 +912,7 @@ function $HttpProvider() {
}

// if we won't have the response in cache, send the request to the backend
if (!cachedResp) {
if (!cachedResp && typeof cachedResp !== 'string') {
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
config.withCredentials, config.responseType);
}
Expand Down
14 changes: 14 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,20 @@ describe('$http', function() {
});


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

callback.andCallFake(function (response, status, headers) {
expect(response).toBe('');
expect(status).toBe(200);
});

$http({method: 'GET', url: '/abc', cache: cache}).success(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalled();
});


it('should default to status code 200 and empty headers if cache contains a non-array element',
inject(function($rootScope) {
cache.put('/myurl', 'simple response');
Expand Down