diff --git a/src/httpDecorator.js b/src/httpDecorator.js index a4c1f14..a6beb3f 100644 --- a/src/httpDecorator.js +++ b/src/httpDecorator.js @@ -49,9 +49,27 @@ 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 + } + } 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) { if (isCachable && rawCacheData && cacheInfo.cacheResponseData) { - callback(cachedResponse, 'cached', undefined, httpConfig, itemCache) + successCallback({ + data: cachedResponse, + status: 'cached', + headers: undefined, + config: httpConfig + }, itemCache) } return httpPromise } diff --git a/test/httpDecorator.js b/test/httpDecorator.js index 61360fd..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', @@ -295,6 +324,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()