diff options
| author | David Bennett | 2013-04-24 12:33:08 -0500 |
|---|---|---|
| committer | Pete Bacon Darwin | 2013-04-30 20:42:34 +0100 |
| commit | cda7b71146f6748116ad5bbc9050ee7e79a9ce2b (patch) | |
| tree | b0a77296446f6f2bb7005c8ec26bf1059c64ca7e /test/ng/httpBackendSpec.js | |
| parent | fc25a443f8595ec8761675595af58ec873452896 (diff) | |
| download | angular.js-cda7b71146f6748116ad5bbc9050ee7e79a9ce2b.tar.bz2 | |
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.
Diffstat (limited to 'test/ng/httpBackendSpec.js')
| -rw-r--r-- | test/ng/httpBackendSpec.js | 53 |
1 files changed, 52 insertions, 1 deletions
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 }); |
