From 8e48c4ff6abf7083a04cf20312d2b106f4ba5b2c Mon Sep 17 00:00:00 2001 From: jankuca Date: Thu, 29 Aug 2013 18:52:30 -0700 Subject: [PATCH] fix($http): allow empty responses to be cached Closes #3809 --- src/ng/http.js | 4 ++-- test/ng/httpSpec.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index ab4900de3032..443daf83a696 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -892,7 +892,7 @@ function $HttpProvider() { if (cache) { cachedResp = cache.get(url); - if (cachedResp) { + if (isDefined(cachedResp)) { if (cachedResp.then) { // cached request has already been sent, but there is no response yet cachedResp.then(removePendingReq, removePendingReq); @@ -912,7 +912,7 @@ function $HttpProvider() { } // if we won't have the response in cache, send the request to the backend - if (!cachedResp) { + if (isUndefined(cachedResp)) { $httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout, config.withCredentials, config.responseType); } diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index ec1cb7f1cb2c..d3653a67ee7d 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -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');