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

Added ifCached function #51

Merged
merged 4 commits into from
Oct 15, 2016
Merged
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
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