Skip to content

Commit 3c474ec

Browse files
ajitsinghkalerjosephperrott
authored andcommitted
fix(common): add boolean to valid json for testing (angular#37893)
boolean is a valid json but at present we cannot test Http request using boolean added support for boolean requests Fixes angular#20690 PR Close angular#37893
1 parent ee03408 commit 3c474ec

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

goldens/public-api/common/http/testing/testing.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export declare class TestRequest {
3333
statusText?: string;
3434
}): void;
3535
event(event: HttpEvent<any>): void;
36-
flush(body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[] | null, opts?: {
36+
flush(body: ArrayBuffer | Blob | boolean | string | number | Object | (boolean | string | number | Object | null)[] | null, opts?: {
3737
headers?: HttpHeaders | {
3838
[name: string]: string | string[];
3939
};

packages/common/http/test/client_spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ import {toArray} from 'rxjs/operators';
3131
});
3232
backend.expectOne('/test').flush({'data': 'hello world'});
3333
});
34+
it('should allow flushing requests with a boolean value', (done: DoneFn) => {
35+
client.get('/test').subscribe(res => {
36+
expect((res as any)).toEqual(true);
37+
done();
38+
});
39+
backend.expectOne('/test').flush(true);
40+
});
3441
it('for text data', done => {
3542
client.get('/test', {responseType: 'text'}).subscribe(res => {
3643
expect(res).toEqual('hello world');

packages/common/http/testing/src/request.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ export class TestRequest {
4040
*
4141
* Both successful and unsuccessful responses can be delivered via `flush()`.
4242
*/
43-
flush(body: ArrayBuffer|Blob|string|number|Object|(string|number|Object|null)[]|null, opts: {
44-
headers?: HttpHeaders|{[name: string]: string | string[]},
45-
status?: number,
46-
statusText?: string,
47-
} = {}): void {
43+
flush(
44+
body: ArrayBuffer|Blob|boolean|string|number|Object|(boolean|string|number|Object|null)[]|
45+
null,
46+
opts: {
47+
headers?: HttpHeaders|{[name: string]: string | string[]},
48+
status?: number,
49+
statusText?: string,
50+
} = {}): void {
4851
if (this.cancelled) {
4952
throw new Error(`Cannot flush a cancelled request.`);
5053
}
@@ -146,7 +149,8 @@ function _toBlob(body: ArrayBuffer|Blob|string|number|Object|
146149
* Helper function to convert a response body to JSON data.
147150
*/
148151
function _toJsonBody(
149-
body: ArrayBuffer|Blob|string|number|Object|(string | number | Object | null)[],
152+
body: ArrayBuffer|Blob|boolean|string|number|Object|
153+
(boolean | string | number | Object | null)[],
150154
format: string = 'JSON'): Object|string|number|(Object | string | number)[] {
151155
if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
152156
throw new Error(`Automatic conversion to ${format} is not supported for ArrayBuffers.`);
@@ -155,7 +159,7 @@ function _toJsonBody(
155159
throw new Error(`Automatic conversion to ${format} is not supported for Blobs.`);
156160
}
157161
if (typeof body === 'string' || typeof body === 'number' || typeof body === 'object' ||
158-
Array.isArray(body)) {
162+
typeof body === 'boolean' || Array.isArray(body)) {
159163
return body;
160164
}
161165
throw new Error(`Automatic conversion to ${format} is not supported for response type.`);

0 commit comments

Comments
 (0)