From c882b26062d4030f48d7c50d3e469669b910ea9f Mon Sep 17 00:00:00 2001 From: Gonzalo Ruiz de Villa Date: Wed, 13 Nov 2013 21:24:15 +0100 Subject: [PATCH] fix(httpBackend): should not read response data when request is aborted When a request is aborted, it makes no sense to read the response headers or text. Also in IE9, trying to read data (either response headers or text) from an aborted request throws an Error c00c023f. Fixes #4913 --- src/ng/httpBackend.js | 12 +++++++++--- test/ng/httpBackendSpec.js | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index dcb15f77a7f6..aa9e3f217124 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -69,13 +69,19 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, // always async xhr.onreadystatechange = function() { if (xhr.readyState == 4) { - var responseHeaders = xhr.getAllResponseHeaders(); + var responseHeaders = null, + response = null; + + if(!xhr.aborted) { + responseHeaders = xhr.getAllResponseHeaders(); + response = xhr.responseType ? xhr.response : xhr.responseText; + } // responseText is the old-school way of retrieving response (supported by IE8 & 9) // response/responseType properties were introduced in XHR Level2 spec (supported by IE10) completeRequest(callback, status || xhr.status, - (xhr.responseType ? xhr.response : xhr.responseText), + response, responseHeaders); } }; @@ -101,7 +107,7 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, function timeoutRequest() { status = -1; jsonpDone && jsonpDone(); - xhr && xhr.abort(); + xhr && (xhr.aborted = true) && xhr.abort(); } function completeRequest(callback, status, response, headersString) { diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index f39e83eae139..dd23a07d9232 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -118,6 +118,25 @@ describe('$httpBackend', function() { }); }); + it('should not try to read response data when request is aborted', function() { + callback.andCallFake(function(status, response, headers) { + expect(status).toBe(-1); + expect(response).toBe(null); + expect(headers).toBe(null); + }); + $backend('GET', '/url', null, callback, {}, 2000); + xhr = MockXhr.$$lastInstance; + spyOn(xhr, 'abort'); + + fakeTimeout.flush(); + expect(xhr.abort).toHaveBeenCalledOnce(); + + xhr.status = 0; + xhr.readyState = 4; + xhr.onreadystatechange(); + expect(callback).toHaveBeenCalledOnce(); + }); + it('should abort request on timeout', function() { callback.andCallFake(function(status, response) { expect(status).toBe(-1);