diff options
| author | Vojta Jina | 2011-11-04 16:34:47 -0700 |
|---|---|---|
| committer | Igor Minar | 2011-11-30 11:17:25 -0500 |
| commit | 5bbd64ac65a89f43ce30a948fc0e0f5442cacb1e (patch) | |
| tree | 20485c0d0646ed14263a4b90538949e97c3338e5 /test/service/httpSpec.js | |
| parent | caeb1bf899bfcc10b5860ad1b6632a3fe7b0b85f (diff) | |
| download | angular.js-5bbd64ac65a89f43ce30a948fc0e0f5442cacb1e.tar.bz2 | |
feat($http): allow passing custom cache instance per request
You can still use cache: true, which will use $http's default cache.
Diffstat (limited to 'test/service/httpSpec.js')
| -rw-r--r-- | test/service/httpSpec.js | 67 |
1 files changed, 55 insertions, 12 deletions
diff --git a/test/service/httpSpec.js b/test/service/httpSpec.js index 8212eb07..a235426e 100644 --- a/test/service/httpSpec.js +++ b/test/service/httpSpec.js @@ -779,16 +779,22 @@ describe('$http', function() { describe('cache', function() { + var cache; + + beforeEach(inject(function($cacheFactory) { + cache = $cacheFactory('testCache'); + })); + function doFirstCacheRequest(method, respStatus, headers) { $httpBackend.expect(method || 'GET', '/url').respond(respStatus || 200, 'content', headers); - $http({method: method || 'GET', url: '/url', cache: true}); + $http({method: method || 'GET', url: '/url', cache: cache}); $httpBackend.flush(); } - it('should cache GET request', function() { + it('should cache GET request when cache is provided', function() { doFirstCacheRequest(); - $http({method: 'get', url: '/url', cache: true}).on('200', callback); + $http({method: 'get', url: '/url', cache: cache}).on('200', callback); $browser.defer.flush(); expect(callback).toHaveBeenCalledOnce(); @@ -796,11 +802,28 @@ describe('$http', function() { }); + it('should not cache when cache is not provided', function() { + doFirstCacheRequest(); + + $httpBackend.expect('GET', '/url').respond(); + $http({method: 'GET', url: '/url'}); + }); + + + it('should perform request when cache cleared', function() { + doFirstCacheRequest(); + + cache.removeAll(); + $httpBackend.expect('GET', '/url').respond(); + $http({method: 'GET', url: '/url', cache: cache}); + }); + + it('should always call callback asynchronously', function() { doFirstCacheRequest(); - $http({method: 'get', url: '/url', cache: true}).on('200', callback); + $http({method: 'get', url: '/url', cache: cache}).on('200', callback); - expect(callback).not.toHaveBeenCalledOnce(); + expect(callback).not.toHaveBeenCalled(); }); @@ -808,7 +831,7 @@ describe('$http', function() { doFirstCacheRequest('POST'); $httpBackend.expect('POST', '/url').respond('content2'); - $http({method: 'POST', url: '/url', cache: true}).on('200', callback); + $http({method: 'POST', url: '/url', cache: cache}).on('200', callback); $httpBackend.flush(); expect(callback).toHaveBeenCalledOnce(); @@ -820,7 +843,7 @@ describe('$http', function() { doFirstCacheRequest('PUT'); $httpBackend.expect('PUT', '/url').respond('content2'); - $http({method: 'PUT', url: '/url', cache: true}).on('200', callback); + $http({method: 'PUT', url: '/url', cache: cache}).on('200', callback); $httpBackend.flush(); expect(callback).toHaveBeenCalledOnce(); @@ -832,7 +855,7 @@ describe('$http', function() { doFirstCacheRequest('DELETE'); $httpBackend.expect('DELETE', '/url').respond(206); - $http({method: 'DELETE', url: '/url', cache: true}).on('206', callback); + $http({method: 'DELETE', url: '/url', cache: cache}).on('206', callback); $httpBackend.flush(); expect(callback).toHaveBeenCalledOnce(); @@ -843,7 +866,7 @@ describe('$http', function() { doFirstCacheRequest('GET', 404); $httpBackend.expect('GET', '/url').respond('content2'); - $http({method: 'GET', url: '/url', cache: true}).on('200', callback); + $http({method: 'GET', url: '/url', cache: cache}).on('200', callback); $httpBackend.flush(); expect(callback).toHaveBeenCalledOnce(); @@ -858,7 +881,7 @@ describe('$http', function() { expect(headers('server')).toBe('Apache'); }); - $http({method: 'GET', url: '/url', cache: true}).on('200', callback); + $http({method: 'GET', url: '/url', cache: cache}).on('200', callback); $browser.defer.flush(); expect(callback).toHaveBeenCalledOnce(); }); @@ -870,10 +893,27 @@ describe('$http', function() { expect(status).toBe(201); }); - $http({method: 'get', url: '/url', cache: true}).on('2xx', callback); + $http({method: 'get', url: '/url', cache: cache}).on('2xx', callback); $browser.defer.flush(); expect(callback).toHaveBeenCalledOnce(); }); + + + it('should use cache even if request fired before first response is back', function() { + $httpBackend.expect('GET', '/url').respond(201, 'fake-response'); + + callback.andCallFake(function(response, status, headers) { + expect(response).toBe('fake-response'); + expect(status).toBe(201); + }); + + $http({method: 'GET', url: '/url', cache: cache}).on('always', callback); + $http({method: 'GET', url: '/url', cache: cache}).on('always', callback); + + $httpBackend.flush(); + expect(callback).toHaveBeenCalled(); + expect(callback.callCount).toBe(2); + }); }); @@ -903,10 +943,13 @@ describe('$http', function() { }); - it('should remove the request when served from cache', function() { + it('should update pending requests even when served from cache', function() { $httpBackend.when('GET').respond(200); $http({method: 'get', url: '/cached', cache: true}); + $http({method: 'get', url: '/cached', cache: true}); + expect($http.pendingRequests.length).toBe(2); + $httpBackend.flush(); expect($http.pendingRequests.length).toBe(0); |
