From a13b5ed3bc337a493029815c595b89c39eb95af6 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 28 Dec 2011 09:26:22 -0800 Subject: fix($http): fix and cleanup $http and friends $http: - use promises internally - get rid of XhrFuture that was previously used internally - get rid of $browser.defer calls for async stuff (serving from cache), promises will take care of asynchronicity - fix transformation bugs (when caching requested + multiple request pending + error is returned) - get rid of native header parsing and instead just lazily parse the header string $httpBackend: - don't return raw/mock XMLHttpRequest object (we don't use it for anything anymore) - call the callback with response headers string mock $httpBackend: - unify response api for expect and when - call the callback with response headers string - changed the expect/when failure error message so that EXPECTED and GOT values are aligned Conflicts: src/service/http.js test/service/compilerSpec.js test/service/httpSpec.js --- test/service/httpSpec.js | 53 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'test/service/httpSpec.js') diff --git a/test/service/httpSpec.js b/test/service/httpSpec.js index 5b8f43f5..c1f8645e 100644 --- a/test/service/httpSpec.js +++ b/test/service/httpSpec.js @@ -66,7 +66,7 @@ describe('$http', function() { expect(data).toBe('Hello!?'); expect(status).toBe(209); callback(); - }) + }); $httpBackend.flush(); expect(callback).toHaveBeenCalledOnce(); })); @@ -550,7 +550,7 @@ describe('$http', function() { }); - describe('transform', function() { + describe('transformData', function() { describe('request', function() { @@ -648,17 +648,19 @@ describe('$http', function() { 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: cache}); $httpBackend.flush(); } - it('should cache GET request when cache is provided', inject(function($browser) { + + it('should cache GET request when cache is provided', inject(function($rootScope) { doFirstCacheRequest(); $http({method: 'get', url: '/url', cache: cache}).success(callback); - $browser.defer.flush(); + $rootScope.$digest(); expect(callback).toHaveBeenCalledOnce(); expect(callback.mostRecentCall.args[0]).toBe('content'); @@ -737,7 +739,7 @@ describe('$http', function() { }); - it('should cache the headers as well', inject(function($browser) { + it('should cache the headers as well', inject(function($rootScope) { doFirstCacheRequest('GET', 200, {'content-encoding': 'gzip', 'server': 'Apache'}); callback.andCallFake(function(r, s, headers) { expect(headers()).toEqual({'content-encoding': 'gzip', 'server': 'Apache'}); @@ -745,24 +747,37 @@ describe('$http', function() { }); $http({method: 'GET', url: '/url', cache: cache}).success(callback); - $browser.defer.flush(); + $rootScope.$digest(); expect(callback).toHaveBeenCalledOnce(); })); - it('should cache status code as well', inject(function($browser) { + it('should not share the cached headers object instance', inject(function($rootScope) { + doFirstCacheRequest('GET', 200, {'content-encoding': 'gzip', 'server': 'Apache'}); + callback.andCallFake(function(r, s, headers) { + expect(headers()).toEqual(cache.get('/url')[2]); + expect(headers()).not.toBe(cache.get('/url')[2]); + }); + + $http({method: 'GET', url: '/url', cache: cache}).success(callback); + $rootScope.$digest(); + expect(callback).toHaveBeenCalledOnce(); + })); + + + it('should cache status code as well', inject(function($rootScope) { doFirstCacheRequest('GET', 201); callback.andCallFake(function(r, status, h) { expect(status).toBe(201); }); $http({method: 'get', url: '/url', cache: cache}).success(callback); - $browser.defer.flush(); + $rootScope.$digest(); expect(callback).toHaveBeenCalledOnce(); })); - it('should use cache even if request fired before first response is back', function() { + it('should use cache even if second request was made before the first returned', function() { $httpBackend.expect('GET', '/url').respond(201, 'fake-response'); callback.andCallFake(function(response, status, headers) { @@ -777,6 +792,22 @@ describe('$http', function() { expect(callback).toHaveBeenCalled(); expect(callback.callCount).toBe(2); }); + + + it('should default to status code 200 and empty headers if cache contains a non-array element', + inject(function($rootScope) { + cache.put('/myurl', 'simple response'); + $http.get('/myurl', {cache: cache}).success(function(data, status, headers) { + expect(data).toBe('simple response'); + expect(status).toBe(200); + expect(headers()).toEqual({}); + callback(); + }); + + $rootScope.$digest(); + expect(callback).toHaveBeenCalledOnce(); + }) + ); }); @@ -794,7 +825,7 @@ describe('$http', function() { }); - it('should update pending requests even when served from cache', inject(function($browser) { + it('should update pending requests even when served from cache', inject(function($rootScope) { $httpBackend.when('GET').respond(200); $http({method: 'get', url: '/cached', cache: true}); @@ -807,7 +838,7 @@ describe('$http', function() { $http({method: 'get', url: '/cached', cache: true}); expect($http.pendingRequests.length).toBe(1); - $browser.defer.flush(); + $rootScope.$apply(); expect($http.pendingRequests.length).toBe(0); })); -- cgit v1.2.3