fix(http): use serializeBody to support JSON payload in FetchBackend (#50776)

`HttpRequest.serializeBody` was used in HttpXhrBackend. `fetch` also needs to serialize request body.

Close #50775

PR Close #50776
This commit is contained in:
Heo Sangmin 2023-06-20 20:41:11 +09:00 committed by Andrew Kushnir
parent 64745a89b2
commit a126cbcf22
3 changed files with 18 additions and 1 deletions

View file

@ -221,7 +221,7 @@ export class FetchBackend implements HttpBackend {
}
return {
body: req.body,
body: req.serializeBody(),
method: req.method,
headers,
credentials,

View file

@ -33,6 +33,10 @@ const TEST_POST = new HttpRequest('POST', '/test', 'some body', {
responseType: 'text',
});
const TEST_POST_WITH_JSON_BODY = new HttpRequest('POST', '/test', {'some': 'body'}, {
responseType: 'text',
});
const XSSI_PREFIX = ')]}\'\n';
describe('FetchBackend', async () => {
@ -109,6 +113,11 @@ describe('FetchBackend', async () => {
expect(fetchMock.request.body).toBe('some body');
});
it('sets outgoing body correctly when request payload is json', () => {
callFetchAndFlush(TEST_POST_WITH_JSON_BODY);
expect(fetchMock.request.body).toBe('{"some":"body"}');
});
it('sets outgoing headers, including default headers', () => {
const post = TEST_POST.clone({
setHeaders: {

View file

@ -24,6 +24,10 @@ const TEST_POST = new HttpRequest('POST', '/test', 'some body', {
responseType: 'text',
});
const TEST_POST_WITH_JSON_BODY = new HttpRequest('POST', '/test', {'some': 'body'}, {
responseType: 'text',
});
const XSSI_PREFIX = ')]}\'\n';
{
@ -49,6 +53,10 @@ const XSSI_PREFIX = ')]}\'\n';
backend.handle(TEST_POST).subscribe();
expect(factory.mock.body).toBe('some body');
});
it('sets outgoing body correctly when request payload is json', () => {
backend.handle(TEST_POST_WITH_JSON_BODY).subscribe();
expect(factory.mock.body).toBe('{"some":"body"}');
});
it('sets outgoing headers, including default headers', () => {
const post = TEST_POST.clone({
setHeaders: {