mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
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:
parent
64745a89b2
commit
a126cbcf22
3 changed files with 18 additions and 1 deletions
|
|
@ -221,7 +221,7 @@ export class FetchBackend implements HttpBackend {
|
|||
}
|
||||
|
||||
return {
|
||||
body: req.body,
|
||||
body: req.serializeBody(),
|
||||
method: req.method,
|
||||
headers,
|
||||
credentials,
|
||||
|
|
|
|||
|
|
@ -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: {
|
||||
|
|
|
|||
|
|
@ -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: {
|
||||
|
|
|
|||
Loading…
Reference in a new issue