From 12c49042abed8847a474c445271f9ba9830d3c18 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 20 Jun 2016 15:02:14 -0700 Subject: [PATCH] fix(HTTP/XhrBackend): correctly set the status code on errors (#9355) fixes #9329 fixes angular/http#54 --- .../@angular/http/src/backends/xhr_backend.ts | 7 ++++++- .../http/test/backends/xhr_backend_spec.ts | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/modules/@angular/http/src/backends/xhr_backend.ts b/modules/@angular/http/src/backends/xhr_backend.ts index 5478d190e9..93530ed717 100644 --- a/modules/@angular/http/src/backends/xhr_backend.ts +++ b/modules/@angular/http/src/backends/xhr_backend.ts @@ -80,7 +80,12 @@ export class XHRConnection implements Connection { }; // error event handler 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)) { responseOptions = baseResponseOptions.merge(responseOptions); } diff --git a/modules/@angular/http/test/backends/xhr_backend_spec.ts b/modules/@angular/http/test/backends/xhr_backend_spec.ts index 3569ca8e1b..313609041c 100644 --- a/modules/@angular/http/test/backends/xhr_backend_spec.ts +++ b/modules/@angular/http/test/backends/xhr_backend_spec.ts @@ -181,6 +181,24 @@ export function main() { 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', () => { var connection = new XHRConnection(sampleRequest, new MockBrowserXHR()); expect(openSpy).not.toHaveBeenCalled(); @@ -188,7 +206,6 @@ export function main() { expect(openSpy).toHaveBeenCalledWith('GET', sampleRequest.url); }); - it('should call send on the backend with request body when subscribed to', () => { var body = 'Some body to love'; var base = new BaseRequestOptions();