From f669d992d9ee679e96be8a6c30c826b1050bbc83 Mon Sep 17 00:00:00 2001 From: Guillaume Pannatier Date: Mon, 10 Nov 2014 11:53:28 +0100 Subject: [PATCH 1/2] fix($httpBackend): compare timeoutId with undefined in completeRequest httpBackend with ngMock browser.defer can never cancel the first deferredFn because the timeoutId returned by defer for the first fn is a zero value. Compare timeoutId with undefined fix this issue. --- src/ng/httpBackend.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index 742d7aa86a85..5ba78b749932 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -126,7 +126,9 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc function completeRequest(callback, status, response, headersString, statusText) { // cancel timeout and subsequent timeout promise resolution - timeoutId && $browserDefer.cancel(timeoutId); + if (timeoutId !== undefined) { + $browserDefer.cancel(timeoutId); + } jsonpDone = xhr = null; callback(status, response, headersString, statusText); From 1dea0f575c19e8998d6f0672b0ef5c2709cc247a Mon Sep 17 00:00:00 2001 From: Guillaume Pannatier Date: Tue, 18 Nov 2014 11:57:31 +0100 Subject: [PATCH 2/2] Use .defer instead of fakeTimeout --- test/ng/httpBackendSpec.js | 47 ++++++++------------------------------ 1 file changed, 9 insertions(+), 38 deletions(-) diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index 2c1da6eba4bb..12ceed5beb01 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -4,36 +4,7 @@ describe('$httpBackend', function() { var $backend, $browser, callbacks, - 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 = fakeTimeout.ids.indexOf(id); - if (i >= 0) { - fakeTimeout.fns.splice(i, 1); - fakeTimeout.delays.splice(i, 1); - fakeTimeout.ids.splice(i, 1); - return true; - } - return false; - }; + xhr, fakeDocument, callback; beforeEach(inject(function($injector) { @@ -57,7 +28,7 @@ describe('$httpBackend', function() { }) } }; - $backend = createHttpBackend($browser, createMockXhr, fakeTimeout, callbacks, fakeDocument); + $backend = createHttpBackend($browser, createMockXhr, $browser.defer, callbacks, fakeDocument); callback = jasmine.createSpy('done'); })); @@ -154,7 +125,7 @@ describe('$httpBackend', function() { xhr = MockXhr.$$lastInstance; spyOn(xhr, 'abort'); - fakeTimeout.flush(); + $browser.defer.flush(); expect(xhr.abort).toHaveBeenCalledOnce(); xhr.status = 0; @@ -171,9 +142,9 @@ describe('$httpBackend', function() { xhr = MockXhr.$$lastInstance; spyOn(xhr, 'abort'); - expect(fakeTimeout.delays[0]).toBe(2000); + expect($browser.deferredFns[0].time).toBe(2000); - fakeTimeout.flush(); + $browser.defer.flush(); expect(xhr.abort).toHaveBeenCalledOnce(); xhr.status = 0; @@ -227,13 +198,13 @@ describe('$httpBackend', function() { xhr = MockXhr.$$lastInstance; spyOn(xhr, 'abort'); - expect(fakeTimeout.delays[0]).toBe(2000); + expect($browser.deferredFns[0].time).toBe(2000); xhr.status = 200; xhr.onload(); expect(callback).toHaveBeenCalledOnce(); - expect(fakeTimeout.delays.length).toBe(0); + expect($browser.deferredFns.length).toBe(0); expect(xhr.abort).not.toHaveBeenCalled(); }); @@ -342,12 +313,12 @@ describe('$httpBackend', function() { $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); + expect($browser.deferredFns[0].time).toBe(2000); var script = fakeDocument.$$scripts.shift(), callbackId = script.src.match(SCRIPT_URL)[2]; - fakeTimeout.flush(); + $browser.defer.flush(); expect(fakeDocument.$$scripts.length).toBe(0); expect(callback).toHaveBeenCalledOnce();