From 15a54df7d3282b71fc8ffd6c5545aa9bdd7f4201 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Wed, 25 Oct 2017 15:55:36 -0700 Subject: [PATCH] fix(common): accept falsy values as HTTP bodies (#19958) Previously, HttpClient used the overly clever test "body || null" to determine when a body parameter was provided. This breaks when the valid bodies '0' or 'false' are provided. This change tests directly against 'undefined' to detect the presence of the body parameter, and thus correctly allows falsy values through. Fixes #19825. Fixes #19195. PR Close #19958 --- packages/common/http/src/client.ts | 2 +- packages/common/http/src/request.ts | 2 +- packages/common/http/src/response.ts | 2 +- packages/common/http/test/client_spec.ts | 20 ++++++++++++++++++++ packages/common/http/test/response_spec.ts | 4 ++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/common/http/src/client.ts b/packages/common/http/src/client.ts index 970897a1a5..042ba5a8c9 100644 --- a/packages/common/http/src/client.ts +++ b/packages/common/http/src/client.ts @@ -371,7 +371,7 @@ export class HttpClient { } // Construct the request. - req = new HttpRequest(first, url !, options.body || null, { + req = new HttpRequest(first, url !, (options.body !== undefined ? options.body : null), { headers, params, reportProgress: options.reportProgress, diff --git a/packages/common/http/src/request.ts b/packages/common/http/src/request.ts index 5494d3c8c6..7bfc6d23d4 100644 --- a/packages/common/http/src/request.ts +++ b/packages/common/http/src/request.ts @@ -171,7 +171,7 @@ export class HttpRequest { // the body argument is to use a known no-body method like GET. if (mightHaveBody(this.method) || !!fourth) { // Body is the third argument, options are the fourth. - this.body = third as T || null; + this.body = (third !== undefined) ? third as T : null; options = fourth; } else { // No body required, options are the third argument. The body stays null. diff --git a/packages/common/http/src/response.ts b/packages/common/http/src/response.ts index 961e5dced6..b30cdc8d0b 100644 --- a/packages/common/http/src/response.ts +++ b/packages/common/http/src/response.ts @@ -262,7 +262,7 @@ export class HttpResponse extends HttpResponseBase { body?: T | null, headers?: HttpHeaders; status?: number; statusText?: string; url?: string; } = {}) { super(init); - this.body = init.body || null; + this.body = init.body !== undefined ? init.body : null; } readonly type: HttpEventType.Response = HttpEventType.Response; diff --git a/packages/common/http/test/client_spec.ts b/packages/common/http/test/client_spec.ts index 73ee65119d..b2067d6330 100644 --- a/packages/common/http/test/client_spec.ts +++ b/packages/common/http/test/client_spec.ts @@ -115,6 +115,26 @@ export function main() { expect(testReq.request.body).toBe(body); testReq.flush('hello world'); }); + it('with a json body of false', (done: DoneFn) => { + client.post('/test', false, {observe: 'response', responseType: 'text'}).subscribe(res => { + expect(res.ok).toBeTruthy(); + expect(res.status).toBe(200); + done(); + }); + const testReq = backend.expectOne('/test'); + expect(testReq.request.body).toBe(false); + testReq.flush('hello world'); + }); + it('with a json body of 0', (done: DoneFn) => { + client.post('/test', 0, {observe: 'response', responseType: 'text'}).subscribe(res => { + expect(res.ok).toBeTruthy(); + expect(res.status).toBe(200); + done(); + }); + const testReq = backend.expectOne('/test'); + expect(testReq.request.body).toBe(0); + testReq.flush('hello world'); + }); it('with an arraybuffer', (done: DoneFn) => { const body = new ArrayBuffer(4); client.post('/test', body, {observe: 'response', responseType: 'text'}).subscribe(res => { diff --git a/packages/common/http/test/response_spec.ts b/packages/common/http/test/response_spec.ts index 9f66b5294d..50eb29b7b2 100644 --- a/packages/common/http/test/response_spec.ts +++ b/packages/common/http/test/response_spec.ts @@ -40,6 +40,10 @@ export function main() { expect(resp.ok).toBeTruthy(); expect(resp.url).toBeNull(); }); + it('accepts a falsy body', () => { + expect(new HttpResponse({body: false}).body).toEqual(false); + expect(new HttpResponse({body: 0}).body).toEqual(0); + }); }); it('.ok is determined by status', () => { const good = new HttpResponse({status: 200});