Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/fix request body #896

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
19 changes: 19 additions & 0 deletions src/handlers/fix-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,23 @@ export function fixRequestBody<TReq = http.IncomingMessage>(
if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
writeBody(querystring.stringify(requestBody));
}

if (contentType && contentType.includes('multipart/form-data')) {
writeBody(handlerFormDataBodyData(contentType, requestBody));
}
}

/**
* format FormData data
* @param contentType
* @param data
* @returns
*/
function handlerFormDataBodyData(contentType: string, data: any) {
const boundary = contentType.replace(/^.*boundary=(.*)$/, '$1');
let str = '';
for (const [key, value] of Object.entries(data)) {
str += `--${boundary}\r\nContent-Disposition: form-data; name="${key}"\r\n\r\n${value}\r\n`;
}
return str;
}
59 changes: 58 additions & 1 deletion test/unit/fix-request-body.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Socket } from 'net';
import { ClientRequest, IncomingMessage } from 'http';
import * as querystring from 'querystring';

import { fixRequestBody, BodyParserLikeRequest } from '../../src/handlers/fix-request-body';

const fakeProxyRequest = (): ClientRequest => {
Expand All @@ -17,6 +16,15 @@ const createRequestWithBody = (body: unknown): BodyParserLikeRequest => {
return req;
};

const handlerFormDataBodyData = (contentType: string, data: { [key: string]: any }) => {
const boundary = contentType.replace(/^.*boundary=(.*)$/, '$1');
let str = '';
for (const [key, value] of Object.entries(data)) {
str += `--${boundary}\r\nContent-Disposition: form-data; name="${key}"\r\n\r\n${value}\r\n`;
}
return str;
};

describe('fixRequestBody', () => {
it('should not write when body is undefined', () => {
const proxyRequest = fakeProxyRequest();
Expand Down Expand Up @@ -57,6 +65,55 @@ describe('fixRequestBody', () => {
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
});

it('should write when body is not empty and Content-Type is multipart/form-data', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'multipart/form-data');

jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');

fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));

const expectedBody = handlerFormDataBodyData('multipart/form-data', {
someField: 'some value',
});

expect(expectedBody).toMatchInlineSnapshot(`
"--multipart/form-data
Content-Disposition: form-data; name="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 includes multipart/form-data', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'multipart/form-data');

jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');

fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));

const expectedBody = handlerFormDataBodyData('multipart/form-data', {
someField: 'some value',
});

expect(expectedBody).toMatchInlineSnapshot(`
"--multipart/form-data
Content-Disposition: form-data; name="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