From 8b14488827787faaa2638b95d96fcbe71861ca8e Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Tue, 9 Jan 2018 08:25:01 -0800 Subject: [PATCH] fix(common): don't convert null to a string when flushing a mock request (#21417) A bug in TestRequest caused null response bodies to be stringified. This change causes null to be treated faithfully. Fixes #20744 PR Close #21417 --- packages/common/http/testing/BUILD.bazel | 7 ++++- packages/common/http/testing/src/request.ts | 15 +++-------- .../common/http/testing/test/request_spec.ts | 27 +++++++++++++++++++ 3 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 packages/common/http/testing/test/request_spec.ts diff --git a/packages/common/http/testing/BUILD.bazel b/packages/common/http/testing/BUILD.bazel index 4fa20c9ee8..c5a212822a 100644 --- a/packages/common/http/testing/BUILD.bazel +++ b/packages/common/http/testing/BUILD.bazel @@ -5,7 +5,12 @@ load("//tools:defaults.bzl", "ts_library") ts_library( name = "testing", testonly = 1, - srcs = glob(["**/*.ts"]), + srcs = glob( + [ + "*.ts", + "src/**/*.ts", + ], + ), module_name = "@angular/common/http/testing", deps = [ "//packages/common/http", diff --git a/packages/common/http/testing/src/request.ts b/packages/common/http/testing/src/request.ts index f37c76ee39..e765fd69f5 100644 --- a/packages/common/http/testing/src/request.ts +++ b/packages/common/http/testing/src/request.ts @@ -184,26 +184,17 @@ function _maybeConvertBody( responseType: string, body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[] | null): ArrayBuffer|Blob|string|number|Object| (string | number | Object | null)[]|null { + if (body === null) { + return null; + } switch (responseType) { case 'arraybuffer': - if (body === null) { - return null; - } return _toArrayBufferBody(body); case 'blob': - if (body === null) { - return null; - } return _toBlob(body); case 'json': - if (body === null) { - return 'null'; - } return _toJsonBody(body); case 'text': - if (body === null) { - return null; - } return _toTextBody(body); default: throw new Error(`Unsupported responseType: ${responseType}`); diff --git a/packages/common/http/testing/test/request_spec.ts b/packages/common/http/testing/test/request_spec.ts new file mode 100644 index 0000000000..111a4bfaad --- /dev/null +++ b/packages/common/http/testing/test/request_spec.ts @@ -0,0 +1,27 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal'; + +import {HttpClient} from '../../src/client'; +import {HttpClientTestingBackend} from '../src/backend'; + +describe('HttpClient TestRequest', () => { + it('accepts a null body', () => { + const mock = new HttpClientTestingBackend(); + const client = new HttpClient(mock); + + let resp: any; + client.post('/some-url', {test: 'test'}).subscribe(body => { resp = body; }); + + const req = mock.expectOne('/some-url'); + req.flush(null); + + expect(resp).toBeNull(); + }); +}); \ No newline at end of file