diff --git a/packages/common/http/test/client_spec.ts b/packages/common/http/test/client_spec.ts index 2a68280294..73f1c70e97 100644 --- a/packages/common/http/test/client_spec.ts +++ b/packages/common/http/test/client_spec.ts @@ -12,7 +12,7 @@ import 'rxjs/add/operator/toPromise'; import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal'; import {HttpClient} from '../src/client'; -import {HttpEventType, HttpResponse} from '../src/response'; +import {HttpErrorResponse, HttpEventType, HttpResponse} from '../src/response'; import {HttpClientTestingBackend} from '../testing/src/backend'; export function main() { @@ -123,5 +123,15 @@ export function main() { .flush('hello world'); }); }); + describe('makes a request for an error response', () => { + it('with a JSON body', (done: DoneFn) => { + client.get('/test').subscribe(() => {}, (res: HttpErrorResponse) => { + expect(res.error.data).toEqual('hello world'); + done(); + }); + backend.expectOne('/test').flush( + {'data': 'hello world'}, {status: 500, statusText: 'Server error'}); + }); + }); }); -} \ No newline at end of file +} diff --git a/packages/common/http/testing/src/request.ts b/packages/common/http/testing/src/request.ts index 4bd68a1edd..796f661f1e 100644 --- a/packages/common/http/testing/src/request.ts +++ b/packages/common/http/testing/src/request.ts @@ -61,12 +61,11 @@ export class TestRequest { if (statusText === undefined) { throw new Error('statusText is required when setting a custom status.'); } - const res = {body, headers, status, statusText, url}; if (status >= 200 && status < 300) { - this.observer.next(new HttpResponse(res)); + this.observer.next(new HttpResponse({body, headers, status, statusText, url})); this.observer.complete(); } else { - this.observer.error(new HttpErrorResponse(res)); + this.observer.error(new HttpErrorResponse({error: body, headers, status, statusText, url})); } }