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
This commit is contained in:
Ajit Singh 2020-07-02 20:48:13 +05:30 committed by Joey Perrott
parent ee03408c44
commit 3c474ecf56
3 changed files with 19 additions and 8 deletions

View File

@ -33,7 +33,7 @@ export declare class TestRequest {
statusText?: string; statusText?: string;
}): void; }): void;
event(event: HttpEvent<any>): void; event(event: HttpEvent<any>): 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 | { headers?: HttpHeaders | {
[name: string]: string | string[]; [name: string]: string | string[];
}; };

View File

@ -31,6 +31,13 @@ import {toArray} from 'rxjs/operators';
}); });
backend.expectOne('/test').flush({'data': 'hello world'}); 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 => { it('for text data', done => {
client.get('/test', {responseType: 'text'}).subscribe(res => { client.get('/test', {responseType: 'text'}).subscribe(res => {
expect(res).toEqual('hello world'); expect(res).toEqual('hello world');

View File

@ -40,11 +40,14 @@ export class TestRequest {
* *
* Both successful and unsuccessful responses can be delivered via `flush()`. * Both successful and unsuccessful responses can be delivered via `flush()`.
*/ */
flush(body: ArrayBuffer|Blob|string|number|Object|(string|number|Object|null)[]|null, opts: { flush(
headers?: HttpHeaders|{[name: string]: string | string[]}, body: ArrayBuffer|Blob|boolean|string|number|Object|(boolean|string|number|Object|null)[]|
status?: number, null,
statusText?: string, opts: {
} = {}): void { headers?: HttpHeaders|{[name: string]: string | string[]},
status?: number,
statusText?: string,
} = {}): void {
if (this.cancelled) { if (this.cancelled) {
throw new Error(`Cannot flush a cancelled request.`); 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. * Helper function to convert a response body to JSON data.
*/ */
function _toJsonBody( 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)[] { format: string = 'JSON'): Object|string|number|(Object | string | number)[] {
if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) { if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
throw new Error(`Automatic conversion to ${format} is not supported for ArrayBuffers.`); 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.`); throw new Error(`Automatic conversion to ${format} is not supported for Blobs.`);
} }
if (typeof body === 'string' || typeof body === 'number' || typeof body === 'object' || if (typeof body === 'string' || typeof body === 'number' || typeof body === 'object' ||
Array.isArray(body)) { typeof body === 'boolean' || Array.isArray(body)) {
return body; return body;
} }
throw new Error(`Automatic conversion to ${format} is not supported for response type.`); throw new Error(`Automatic conversion to ${format} is not supported for response type.`);