Skip to content

Commit 76a9d8d

Browse files
authored
fix(fixRequestBody): prevent multiple .write() calls (#1090)
* fix(fixRequestBody): prevent multiple .write() calls * ci(github-actions): bump actions and node versions
1 parent 1e92339 commit 76a9d8d

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

.github/workflows/ci.yml

+15-15
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ jobs:
88
runs-on: ubuntu-latest
99

1010
steps:
11-
- uses: actions/checkout@v2
12-
- name: Use Node.js 16.x
13-
uses: actions/setup-node@v2
11+
- uses: actions/checkout@v4
12+
- name: Use Node.js 22.x
13+
uses: actions/setup-node@v4
1414
with:
15-
node-version: 16.x
15+
node-version: 22.x
1616

17-
- uses: actions/cache@v2
17+
- uses: actions/cache@v4
1818
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
1919
with:
2020
path: '**/node_modules'
@@ -38,16 +38,16 @@ jobs:
3838

3939
strategy:
4040
matrix:
41-
node-version: [12.x, 14.x, 16.x]
41+
node-version: [18.x, 20.x, 22.x]
4242

4343
steps:
44-
- uses: actions/checkout@v2
44+
- uses: actions/checkout@v4
4545
- name: Use Node.js ${{ matrix.node-version }}
46-
uses: actions/setup-node@v2
46+
uses: actions/setup-node@v4
4747
with:
4848
node-version: ${{ matrix.node-version }}
4949

50-
- uses: actions/cache@v2
50+
- uses: actions/cache@v4
5151
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
5252
with:
5353
path: '**/node_modules'
@@ -70,13 +70,13 @@ jobs:
7070
runs-on: ubuntu-latest
7171

7272
steps:
73-
- uses: actions/checkout@v2
74-
- name: Use Node.js 16.x
75-
uses: actions/setup-node@v2
73+
- uses: actions/checkout@v4
74+
- name: Use Node.js 22.x
75+
uses: actions/setup-node@v4
7676
with:
77-
node-version: 16.x
77+
node-version: 22.x
7878

79-
- uses: actions/cache@v2
79+
- uses: actions/cache@v4
8080
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
8181
with:
8282
path: '**/node_modules'
@@ -100,7 +100,7 @@ jobs:
100100
name: Spellcheck
101101
runs-on: ubuntu-latest
102102
steps:
103-
- uses: actions/checkout@v2
103+
- uses: actions/checkout@v4
104104
- uses: streetsidesoftware/cspell-action@main
105105
with:
106106
# Github token used to fetch the list of changed files in the commit.

src/handlers/fix-request-body.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ export function fixRequestBody(proxyReq: http.ClientRequest, req: http.IncomingM
1313
}
1414

1515
const contentType = proxyReq.getHeader('Content-Type') as string;
16+
17+
if (!contentType) {
18+
return;
19+
}
20+
1621
const writeBody = (bodyData: string) => {
1722
// deepcode ignore ContentLengthInCode: bodyParser fix
1823
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
1924
proxyReq.write(bodyData);
2025
};
2126

22-
if (contentType && contentType.includes('application/json')) {
27+
if (contentType.includes('application/json')) {
2328
writeBody(JSON.stringify(requestBody));
24-
}
25-
26-
if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
29+
} else if (contentType.includes('application/x-www-form-urlencoded')) {
2730
writeBody(querystring.stringify(requestBody));
2831
}
2932
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,19 @@ describe('fixRequestBody', () => {
7878
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
7979
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
8080
});
81+
82+
it('should parse json and call write() once with incorrect content-type application/x-www-form-urlencoded+application/json', () => {
83+
const proxyRequest = fakeProxyRequest();
84+
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded+application/json');
85+
86+
jest.spyOn(proxyRequest, 'setHeader');
87+
jest.spyOn(proxyRequest, 'write');
88+
89+
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as Request);
90+
91+
const expectedBody = JSON.stringify({ someField: 'some value' });
92+
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
93+
expect(proxyRequest.write).toHaveBeenCalledTimes(1);
94+
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
95+
});
8196
});

0 commit comments

Comments
 (0)