Skip to content

Commit 3048532

Browse files
committed
test: cover UTF-16 HTTP body branches
1 parent 47502c6 commit 3048532

1 file changed

Lines changed: 20 additions & 30 deletions

File tree

src/infra/http-error-body.test.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -97,39 +97,29 @@ describe("readResponseBodySnippet", () => {
9797
expect(result).toBe("");
9898
});
9999

100-
it("does not split surrogate pairs when truncating by maxChars (body-less path)", async () => {
101-
// "a" (1 code unit) + 5×🦞 (10 code units) = 11 code units > maxChars=10
102-
// .slice(0, 10) would cut between the surrogates of the 5th emoji
103-
const text = "a" + "🦞".repeat(5);
104-
const result = await readResponseBodySnippet(bodyLessResponse(text), {
100+
it.each([
101+
{
102+
name: "body-less response under the byte limit",
103+
response: () => bodyLessResponse("a" + "🦞".repeat(10)),
105104
maxBytes: 1024,
106-
maxChars: 10,
107-
});
108-
for (let i = 0; i < result.length; i++) {
109-
const cp = result.charCodeAt(i);
110-
if (cp >= 0xd800 && cp <= 0xdbff) {
111-
expect(result.charCodeAt(i + 1)).toBeGreaterThanOrEqual(0xdc00);
112-
expect(result.charCodeAt(i + 1)).toBeLessThanOrEqual(0xdfff);
113-
}
114-
}
115-
const lastCode = result.charCodeAt(result.length - 1);
116-
expect(lastCode < 0xd800 || lastCode > 0xdbff).toBe(true);
117-
});
118-
119-
it("does not split surrogate pairs in the stream path", async () => {
120-
const text = "a" + "🦞".repeat(5);
121-
const data = new TextEncoder().encode(text);
122-
const response = new Response(new Blob([data]).stream());
123-
const result = await readResponseBodySnippet(response, {
105+
},
106+
{
107+
name: "body-less response truncated by the byte limit",
108+
response: () => bodyLessResponse("a" + "🦞".repeat(10)),
109+
maxBytes: 30,
110+
},
111+
{
112+
name: "streamed response",
113+
response: () =>
114+
new Response(new Blob([new TextEncoder().encode("a" + "🦞".repeat(10))]).stream()),
124115
maxBytes: 1024,
116+
},
117+
])("preserves surrogate pairs for $name", async ({ response, maxBytes }) => {
118+
const result = await readResponseBodySnippet(response(), {
119+
maxBytes,
125120
maxChars: 10,
126121
});
127-
for (let i = 0; i < result.length; i++) {
128-
const cp = result.charCodeAt(i);
129-
if (cp >= 0xd800 && cp <= 0xdbff) {
130-
expect(result.charCodeAt(i + 1)).toBeGreaterThanOrEqual(0xdc00);
131-
expect(result.charCodeAt(i + 1)).toBeLessThanOrEqual(0xdfff);
132-
}
133-
}
122+
123+
expect(result).toBe("a" + "🦞".repeat(4));
134124
});
135125
});

0 commit comments

Comments
 (0)