Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Commit

Permalink
Added ifCached function (#51)
Browse files Browse the repository at this point in the history
* Added ifCached function

* Fixed styling issues

* Cached method throws error when useLegacyExtensions is false

* Added test for ifCached method
  • Loading branch information
Tommatheussen authored and shaungrady committed Oct 15, 2016
1 parent 93e5bd2 commit c6c5c2f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/httpDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
41 changes: 41 additions & 0 deletions test/httpDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var $http
var $httpBackend

var cachedSpy
var ifCachedSpy
var successSpy
var errorSpy

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c6c5c2f

Please sign in to comment.