From 9610cefd8a9d5d17fcdca692ca1c954be87fc1f0 Mon Sep 17 00:00:00 2001 From: Tom Matheussen Date: Fri, 14 Oct 2016 10:49:24 +0200 Subject: [PATCH 1/4] Added ifCached function --- src/httpDecorator.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/httpDecorator.js b/src/httpDecorator.js index a4c1f14..7e04700 100644 --- a/src/httpDecorator.js +++ b/src/httpDecorator.js @@ -49,11 +49,24 @@ function httpEtagHttpDecorator ($delegate, httpEtag) { var then = httpPromise.then var success = httpPromise.success - httpPromise.cached = function httpEtagPromiseCached (callback) { + if (useLegacyPromiseExtensions) { + httpPromise.cached = function httpEtagPromiseCached(callback) { + if (isCachable && rawCacheData && cacheInfo.cacheResponseData) { + callback(cachedResponse, 'cached', undefined, httpConfig, itemCache) + } + return httpPromise + } + } + + httpPromise.ifCached = function httpEtagPromiseIfCached(successCallback, errorCallback, progressBackCallback) { if (isCachable && rawCacheData && cacheInfo.cacheResponseData) { - callback(cachedResponse, 'cached', undefined, httpConfig, itemCache) + successCallback({ + data: cachedResponse, + status: 'cached', + headers: undefined, + config: httpConfig + }, itemCache); } - return httpPromise } httpPromise.then = function httpEtagThenWrapper (successCallback, errorCallback, progressBackCallback) { From 0e4fe744be692339f3b29ee78b455b5b36e1faee Mon Sep 17 00:00:00 2001 From: Tom Matheussen Date: Fri, 14 Oct 2016 12:03:36 +0200 Subject: [PATCH 2/4] Fixed styling issues --- src/httpDecorator.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/httpDecorator.js b/src/httpDecorator.js index 7e04700..4f0cc79 100644 --- a/src/httpDecorator.js +++ b/src/httpDecorator.js @@ -50,7 +50,7 @@ function httpEtagHttpDecorator ($delegate, httpEtag) { var success = httpPromise.success if (useLegacyPromiseExtensions) { - httpPromise.cached = function httpEtagPromiseCached(callback) { + httpPromise.cached = function httpEtagPromiseCached (callback) { if (isCachable && rawCacheData && cacheInfo.cacheResponseData) { callback(cachedResponse, 'cached', undefined, httpConfig, itemCache) } @@ -58,14 +58,14 @@ function httpEtagHttpDecorator ($delegate, httpEtag) { } } - httpPromise.ifCached = function httpEtagPromiseIfCached(successCallback, errorCallback, progressBackCallback) { + httpPromise.ifCached = function httpEtagPromiseIfCached (successCallback, errorCallback, progressBackCallback) { if (isCachable && rawCacheData && cacheInfo.cacheResponseData) { successCallback({ data: cachedResponse, status: 'cached', headers: undefined, config: httpConfig - }, itemCache); + }, itemCache) } } From b20adf8f29a40843486b60662953b7f651fb99b3 Mon Sep 17 00:00:00 2001 From: Tom Matheussen Date: Fri, 14 Oct 2016 18:54:47 +0200 Subject: [PATCH 3/4] Cached method throws error when useLegacyExtensions is false --- src/httpDecorator.js | 4 ++++ test/httpDecorator.js | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/httpDecorator.js b/src/httpDecorator.js index 4f0cc79..5c43b27 100644 --- a/src/httpDecorator.js +++ b/src/httpDecorator.js @@ -56,6 +56,10 @@ function httpEtagHttpDecorator ($delegate, httpEtag) { } return httpPromise } + } else { + httpPromise.cached = function () { + throw new Error('The method `cached` on the promise returned from `$http` has been disabled.') + } } httpPromise.ifCached = function httpEtagPromiseIfCached (successCallback, errorCallback, progressBackCallback) { diff --git a/test/httpDecorator.js b/test/httpDecorator.js index 61360fd..9f44419 100644 --- a/test/httpDecorator.js +++ b/test/httpDecorator.js @@ -295,6 +295,18 @@ describe('HTTP Decorator', function () { $http(httpConfig).success.should.throw(Error) }) + it('should not wrap `cached` when `useLegacyPromiseExtensions` is false', function () { + $httpProvider.useLegacyPromiseExtensions(false) + + var httpConfig = { + method: 'GET', + url: '/1.json', + etagCache: true + } + + $http(httpConfig).cached.should.throw(Error) + }) + it('should use the default cacheId with `{ etagCache: true }`', function () { $http.get('/1.json', { etagCache: true }) $httpBackend.flush() From df2316898d5b0db9fb64d10923079e8d169b1844 Mon Sep 17 00:00:00 2001 From: Tom Matheussen Date: Fri, 14 Oct 2016 19:16:08 +0200 Subject: [PATCH 4/4] Added test for ifCached method --- src/httpDecorator.js | 1 + test/httpDecorator.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/httpDecorator.js b/src/httpDecorator.js index 5c43b27..a6beb3f 100644 --- a/src/httpDecorator.js +++ b/src/httpDecorator.js @@ -71,6 +71,7 @@ function httpEtagHttpDecorator ($delegate, httpEtag) { config: httpConfig }, itemCache) } + return httpPromise } httpPromise.then = function httpEtagThenWrapper (successCallback, errorCallback, progressBackCallback) { diff --git a/test/httpDecorator.js b/test/httpDecorator.js index 9f44419..23ebe40 100644 --- a/test/httpDecorator.js +++ b/test/httpDecorator.js @@ -14,6 +14,7 @@ var $http var $httpBackend var cachedSpy +var ifCachedSpy var successSpy var errorSpy @@ -58,6 +59,7 @@ describe('HTTP Decorator', function () { $httpBackend = $injector.get('$httpBackend') cachedSpy = spy('cached', angular.noop) + ifCachedSpy = spy('ifCached', angular.noop) successSpy = spy('success', angular.noop) errorSpy = spy('error', angular.noop) @@ -203,6 +205,33 @@ describe('HTTP Decorator', function () { $httpBackend.flush() }) + it('should call `ifCached` callback with proper arguments', function () { + var httpConfig = { + method: 'GET', + url: '/1.json', + etagCache: true + } + + $http(httpConfig) + .ifCached(ifCachedSpy) + .success(successSpy) + $httpBackend.flush() + + $http(httpConfig) + .ifCached(function (response, itemCache) { + response.data.should.deep.equal(mockResponseData) + response.status.should.equal('cached') + should.not.exist(response.headers) + response.config.method.should.equal(httpConfig.method) + response.config.url.should.equal(httpConfig.url) + response.config.etagCache.should.equal(httpConfig.etagCache) + var cacheInfo = itemCache.info() + cacheInfo.id.should.equal('httpEtagCache') + }) + .error(errorSpy) + $httpBackend.flush() + }) + it('should call `success` callback with proper arguments', function () { var httpConfig = { method: 'GET',