Skip to content
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
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