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
This commit is contained in:
Flounn 2016-09-23 22:44:01 +02:00 committed by Rado Kirov
parent f1b6c6efa1
commit 5ab5cc77bb
2 changed files with 19 additions and 3 deletions

View File

@ -54,9 +54,8 @@ export class XHRConnection implements Connection {
let onLoad = () => { let onLoad = () => {
// responseText is the old-school way of retrieving response (supported by IE8 & 9) // responseText is the old-school way of retrieving response (supported by IE8 & 9)
// response/responseType properties were introduced in ResourceLoader Level2 spec (supported // response/responseType properties were introduced in ResourceLoader Level2 spec (supported
// by // by IE10)
// IE10) let body = _xhr.response === undefined ? _xhr.responseText : _xhr.response;
let body = isPresent(_xhr.response) ? _xhr.response : _xhr.responseText;
// Implicitly strip a potential XSSI prefix. // Implicitly strip a potential XSSI prefix.
if (isString(body)) body = body.replace(XSSI_PREFIX, ''); if (isString(body)) body = body.replace(XSSI_PREFIX, '');
let headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders()); let headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders());

View File

@ -686,6 +686,23 @@ Connection: keep-alive`;
existingXHRs[0].setStatusCode(statusCode); existingXHRs[0].setStatusCode(statusCode);
existingXHRs[0].dispatchEvent('load'); 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');
}));
}); });
}); });
} }