Skip to content

Commit

Permalink
fix(fix-request-body): support '+json' content-type suffix (#1015)
Browse files Browse the repository at this point in the history
* fix(fix-request-body): add fix for '+json' ending
* test: add unit test
* fix: fix ut error
* docs: Update CHANGELOG.md

---------

Co-authored-by: Steven Chim <[email protected]>
  • Loading branch information
howiezhao and chimurai authored Jul 7, 2024
1 parent c8d34f1 commit 09d05d5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- ci(package): npm package provenance
- fix(logger-plugin): log target port when router option is used
- refactor: fix circular dependencies
- fix(fix-request-body): support '+json' content-type suffix

## [v3.0.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v3.0.0)

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/fix-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function fixRequestBody<TReq = http.IncomingMessage>(
proxyReq.write(bodyData);
};

if (contentType && contentType.includes('application/json')) {
if (contentType && (contentType.includes('application/json') || contentType.includes('+json'))) {
writeBody(JSON.stringify(requestBody));
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/fix-request-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ describe('fixRequestBody', () => {
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
});

it('should write when body is not empty and Content-Type ends with +json', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'application/merge-patch+json; charset=utf-8');

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

fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));

const expectedBody = JSON.stringify({ someField: 'some value' });
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
});

it('should write when body is not empty and Content-Type is application/x-www-form-urlencoded', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded');
Expand Down

0 comments on commit 09d05d5

Please sign in to comment.