From 3c474ecf562342762acd30c58799d2d223263334 Mon Sep 17 00:00:00 2001 From: Ajit Singh Date: Thu, 2 Jul 2020 20:48:13 +0530 Subject: [PATCH] fix(common): add boolean to valid json for testing (#37893) boolean is a valid json but at present we cannot test Http request using boolean added support for boolean requests Fixes #20690 PR Close #37893 --- .../common/http/testing/testing.d.ts | 2 +- packages/common/http/test/client_spec.ts | 7 +++++++ packages/common/http/testing/src/request.ts | 18 +++++++++++------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/goldens/public-api/common/http/testing/testing.d.ts b/goldens/public-api/common/http/testing/testing.d.ts index 6e896345e2..8143ffd6a0 100644 --- a/goldens/public-api/common/http/testing/testing.d.ts +++ b/goldens/public-api/common/http/testing/testing.d.ts @@ -33,7 +33,7 @@ export declare class TestRequest { statusText?: string; }): void; event(event: HttpEvent): void; - flush(body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[] | null, opts?: { + flush(body: ArrayBuffer | Blob | boolean | string | number | Object | (boolean | string | number | Object | null)[] | null, opts?: { headers?: HttpHeaders | { [name: string]: string | string[]; }; diff --git a/packages/common/http/test/client_spec.ts b/packages/common/http/test/client_spec.ts index 7ecbdef8b4..f76c1a2389 100644 --- a/packages/common/http/test/client_spec.ts +++ b/packages/common/http/test/client_spec.ts @@ -31,6 +31,13 @@ import {toArray} from 'rxjs/operators'; }); backend.expectOne('/test').flush({'data': 'hello world'}); }); + it('should allow flushing requests with a boolean value', (done: DoneFn) => { + client.get('/test').subscribe(res => { + expect((res as any)).toEqual(true); + done(); + }); + backend.expectOne('/test').flush(true); + }); it('for text data', done => { client.get('/test', {responseType: 'text'}).subscribe(res => { expect(res).toEqual('hello world'); diff --git a/packages/common/http/testing/src/request.ts b/packages/common/http/testing/src/request.ts index 66f760b06b..bdd9121784 100644 --- a/packages/common/http/testing/src/request.ts +++ b/packages/common/http/testing/src/request.ts @@ -40,11 +40,14 @@ export class TestRequest { * * Both successful and unsuccessful responses can be delivered via `flush()`. */ - flush(body: ArrayBuffer|Blob|string|number|Object|(string|number|Object|null)[]|null, opts: { - headers?: HttpHeaders|{[name: string]: string | string[]}, - status?: number, - statusText?: string, - } = {}): void { + flush( + body: ArrayBuffer|Blob|boolean|string|number|Object|(boolean|string|number|Object|null)[]| + null, + opts: { + headers?: HttpHeaders|{[name: string]: string | string[]}, + status?: number, + statusText?: string, + } = {}): void { if (this.cancelled) { throw new Error(`Cannot flush a cancelled request.`); } @@ -146,7 +149,8 @@ function _toBlob(body: ArrayBuffer|Blob|string|number|Object| * Helper function to convert a response body to JSON data. */ function _toJsonBody( - body: ArrayBuffer|Blob|string|number|Object|(string | number | Object | null)[], + body: ArrayBuffer|Blob|boolean|string|number|Object| + (boolean | string | number | Object | null)[], format: string = 'JSON'): Object|string|number|(Object | string | number)[] { if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) { throw new Error(`Automatic conversion to ${format} is not supported for ArrayBuffers.`); @@ -155,7 +159,7 @@ function _toJsonBody( throw new Error(`Automatic conversion to ${format} is not supported for Blobs.`); } if (typeof body === 'string' || typeof body === 'number' || typeof body === 'object' || - Array.isArray(body)) { + typeof body === 'boolean' || Array.isArray(body)) { return body; } throw new Error(`Automatic conversion to ${format} is not supported for response type.`);