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
This commit is contained in:
parent
f9fa157a09
commit
8b14488827
|
@ -5,7 +5,12 @@ load("//tools:defaults.bzl", "ts_library")
|
||||||
ts_library(
|
ts_library(
|
||||||
name = "testing",
|
name = "testing",
|
||||||
testonly = 1,
|
testonly = 1,
|
||||||
srcs = glob(["**/*.ts"]),
|
srcs = glob(
|
||||||
|
[
|
||||||
|
"*.ts",
|
||||||
|
"src/**/*.ts",
|
||||||
|
],
|
||||||
|
),
|
||||||
module_name = "@angular/common/http/testing",
|
module_name = "@angular/common/http/testing",
|
||||||
deps = [
|
deps = [
|
||||||
"//packages/common/http",
|
"//packages/common/http",
|
||||||
|
|
|
@ -184,26 +184,17 @@ function _maybeConvertBody(
|
||||||
responseType: string, body: ArrayBuffer | Blob | string | number | Object |
|
responseType: string, body: ArrayBuffer | Blob | string | number | Object |
|
||||||
(string | number | Object | null)[] | null): ArrayBuffer|Blob|string|number|Object|
|
(string | number | Object | null)[] | null): ArrayBuffer|Blob|string|number|Object|
|
||||||
(string | number | Object | null)[]|null {
|
(string | number | Object | null)[]|null {
|
||||||
|
if (body === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
switch (responseType) {
|
switch (responseType) {
|
||||||
case 'arraybuffer':
|
case 'arraybuffer':
|
||||||
if (body === null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return _toArrayBufferBody(body);
|
return _toArrayBufferBody(body);
|
||||||
case 'blob':
|
case 'blob':
|
||||||
if (body === null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return _toBlob(body);
|
return _toBlob(body);
|
||||||
case 'json':
|
case 'json':
|
||||||
if (body === null) {
|
|
||||||
return 'null';
|
|
||||||
}
|
|
||||||
return _toJsonBody(body);
|
return _toJsonBody(body);
|
||||||
case 'text':
|
case 'text':
|
||||||
if (body === null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return _toTextBody(body);
|
return _toTextBody(body);
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unsupported responseType: ${responseType}`);
|
throw new Error(`Unsupported responseType: ${responseType}`);
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue