Skip to content

Commit 68bc6d7

Browse files
authored
fix(fix-request-body): improve content type check (#725)
* Update fix-request-body content type check * add unit test
1 parent e9e25ca commit 68bc6d7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/handlers/fix-request-body.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function fixRequestBody(proxyReq: http.ClientRequest, req: http.IncomingM
2323
writeBody(JSON.stringify(requestBody));
2424
}
2525

26-
if (contentType === 'application/x-www-form-urlencoded') {
26+
if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
2727
writeBody(querystring.stringify(requestBody));
2828
}
2929
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,18 @@ describe('fixRequestBody', () => {
6464
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
6565
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
6666
});
67+
68+
it('should write when body is not empty and Content-Type includes application/x-www-form-urlencoded', () => {
69+
const proxyRequest = fakeProxyRequest();
70+
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded; charset=UTF-8');
71+
72+
jest.spyOn(proxyRequest, 'setHeader');
73+
jest.spyOn(proxyRequest, 'write');
74+
75+
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as Request);
76+
77+
const expectedBody = querystring.stringify({ someField: 'some value' });
78+
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
79+
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
80+
});
6781
});

0 commit comments

Comments
 (0)