fix(HTTP/XhrBackend): correctly set the status code on errors (#9355)

fixes #9329
fixes angular/http#54
This commit is contained in:
Victor Berchet 2016-06-20 15:02:14 -07:00 committed by GitHub
parent ba46ca683b
commit 12c49042ab
2 changed files with 24 additions and 2 deletions

View File

@ -80,7 +80,12 @@ export class XHRConnection implements Connection {
}; };
// error event handler // error event handler
let onError = (err: any) => { let onError = (err: any) => {
var responseOptions = new ResponseOptions({body: err, type: ResponseType.Error}); var responseOptions = new ResponseOptions({
body: err,
type: ResponseType.Error,
status: _xhr.status,
statusText: _xhr.statusText,
});
if (isPresent(baseResponseOptions)) { if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions); responseOptions = baseResponseOptions.merge(responseOptions);
} }

View File

@ -181,6 +181,24 @@ export function main() {
existingXHRs[0].dispatchEvent('error'); existingXHRs[0].dispatchEvent('error');
})); }));
it('should set the status text and status code on error',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var connection = new XHRConnection(
sampleRequest, new MockBrowserXHR(),
new ResponseOptions({type: ResponseType.Error}));
connection.response.subscribe(null, (res: Response) => {
expect(res.type).toBe(ResponseType.Error);
expect(res.status).toEqual(0);
expect(res.statusText).toEqual('');
async.done();
});
const xhr = existingXHRs[0];
// status=0 with a text='' is common for CORS errors
xhr.setStatusCode(0);
xhr.setStatusText('');
xhr.dispatchEvent('error');
}));
it('should call open with method and url when subscribed to', () => { it('should call open with method and url when subscribed to', () => {
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR()); var connection = new XHRConnection(sampleRequest, new MockBrowserXHR());
expect(openSpy).not.toHaveBeenCalled(); expect(openSpy).not.toHaveBeenCalled();
@ -188,7 +206,6 @@ export function main() {
expect(openSpy).toHaveBeenCalledWith('GET', sampleRequest.url); expect(openSpy).toHaveBeenCalledWith('GET', sampleRequest.url);
}); });
it('should call send on the backend with request body when subscribed to', () => { it('should call send on the backend with request body when subscribed to', () => {
var body = 'Some body to love'; var body = 'Some body to love';
var base = new BaseRequestOptions(); var base = new BaseRequestOptions();