Skip to content

Commit 2bddd38

Browse files
mhassan1chimurai
andauthored
fix(fixRequestBody): fix request body for empty JSON object requests (#640)
Co-authored-by: chimurai <655241+chimurai@users.noreply.github.com>
1 parent 6b5d7a8 commit 2bddd38

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/handlers/fix-request-body.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as querystring from 'querystring';
88
export function fixRequestBody(proxyReq: http.ClientRequest, req: http.IncomingMessage): void {
99
const requestBody = (req as Request).body;
1010

11-
if (!requestBody || !Object.keys(requestBody).length) {
11+
if (!requestBody) {
1212
return;
1313
}
1414

test/unit/fix-request-body.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ describe('fixRequestBody', () => {
2424
expect(proxyRequest.write).not.toHaveBeenCalled();
2525
});
2626

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

3031
jest.spyOn(proxyRequest, 'setHeader');
3132
jest.spyOn(proxyRequest, 'write');
3233

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

35-
expect(proxyRequest.setHeader).not.toHaveBeenCalled();
36-
expect(proxyRequest.write).not.toHaveBeenCalled();
36+
expect(proxyRequest.setHeader).toHaveBeenCalled();
37+
expect(proxyRequest.write).toHaveBeenCalled();
3738
});
3839

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

0 commit comments

Comments
 (0)