diff --git a/src/handlers/fix-request-body.ts b/src/handlers/fix-request-body.ts index 1855f569..0f45a2f7 100644 --- a/src/handlers/fix-request-body.ts +++ b/src/handlers/fix-request-body.ts @@ -23,7 +23,7 @@ export function fixRequestBody(proxyReq: http.ClientRequest, req: http.IncomingM writeBody(JSON.stringify(requestBody)); } - if (contentType === 'application/x-www-form-urlencoded') { + if (contentType && contentType.includes('application/x-www-form-urlencoded')) { writeBody(querystring.stringify(requestBody)); } } diff --git a/test/unit/fix-request-body.spec.ts b/test/unit/fix-request-body.spec.ts index 96677502..5bc42f8c 100644 --- a/test/unit/fix-request-body.spec.ts +++ b/test/unit/fix-request-body.spec.ts @@ -64,4 +64,18 @@ describe('fixRequestBody', () => { expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length); expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody); }); + + it('should write when body is not empty and Content-Type includes application/x-www-form-urlencoded', () => { + const proxyRequest = fakeProxyRequest(); + proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded; charset=UTF-8'); + + jest.spyOn(proxyRequest, 'setHeader'); + jest.spyOn(proxyRequest, 'write'); + + fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as Request); + + const expectedBody = querystring.stringify({ someField: 'some value' }); + expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length); + expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody); + }); });