diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/ng/httpBackendSpec.js | 38 | ||||
| -rw-r--r-- | test/ng/httpSpec.js | 27 | ||||
| -rw-r--r-- | test/ngMock/angular-mocksSpec.js | 22 |
3 files changed, 85 insertions, 2 deletions
diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index da4fed16..c65ab2e1 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -117,6 +117,44 @@ describe('$httpBackend', function() { }); + it('should abort request on timeout promise resolution', inject(function($timeout) { + callback.andCallFake(function(status, response) { + expect(status).toBe(-1); + }); + + $backend('GET', '/url', null, callback, {}, $timeout(noop, 2000)); + xhr = MockXhr.$$lastInstance; + spyOn(xhr, 'abort'); + + $timeout.flush(); + expect(xhr.abort).toHaveBeenCalledOnce(); + + xhr.status = 0; + xhr.readyState = 4; + xhr.onreadystatechange(); + expect(callback).toHaveBeenCalledOnce(); + })); + + + it('should not abort resolved request on timeout promise resolution', inject(function($timeout) { + callback.andCallFake(function(status, response) { + expect(status).toBe(200); + }); + + $backend('GET', '/url', null, callback, {}, $timeout(noop, 2000)); + xhr = MockXhr.$$lastInstance; + spyOn(xhr, 'abort'); + + xhr.status = 200; + xhr.readyState = 4; + xhr.onreadystatechange(); + expect(callback).toHaveBeenCalledOnce(); + + $timeout.flush(); + expect(xhr.abort).not.toHaveBeenCalled(); + })); + + it('should cancel timeout on completion', function() { callback.andCallFake(function(status, response) { expect(status).toBe(200); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 5984106c..4ddb3661 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -1273,6 +1273,33 @@ describe('$http', function() { }); + describe('timeout', function() { + + it('should abort requests when timeout promise resolves', inject(function($q) { + var canceler = $q.defer(); + + $httpBackend.expect('GET', '/some').respond(200); + + $http({method: 'GET', url: '/some', timeout: canceler.promise}).error( + function(data, status, headers, config) { + expect(data).toBeUndefined(); + expect(status).toBe(0); + expect(headers()).toEqual({}); + expect(config.url).toBe('/some'); + callback(); + }); + + $rootScope.$apply(function() { + canceler.resolve(); + }); + + expect(callback).toHaveBeenCalled(); + $httpBackend.verifyNoOutstandingExpectation(); + $httpBackend.verifyNoOutstandingRequest(); + })); + }); + + describe('pendingRequests', function() { it('should be an array of pending requests', function() { diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 176c5c92..220f8e58 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -798,6 +798,24 @@ describe('ngMock', function() { }); + it('should abort requests when timeout promise resolves', function() { + hb.expect('GET', '/url1').respond(200); + + var canceler, then = jasmine.createSpy('then').andCallFake(function(fn) { + canceler = fn; + }); + + hb('GET', '/url1', null, callback, null, {then: then}); + expect(typeof canceler).toBe('function'); + + canceler(); // simulate promise resolution + + expect(callback).toHaveBeenCalledWith(-1, undefined, ''); + hb.verifyNoOutstandingExpectation(); + hb.verifyNoOutstandingRequest(); + }); + + it('should throw an exception if no response defined', function() { hb.when('GET', '/test'); expect(function() { @@ -1006,8 +1024,8 @@ describe('ngMockE2E', function() { hb.when('GET', /\/passThrough\/.*/).passThrough(); hb('GET', '/passThrough/23', null, callback); - expect(realHttpBackend). - toHaveBeenCalledOnceWith('GET', '/passThrough/23', null, callback, undefined); + expect(realHttpBackend).toHaveBeenCalledOnceWith( + 'GET', '/passThrough/23', null, callback, undefined, undefined); }); }); |
