Skip to content

Commit

Permalink
fix(fixRequestBody): fix request body for empty JSON object requests (#…
Browse files Browse the repository at this point in the history
…640)

Co-authored-by: chimurai <[email protected]>
  • Loading branch information
mhassan1 and chimurai authored Jan 23, 2022
1 parent 6b5d7a8 commit 2bddd38
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/handlers/fix-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as querystring from 'querystring';
export function fixRequestBody(proxyReq: http.ClientRequest, req: http.IncomingMessage): void {
const requestBody = (req as Request).body;

if (!requestBody || !Object.keys(requestBody).length) {
if (!requestBody) {
return;
}

Expand Down
7 changes: 4 additions & 3 deletions test/unit/fix-request-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ describe('fixRequestBody', () => {
expect(proxyRequest.write).not.toHaveBeenCalled();
});

it('should not write when body is empty', () => {
it('should write when body is an empty JSON object', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'application/json; charset=utf-8');

jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');

fixRequestBody(proxyRequest, { body: {} } as Request);

expect(proxyRequest.setHeader).not.toHaveBeenCalled();
expect(proxyRequest.write).not.toHaveBeenCalled();
expect(proxyRequest.setHeader).toHaveBeenCalled();
expect(proxyRequest.write).toHaveBeenCalled();
});

it('should write when body is not empty and Content-Type is application/json', () => {
Expand Down

0 comments on commit 2bddd38

Please sign in to comment.