Skip to content

fix(util-body-length-browser): handle trail surrogate character #3866

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

Merged
merged 2 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 26 additions & 17 deletions packages/util-body-length-browser/src/calculateBodyLength.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { calculateBodyLength } from "./calculateBodyLength";

const arrayBuffer = new ArrayBuffer(1);
const typedArray = new Uint8Array(1);

describe(calculateBodyLength.name, () => {
it("should handle string inputs", () => {
expect(calculateBodyLength("foo")).toEqual(3);
describe("should handle string input", () => {
it.each([
{ desc: "basic", input: "foo", output: 3 },
{ desc: "emoji", input: "foo 🥺", output: 8 },
{ desc: "multi-byte characters", input: "2。", output: 4 },
])("%s", ({ input, output }) => {
expect(calculateBodyLength(input)).toEqual(output);
});
});

it("should handle string inputs with multi-byte characters", () => {
expect(calculateBodyLength("2。")).toEqual(4);
});
describe("should handle input with byteLength", () => {
const sizes = [1, 256, 65536];

it("should handle inputs with byteLengths", () => {
expect(calculateBodyLength(arrayBuffer)).toEqual(1);
});
describe("ArrayBuffer", () => {
it.each(sizes)("size: %s", (size) => {
expect(calculateBodyLength(new ArrayBuffer(size))).toEqual(size);
});
});

it("should handle TypedArray inputs", () => {
expect(calculateBodyLength(typedArray)).toEqual(1);
describe("TypedArray", () => {
it.each(sizes)("size: %s", (size) => {
expect(calculateBodyLength(new Uint8Array(size))).toEqual(size);
});
});
});

it("should handle File object", () => {
Expand All @@ -34,9 +41,11 @@ describe(calculateBodyLength.name, () => {
expect(calculateBodyLength(mockFileObject)).toEqual(mockFileObject.size);
});

it.each([true, 1, {}, []])("throws error if Body Length computation fails for: %s", (body) => {
expect(() => {
expect(calculateBodyLength(body));
}).toThrowError(`Body Length computation failed for ${body}`);
describe("throws error if Body Length computation fails", () => {
it.each([true, 1, {}, []])("%s", (body) => {
expect(() => {
expect(calculateBodyLength(body));
}).toThrowError(`Body Length computation failed for ${body}`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const calculateBodyLength = (body: any): number | undefined => {
const code = body.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) len++;
else if (code > 0x7ff && code <= 0xffff) len += 2;
if (code >= 0xdc00 && code <= 0xdfff) i--; //trail surrogate
}

return len;
Expand Down