Skip to content

Commit

Permalink
fix(fix-request-body): improve content type check (#725)
Browse files Browse the repository at this point in the history
* Update fix-request-body content type check
* add unit test
  • Loading branch information
kevinxh authored Mar 13, 2022
1 parent e9e25ca commit 68bc6d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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(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));
}
}
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 @@ -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);
});
});

0 comments on commit 68bc6d7

Please sign in to comment.