From 3019140e7eb89a380618c0a9fab9e9da04397539 Mon Sep 17 00:00:00 2001 From: Aliaksei Palkanau Date: Sun, 13 Sep 2015 19:31:56 +0300 Subject: [PATCH] feat(http): set the statusText property from the XMLHttpRequest instance Closes #4162 --- .../@angular/http/src/backends/xhr_backend.ts | 5 ++- .../http/test/backends/xhr_backend_spec.ts | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/modules/@angular/http/src/backends/xhr_backend.ts b/modules/@angular/http/src/backends/xhr_backend.ts index db6a87e4db..dd418ceb1f 100644 --- a/modules/@angular/http/src/backends/xhr_backend.ts +++ b/modules/@angular/http/src/backends/xhr_backend.ts @@ -58,7 +58,10 @@ export class XHRConnection implements Connection { if (status === 0) { status = body ? 200 : 0; } - var responseOptions = new ResponseOptions({body, status, headers, url}); + + let statusText = _xhr.statusText || 'OK'; + + var responseOptions = new ResponseOptions({body, status, headers, statusText, url}); 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 bbc5b5ced9..d290391fcf 100644 --- a/modules/@angular/http/test/backends/xhr_backend_spec.ts +++ b/modules/@angular/http/test/backends/xhr_backend_spec.ts @@ -40,6 +40,8 @@ class MockBrowserXHR extends BrowserXhr { status: number; responseHeaders: string; responseURL: string; + statusText: string; + constructor() { super(); var spy = new SpyObject(); @@ -51,6 +53,8 @@ class MockBrowserXHR extends BrowserXhr { setStatusCode(status: number) { this.status = status; } + setStatusText(statusText: string) { this.statusText = statusText; } + setResponse(value: string) { this.response = value; } setResponseText(value: string) { this.responseText = value; } @@ -354,6 +358,35 @@ export function main() { existingXHRs[0].setStatusCode(statusCode); existingXHRs[0].dispatchEvent('load'); })); + + it('should set the status text property from the XMLHttpRequest instance if present', + inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { + var statusText = 'test'; + var connection = new XHRConnection(sampleRequest, new MockBrowserXHR()); + + connection.response.subscribe((res: Response) => { + expect(res.statusText).toBe(statusText); + async.done(); + }); + + existingXHRs[0].setStatusText(statusText); + existingXHRs[0].setStatusCode(200); + existingXHRs[0].dispatchEvent('load'); + })); + + it('should set status text to "OK" if it is not present in XMLHttpRequest instance', + inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { + var connection = new XHRConnection(sampleRequest, new MockBrowserXHR()); + + connection.response.subscribe((res: Response) => { + expect(res.statusText).toBe('OK'); + async.done(); + }); + + existingXHRs[0].setStatusCode(200); + existingXHRs[0].dispatchEvent('load'); + })); + }); }); }