From 89b6ca9c91ebdd4f8d07c75f8668aa888c100e30 Mon Sep 17 00:00:00 2001 From: Aakash Chaudhary Date: Thu, 19 Jan 2017 18:53:07 +0530 Subject: [PATCH 1/6] Adding param xhrStatus to response --- src/ng/http.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index b8d350ea3143..500d0309681d 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -1268,9 +1268,9 @@ function $HttpProvider() { } else { // serving from cache if (isArray(cachedResp)) { - resolvePromise(cachedResp[1], cachedResp[0], shallowCopy(cachedResp[2]), cachedResp[3]); + resolvePromise(cachedResp[1], cachedResp[0], shallowCopy(cachedResp[2]), cachedResp[3], cachedResp[4]); } else { - resolvePromise(cachedResp, 200, {}, 'OK'); + resolvePromise(cachedResp, 200, {}, 'OK', 'Request Completed'); } } } else { @@ -1327,10 +1327,10 @@ function $HttpProvider() { * - resolves the raw $http promise * - calls $apply */ - function done(status, response, headersString, statusText) { + function done(status, response, headersString, statusText, xhrStatus) { if (cache) { if (isSuccess(status)) { - cache.put(url, [status, response, parseHeaders(headersString), statusText]); + cache.put(url, [status, response, parseHeaders(headersString), statusText, xhrStatus]); } else { // remove promise from the cache cache.remove(url); @@ -1338,7 +1338,7 @@ function $HttpProvider() { } function resolveHttpPromise() { - resolvePromise(response, status, headersString, statusText); + resolvePromise(response, status, headersString, statusText, xhrStatus); } if (useApplyAsync) { @@ -1353,7 +1353,7 @@ function $HttpProvider() { /** * Resolves the raw $http promise. */ - function resolvePromise(response, status, headers, statusText) { + function resolvePromise(response, status, headers, statusText, xhrStatus) { //status: HTTP response status code, 0, -1 (aborted by timeout / promise) status = status >= -1 ? status : 0; @@ -1362,12 +1362,13 @@ function $HttpProvider() { status: status, headers: headersGetter(headers), config: config, - statusText: statusText + statusText: statusText, + xhrStatus: xhrStatus }); } function resolvePromiseWithResult(result) { - resolvePromise(result.data, result.status, shallowCopy(result.headers()), result.statusText); + resolvePromise(result.data, result.status, shallowCopy(result.headers()), result.statusText, result.xhrStatus); } function removePendingReq() { From 377a74248f73c5938861ca611b47ed78d9ed2c2c Mon Sep 17 00:00:00 2001 From: Aakash Chaudhary Date: Thu, 19 Jan 2017 18:56:18 +0530 Subject: [PATCH 2/6] Adding separate handlers for xhr Adding param xhrStatus to response and separate handlers for onerror, onabort, ontimeout --- src/ng/httpBackend.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index 501c1de86c73..fe0c8877e7ff 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -64,7 +64,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc var jsonpDone = jsonpReq(url, callbackPath, function(status, text) { // jsonpReq only ever sets status to 200 (OK), 404 (ERROR) or -1 (WAITING) var response = (status === 200) && callbacks.getResponse(callbackPath); - completeRequest(callback, status, response, '', text); + completeRequest(callback, status, response, '', text, ''); callbacks.removeCallback(callbackPath); }); } else { @@ -99,18 +99,27 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc status, response, xhr.getAllResponseHeaders(), - statusText); + statusText, + 'Request Completed'); }; var requestError = function() { // The response is always empty // See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error - completeRequest(callback, -1, null, null, ''); + completeRequest(callback, -1, null, null, '', 'Request Error'); + }; + + var requestAborted = function() { + completeRequest(callback, -1, null, null, '', 'Request Aborted'); + }; + + var requestTimedOut = function() { + completeRequest(callback, -1, null, null, '', 'Request Timed Out'); }; xhr.onerror = requestError; - xhr.onabort = requestError; - xhr.ontimeout = requestError; + xhr.onabort = requestAborted; + xhr.ontimeout = requestTimedOut; forEach(eventHandlers, function(value, key) { xhr.addEventListener(key, value); @@ -160,14 +169,14 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc } } - function completeRequest(callback, status, response, headersString, statusText) { + function completeRequest(callback, status, response, headersString, statusText, xhrStatus) { // cancel timeout and subsequent timeout promise resolution if (isDefined(timeoutId)) { $browserDefer.cancel(timeoutId); } jsonpDone = xhr = null; - callback(status, response, headersString, statusText); + callback(status, response, headersString, statusText, xhrStatus); } }; From 6e3628a1b0f3aab691b3be2ce3aa716b47f432a6 Mon Sep 17 00:00:00 2001 From: Aakash Chaudhary Date: Thu, 19 Jan 2017 19:10:58 +0530 Subject: [PATCH 3/6] Updating for xhrStatus changes --- test/ng/httpBackendSpec.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index fbc58072894f..78cc5ea29e67 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -90,7 +90,7 @@ describe('$httpBackend', function() { }); it('should call completion function with xhr.statusText if present', function() { - callback.and.callFake(function(status, response, headers, statusText) { + callback.and.callFake(function(status, response, headers, statusText, xhrStatus) { expect(statusText).toBe('OK'); }); @@ -102,7 +102,7 @@ describe('$httpBackend', function() { }); it('should call completion function with empty string if not present', function() { - callback.and.callFake(function(status, response, headers, statusText) { + callback.and.callFake(function(status, response, headers, statusText, xhrStatus) { expect(statusText).toBe(''); }); @@ -155,11 +155,12 @@ describe('$httpBackend', function() { }); it('should not try to read response data when request is aborted', function() { - callback.and.callFake(function(status, response, headers, statusText) { + callback.and.callFake(function(status, response, headers, statusText, xhrStatus) { expect(status).toBe(-1); expect(response).toBe(null); expect(headers).toBe(null); expect(statusText).toBe(''); + expect(xhrStatus).toBe('Request Aborted'); }); $backend('GET', '/url', null, callback, {}, 2000); xhr = MockXhr.$$lastInstance; @@ -174,11 +175,12 @@ describe('$httpBackend', function() { }); it('should complete the request on timeout', function() { - callback.and.callFake(function(status, response, headers, statusText) { + callback.and.callFake(function(status, response, headers, statusText, xhrStatus) { expect(status).toBe(-1); expect(response).toBe(null); expect(headers).toBe(null); expect(statusText).toBe(''); + expect(xhrStatus).toBe('Request Timed Out'); }); $backend('GET', '/url', null, callback, {}); xhr = MockXhr.$$lastInstance; @@ -511,4 +513,3 @@ describe('$httpBackend', function() { }); }); }); - From 28069435cc5ce58159ceb0e0cddc933d86f63e78 Mon Sep 17 00:00:00 2001 From: Aakash Chaudhary Date: Fri, 27 Jan 2017 13:04:09 +0530 Subject: [PATCH 4/6] Concise xhr status --- src/ng/httpBackend.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index fe0c8877e7ff..aaeb9def4388 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -100,21 +100,21 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc response, xhr.getAllResponseHeaders(), statusText, - 'Request Completed'); + 'success'); }; var requestError = function() { // The response is always empty // See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error - completeRequest(callback, -1, null, null, '', 'Request Error'); + completeRequest(callback, -1, null, null, '', 'error'); }; var requestAborted = function() { - completeRequest(callback, -1, null, null, '', 'Request Aborted'); + completeRequest(callback, -1, null, null, '', 'abort'); }; var requestTimedOut = function() { - completeRequest(callback, -1, null, null, '', 'Request Timed Out'); + completeRequest(callback, -1, null, null, '', 'timeout'); }; xhr.onerror = requestError; From c5e0a31d766fea3b6886da012ede87751c685cfd Mon Sep 17 00:00:00 2001 From: Aakash Chaudhary Date: Fri, 27 Jan 2017 13:07:00 +0530 Subject: [PATCH 5/6] Concise xhr status --- src/ng/http.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ng/http.js b/src/ng/http.js index 500d0309681d..dca49b892dac 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -1270,7 +1270,7 @@ function $HttpProvider() { if (isArray(cachedResp)) { resolvePromise(cachedResp[1], cachedResp[0], shallowCopy(cachedResp[2]), cachedResp[3], cachedResp[4]); } else { - resolvePromise(cachedResp, 200, {}, 'OK', 'Request Completed'); + resolvePromise(cachedResp, 200, {}, 'OK', 'success'); } } } else { From 7d13e738f22759d87739424d2f7f89491d99afab Mon Sep 17 00:00:00 2001 From: Aakash Chaudhary Date: Fri, 27 Jan 2017 13:17:08 +0530 Subject: [PATCH 6/6] Concise xhr status --- test/ng/httpBackendSpec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index 78cc5ea29e67..f0f3a65d6dc8 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -90,7 +90,7 @@ describe('$httpBackend', function() { }); it('should call completion function with xhr.statusText if present', function() { - callback.and.callFake(function(status, response, headers, statusText, xhrStatus) { + callback.and.callFake(function(status, response, headers, statusText) { expect(statusText).toBe('OK'); }); @@ -102,7 +102,7 @@ describe('$httpBackend', function() { }); it('should call completion function with empty string if not present', function() { - callback.and.callFake(function(status, response, headers, statusText, xhrStatus) { + callback.and.callFake(function(status, response, headers, statusText) { expect(statusText).toBe(''); }); @@ -160,7 +160,7 @@ describe('$httpBackend', function() { expect(response).toBe(null); expect(headers).toBe(null); expect(statusText).toBe(''); - expect(xhrStatus).toBe('Request Aborted'); + expect(xhrStatus).toBe('abort'); }); $backend('GET', '/url', null, callback, {}, 2000); xhr = MockXhr.$$lastInstance; @@ -180,7 +180,7 @@ describe('$httpBackend', function() { expect(response).toBe(null); expect(headers).toBe(null); expect(statusText).toBe(''); - expect(xhrStatus).toBe('Request Timed Out'); + expect(xhrStatus).toBe('timeout'); }); $backend('GET', '/url', null, callback, {}); xhr = MockXhr.$$lastInstance;