Repository: chimurai/http-proxy-middleware
Pull request #640 (2021-07-14 03:23:20)

fix(fixRequestBody): fix request body for empty JSON object requests
## Description

This PR fixes the `fixRequestBody` utility so that it treats empty object bodies like non-empty object bodies and fixes them, as expected.

## Motivation and Context

The `fixRequestBody` utility contains a check for empty objects that results in early `return`: https://github.com/chimurai/http-proxy-middleware/blob/c9bec81e82664809c816250f15d30543c4447571/src/handlers/fix-request-body.ts#L11-L13

Currently, JSON requests with empty object payloads do not get their request bodies fixed by this utility, and the request to the target hangs while the target waits for a payload that will never arrive.

This PR removes the special handling for empty objects and treats them like any other object.

Resolves https://github.com/chimurai/http-proxy-middleware/issues/639.

## How has this been tested?

I ran the reproduction steps in https://github.com/chimurai/http-proxy-middleware/issues/639 before and after the code change. I also updated automated tests.

## Types of changes

- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

## Checklist:

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.


HappyColour: This PR has been released right?
eemelipa: > This PR has been released right?

I don't think so. The PR is still open and the library doesn't have the fix
HappyColour: > > This PR has been released right?
> 
> I don't think so. The PR is still open and the library doesn't have the fix

Thx bro @eemelipa,please confirm this pr has been merged @mhassan1.
mhassan1: No, it is still open.
chimurai: Hi @mhassan1. I left a remark in the PR a while ago which hasn't been resolved.

Happy to merge it after it's fixed.
mhassan1: @chimurai This is ready for re-review.
coveralls: 
[![Coverage Status](https://coveralls.io/builds/44707012/badge)](https://coveralls.io/builds/44707012)

Coverage remained the same at 98.86% when pulling **df96284b753519319c00644aa43234ff1da707a6 on mhassan1:fix/fix-request-body-empty-json** into **a21c0dcbc9beeaf131dfe7efb32da156ec7e3a2b on chimurai:master**.


Patch:
diff --git a/src/handlers/fix-request-body.ts b/src/handlers/fix-request-body.ts
index 500e623..1855f56 100644
--- a/src/handlers/fix-request-body.ts
+++ b/src/handlers/fix-request-body.ts
@@ -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;
   }
 


Test patch:
diff --git a/test/unit/fix-request-body.spec.ts b/test/unit/fix-request-body.spec.ts
index 6ea56ba..9667750 100644
--- a/test/unit/fix-request-body.spec.ts
+++ b/test/unit/fix-request-body.spec.ts
@@ -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', () => {