aboutsummaryrefslogtreecommitdiffstats
path: root/test/ng/httpSpec.js
diff options
context:
space:
mode:
authorAlexander Shtuchkin2013-02-28 09:25:21 +0400
committerJames deBoer2013-03-08 10:19:18 -0800
commit99f3b70b2d316f5bb39e21249e752c29f49c90ab (patch)
tree6d45c16c42bd3deda361822efe3b9b37391f1e29 /test/ng/httpSpec.js
parent603fe0d19608ffe1915d8bc23bf412912e7ee1ac (diff)
downloadangular.js-99f3b70b2d316f5bb39e21249e752c29f49c90ab.tar.bz2
feat(http): set custom default cache in $http.defaults.cache
When we need more control over http caching, we may want to provide a custom cache to be used in all http requests by default. To skip default cache, set {cache: false} in request configuration. To use other cache, set {cache: cache} as before. See #2079
Diffstat (limited to 'test/ng/httpSpec.js')
-rw-r--r--test/ng/httpSpec.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js
index 600a6a2e..2dd14192 100644
--- a/test/ng/httpSpec.js
+++ b/test/ng/httpSpec.js
@@ -923,6 +923,77 @@ describe('$http', function() {
expect(callback).toHaveBeenCalledOnce();
})
);
+
+ describe('$http.defaults.cache', function () {
+
+ it('should be undefined by default', function() {
+ expect($http.defaults.cache).toBeUndefined()
+ });
+
+ it('should cache requests when no cache given in request config', function() {
+ $http.defaults.cache = cache;
+
+ // First request fills the cache from server response.
+ $httpBackend.expect('GET', '/url').respond(200, 'content');
+ $http({method: 'GET', url: '/url'}); // Notice no cache given in config.
+ $httpBackend.flush();
+
+ // Second should be served from cache, without sending request to server.
+ $http({method: 'get', url: '/url'}).success(callback);
+ $rootScope.$digest();
+
+ expect(callback).toHaveBeenCalledOnce();
+ expect(callback.mostRecentCall.args[0]).toBe('content');
+
+ // Invalidate cache entry.
+ $http.defaults.cache.remove("/url");
+
+ // After cache entry removed, a request should be sent to server.
+ $httpBackend.expect('GET', '/url').respond(200, 'content');
+ $http({method: 'GET', url: '/url'});
+ $httpBackend.flush();
+ });
+
+ it('should have less priority than explicitly given cache', inject(function($cacheFactory) {
+ var localCache = $cacheFactory('localCache');
+ $http.defaults.cache = cache;
+
+ // Fill local cache.
+ $httpBackend.expect('GET', '/url').respond(200, 'content-local-cache');
+ $http({method: 'GET', url: '/url', cache: localCache});
+ $httpBackend.flush();
+
+ // Fill default cache.
+ $httpBackend.expect('GET', '/url').respond(200, 'content-default-cache');
+ $http({method: 'GET', url: '/url'});
+ $httpBackend.flush();
+
+ // Serve request from default cache when no local given.
+ $http({method: 'get', url: '/url'}).success(callback);
+ $rootScope.$digest();
+ expect(callback).toHaveBeenCalledOnce();
+ expect(callback.mostRecentCall.args[0]).toBe('content-default-cache');
+ callback.reset();
+
+ // Serve request from local cache when it is given (but default filled too).
+ $http({method: 'get', url: '/url', cache: localCache}).success(callback);
+ $rootScope.$digest();
+ expect(callback).toHaveBeenCalledOnce();
+ expect(callback.mostRecentCall.args[0]).toBe('content-local-cache');
+ }));
+
+ it('should be skipped if {cache: false} is passed in request config', function() {
+ $http.defaults.cache = cache;
+
+ $httpBackend.expect('GET', '/url').respond(200, 'content');
+ $http({method: 'GET', url: '/url'});
+ $httpBackend.flush();
+
+ $httpBackend.expect('GET', '/url').respond();
+ $http({method: 'GET', url: '/url', cache: false});
+ $httpBackend.flush();
+ });
+ });
});