Skip to content

Commit

Permalink
fix(http): use serializeBody to support JSON payload in FetchBackend (#…
Browse files Browse the repository at this point in the history
…50776)

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

Close #50775

PR Close #50776
  • Loading branch information
leo6104 authored and AndrewKushnir committed Jun 20, 2023
1 parent 6f443a9 commit 5ae0018
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/common/http/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class FetchBackend implements HttpBackend {
}

return {
body: req.body,
body: req.serializeBody(),
method: req.method,
headers,
credentials,
Expand Down
9 changes: 9 additions & 0 deletions packages/common/http/test/fetch_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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: {
Expand Down
8 changes: 8 additions & 0 deletions packages/common/http/test/xhr_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

{
Expand All @@ -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: {
Expand Down

0 comments on commit 5ae0018

Please sign in to comment.