From cda7b71146f6748116ad5bbc9050ee7e79a9ce2b Mon Sep 17 00:00:00 2001 From: David Bennett Date: Wed, 24 Apr 2013 12:33:08 -0500 Subject: feat($httpBackend): add timeout support for JSONP requests Documentation implies that timeout works for all requests, though it only works with XHR. To implement: - Change $httpBackend to set a timeout for JSONP requests which will immediately resolve the request when fired. - Cancel the timeout when requests are completed. --- test/ng/httpBackendSpec.js | 53 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'test/ng/httpBackendSpec.js') diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index a7935a7c..da4fed16 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -1,21 +1,36 @@ describe('$httpBackend', function() { var $backend, $browser, callbacks, - xhr, fakeDocument, callback; + xhr, fakeDocument, callback, + fakeTimeoutId = 0; // TODO(vojta): should be replaced by $defer mock function fakeTimeout(fn, delay) { fakeTimeout.fns.push(fn); fakeTimeout.delays.push(delay); + fakeTimeout.ids.push(++fakeTimeoutId); + return fakeTimeoutId; } fakeTimeout.fns = []; fakeTimeout.delays = []; + fakeTimeout.ids = []; fakeTimeout.flush = function() { var len = fakeTimeout.fns.length; fakeTimeout.delays = []; + fakeTimeout.ids = []; while (len--) fakeTimeout.fns.shift()(); }; + fakeTimeout.cancel = function(id) { + var i = indexOf(fakeTimeout.ids, id); + if (i >= 0) { + fakeTimeout.fns.splice(i, 1); + fakeTimeout.delays.splice(i, 1); + fakeTimeout.ids.splice(i, 1); + return true; + } + return false; + }; beforeEach(inject(function($injector) { @@ -102,6 +117,27 @@ describe('$httpBackend', function() { }); + it('should cancel timeout on completion', function() { + callback.andCallFake(function(status, response) { + expect(status).toBe(200); + }); + + $backend('GET', '/url', null, callback, {}, 2000); + xhr = MockXhr.$$lastInstance; + spyOn(xhr, 'abort'); + + expect(fakeTimeout.delays[0]).toBe(2000); + + xhr.status = 200; + xhr.readyState = 4; + xhr.onreadystatechange(); + expect(callback).toHaveBeenCalledOnce(); + + expect(fakeTimeout.delays.length).toBe(0); + expect(xhr.abort).not.toHaveBeenCalled(); + }); + + it('should register onreadystatechange callback before sending', function() { // send() in IE6, IE7 is sync when serving from cache function SyncXhr() { @@ -239,6 +275,21 @@ describe('$httpBackend', function() { }); + it('should abort request on timeout', function() { + callback.andCallFake(function(status, response) { + expect(status).toBe(-1); + }); + + $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback, null, 2000); + expect(fakeDocument.$$scripts.length).toBe(1); + expect(fakeTimeout.delays[0]).toBe(2000); + + fakeTimeout.flush(); + expect(fakeDocument.$$scripts.length).toBe(0); + expect(callback).toHaveBeenCalledOnce(); + }); + + // TODO(vojta): test whether it fires "async-start" // TODO(vojta): test whether it fires "async-end" on both success and error }); -- cgit v1.2.3