From 5ab5cc77bb22654a91124d6941c04db0565d9aed Mon Sep 17 00:00:00 2001 From: Flounn Date: Fri, 23 Sep 2016 22:44:01 +0200 Subject: [PATCH] Fix(http): invalidStateError if response body without content (#11786) Fix(http): invalidStateError if response body without content If the responseType has been specified and other than 'text', responseText throw an InvalidStateError exception See XHR doc => https://xhr.spec.whatwg.org/#the-responsetext-attribute Unit Test to prevent invalidStateError --- .../@angular/http/src/backends/xhr_backend.ts | 5 ++--- .../http/test/backends/xhr_backend_spec.ts | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/modules/@angular/http/src/backends/xhr_backend.ts b/modules/@angular/http/src/backends/xhr_backend.ts index 416cf552b3..90d4f32912 100644 --- a/modules/@angular/http/src/backends/xhr_backend.ts +++ b/modules/@angular/http/src/backends/xhr_backend.ts @@ -54,9 +54,8 @@ export class XHRConnection implements Connection { let onLoad = () => { // responseText is the old-school way of retrieving response (supported by IE8 & 9) // response/responseType properties were introduced in ResourceLoader Level2 spec (supported - // by - // IE10) - let body = isPresent(_xhr.response) ? _xhr.response : _xhr.responseText; + // by IE10) + let body = _xhr.response === undefined ? _xhr.responseText : _xhr.response; // Implicitly strip a potential XSSI prefix. if (isString(body)) body = body.replace(XSSI_PREFIX, ''); let headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders()); diff --git a/modules/@angular/http/test/backends/xhr_backend_spec.ts b/modules/@angular/http/test/backends/xhr_backend_spec.ts index 78cb2bec60..0b74c4b14a 100644 --- a/modules/@angular/http/test/backends/xhr_backend_spec.ts +++ b/modules/@angular/http/test/backends/xhr_backend_spec.ts @@ -686,6 +686,23 @@ Connection: keep-alive`; existingXHRs[0].setStatusCode(statusCode); existingXHRs[0].dispatchEvent('load'); })); + + it('should not throw invalidStateError if response without body and responseType not equal to text', + inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { + const base = new BaseRequestOptions(); + const connection = new XHRConnection( + new Request( + base.merge(new RequestOptions({responseType: ResponseContentType.Json}))), + new MockBrowserXHR()); + + connection.response.subscribe((res: Response) => { + expect(res.json()).toBe(null); + async.done(); + }); + + existingXHRs[0].setStatusCode(204); + existingXHRs[0].dispatchEvent('load'); + })); }); }); }