Skip to content

Commit 65afa3d

Browse files
committed
use req.setEncoding('utf8') instead of per-chunk Buffer.toString()
Per-chunk .toString() corrupts a multi-byte UTF-8 char that straddles a TCP chunk boundary (e.g. 0xC3 | 0xBC decodes to two replacement chars instead of 'ü'). The custom-headers scenario sends '日本語'/'naïve' in the body, so this is reachable in principle. setEncoding('utf8') makes 'data' emit strings with boundary handling done by Node's StringDecoder. Fixed in both places: BaseHttpScenario.handleRequest (request body) and sendRawRequest (response body).
1 parent 1d2d267 commit 65afa3d

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

src/scenarios/client/http-base.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@ export abstract class BaseHttpScenario implements Scenario {
8888
return;
8989
}
9090

91+
// Decode the stream as UTF-8 so multi-byte characters that straddle a
92+
// chunk boundary aren't corrupted by per-chunk Buffer.toString().
93+
req.setEncoding('utf8');
9194
let body = '';
9295
req.on('data', (chunk) => {
93-
body += chunk.toString();
96+
body += chunk;
9497
});
9598
req.on('end', () => {
9699
try {

src/scenarios/server/http-standard-headers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ async function sendRawRequest(
8282
}
8383
},
8484
(res) => {
85+
res.setEncoding('utf8');
8586
let data = '';
8687
res.on('data', (chunk) => {
87-
data += chunk.toString();
88+
data += chunk;
8889
});
8990
res.on('end', () => {
9091
let responseBody: any;

0 commit comments

Comments
 (0)