Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fixRequestBody): fix request body for empty JSON object requests #640

Merged
merged 2 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
mhassan1 marked this conversation as resolved.
Show resolved Hide resolved
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