Skip to content

Commit 8b14488

Browse files
committed
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
1 parent f9fa157 commit 8b14488

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

packages/common/http/testing/BUILD.bazel

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ load("//tools:defaults.bzl", "ts_library")
55
ts_library(
66
name = "testing",
77
testonly = 1,
8-
srcs = glob(["**/*.ts"]),
8+
srcs = glob(
9+
[
10+
"*.ts",
11+
"src/**/*.ts",
12+
],
13+
),
914
module_name = "@angular/common/http/testing",
1015
deps = [
1116
"//packages/common/http",

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

+3-12
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,17 @@ function _maybeConvertBody(
184184
responseType: string, body: ArrayBuffer | Blob | string | number | Object |
185185
(string | number | Object | null)[] | null): ArrayBuffer|Blob|string|number|Object|
186186
(string | number | Object | null)[]|null {
187+
if (body === null) {
188+
return null;
189+
}
187190
switch (responseType) {
188191
case 'arraybuffer':
189-
if (body === null) {
190-
return null;
191-
}
192192
return _toArrayBufferBody(body);
193193
case 'blob':
194-
if (body === null) {
195-
return null;
196-
}
197194
return _toBlob(body);
198195
case 'json':
199-
if (body === null) {
200-
return 'null';
201-
}
202196
return _toJsonBody(body);
203197
case 'text':
204-
if (body === null) {
205-
return null;
206-
}
207198
return _toTextBody(body);
208199
default:
209200
throw new Error(`Unsupported responseType: ${responseType}`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal';
10+
11+
import {HttpClient} from '../../src/client';
12+
import {HttpClientTestingBackend} from '../src/backend';
13+
14+
describe('HttpClient TestRequest', () => {
15+
it('accepts a null body', () => {
16+
const mock = new HttpClientTestingBackend();
17+
const client = new HttpClient(mock);
18+
19+
let resp: any;
20+
client.post('/some-url', {test: 'test'}).subscribe(body => { resp = body; });
21+
22+
const req = mock.expectOne('/some-url');
23+
req.flush(null);
24+
25+
expect(resp).toBeNull();
26+
});
27+
});

0 commit comments

Comments
 (0)