Skip to content

Commit 09d05d5

Browse files
howiezhaochimurai
andauthored
fix(fix-request-body): support '+json' content-type suffix (#1015)
* 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 <655241+chimurai@users.noreply.github.com>
1 parent c8d34f1 commit 09d05d5

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- ci(package): npm package provenance
99
- fix(logger-plugin): log target port when router option is used
1010
- refactor: fix circular dependencies
11+
- fix(fix-request-body): support '+json' content-type suffix
1112

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

src/handlers/fix-request-body.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function fixRequestBody<TReq = http.IncomingMessage>(
2323
proxyReq.write(bodyData);
2424
};
2525

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ describe('fixRequestBody', () => {
5757
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
5858
});
5959

60+
it('should write when body is not empty and Content-Type ends with +json', () => {
61+
const proxyRequest = fakeProxyRequest();
62+
proxyRequest.setHeader('content-type', 'application/merge-patch+json; charset=utf-8');
63+
64+
jest.spyOn(proxyRequest, 'setHeader');
65+
jest.spyOn(proxyRequest, 'write');
66+
67+
fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));
68+
69+
const expectedBody = JSON.stringify({ someField: 'some value' });
70+
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
71+
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
72+
});
73+
6074
it('should write when body is not empty and Content-Type is application/x-www-form-urlencoded', () => {
6175
const proxyRequest = fakeProxyRequest();
6276
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded');

0 commit comments

Comments
 (0)