Skip to content

Commit 3928202

Browse files
authored
test(teeny-request): add multi-byte UTF-8 Content-Length unit test (#9763)
## Description Adds a multi-byte UTF-8 string `Content-Length` unit test for `bunFetch`. ## Impact Verifies byte-accurate `Content-Length` headers for non-ASCII string request bodies. ## Changes - Follow up on review feedback in #9748 (`b/578941498`). - Add a unit test case in `core/packages/teeny-request/test/index.ts` sending a multi-byte UTF-8 string body (`'hello 🌍'`, 8 UTF-16 code units / 10 UTF-8 bytes) via `bunFetch` and asserting that `Content-Length` is `'10'`. ## Testing - Ran `core/packages/teeny-request` unit tests 5 consecutive times on Node.js (`pnpm --dir core/packages/teeny-request test`) and 5 consecutive times on Bun (`JS_RUNTIME=bun MOCHA_PARALLEL=false bun --bun run --cwd core/packages/teeny-request test`). - Verified `GIT_DIFF_ARG="upstream/main...HEAD -- :!packages" node ./bin/linter.mjs --strict` and `ci/run_conditional_tests.sh --strict` pass. ## Alternatives - Relying solely on the existing ASCII `'hello'` test was considered and rejected because ASCII strings have identical character length and UTF-8 byte length (`5`), which would not catch regressions that use `body.length` instead of `Buffer.byteLength(body)`. - Not merging this PR was considered, which would leave multi-byte UTF-8 string `Content-Length` calculation in `bunFetch` without regression coverage.
1 parent 2762a27 commit 3928202

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

  • core/packages/teeny-request/test

‎core/packages/teeny-request/test/index.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,31 @@ describe('teeny', () => {
707707
nock.disableNetConnect();
708708
}
709709

710+
const multibyteContentLengthScope = nock(uri)
711+
.post('/content-length-utf8', 'hello 🌍')
712+
.matchHeader('content-length', '10')
713+
.reply(200, 'ok');
714+
await new Promise<void>((resolve, reject) => {
715+
teenyRequest(
716+
{
717+
uri: `${uri}/content-length-utf8`,
718+
method: 'POST',
719+
body: 'hello 🌍',
720+
},
721+
(err, res, body) => {
722+
if (err) return reject(err);
723+
try {
724+
assert.strictEqual(res.statusCode, 200);
725+
assert.strictEqual(body, 'ok');
726+
multibyteContentLengthScope.done();
727+
resolve();
728+
} catch (e) {
729+
reject(e);
730+
}
731+
},
732+
);
733+
});
734+
710735
const contentLengthScope = nock(uri)
711736
.post('/content-length', 'hello')
712737
.matchHeader('content-length', '5')

0 commit comments

Comments
 (0)