From c3d89139b6a3a6659d526aeabebe1a2a8056f90d Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Fri, 19 Aug 2022 14:18:49 +0000 Subject: [PATCH 1/2] fix(util-body-length-browser): handle trail surrogate character --- .../src/calculateBodyLength.spec.ts | 12 ++++++------ .../src/calculateBodyLength.ts | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/util-body-length-browser/src/calculateBodyLength.spec.ts b/packages/util-body-length-browser/src/calculateBodyLength.spec.ts index 301ed51a735c..b915a5dbf901 100644 --- a/packages/util-body-length-browser/src/calculateBodyLength.spec.ts +++ b/packages/util-body-length-browser/src/calculateBodyLength.spec.ts @@ -4,12 +4,12 @@ const arrayBuffer = new ArrayBuffer(1); const typedArray = new Uint8Array(1); describe(calculateBodyLength.name, () => { - it("should handle string inputs", () => { - expect(calculateBodyLength("foo")).toEqual(3); - }); - - it("should handle string inputs with multi-byte characters", () => { - expect(calculateBodyLength("2。")).toEqual(4); + it.each([ + { desc: "basic", input: "foo", output: 3 }, + { desc: "emoji", input: "foo 🥺", output: 8 }, + { desc: "multi-byte characters", input: "2。", output: 4 }, + ])("should handle string input: %s", ({ input, output }) => { + expect(calculateBodyLength(input)).toEqual(output); }); it("should handle inputs with byteLengths", () => { diff --git a/packages/util-body-length-browser/src/calculateBodyLength.ts b/packages/util-body-length-browser/src/calculateBodyLength.ts index 55ca7f7fa921..3f02b9911164 100644 --- a/packages/util-body-length-browser/src/calculateBodyLength.ts +++ b/packages/util-body-length-browser/src/calculateBodyLength.ts @@ -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; From 7e76b7f8480ce83d64e6191e14d86330826b6af2 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Fri, 19 Aug 2022 15:03:36 +0000 Subject: [PATCH 2/2] test: organize tests for other components --- .../src/calculateBodyLength.spec.ts | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/packages/util-body-length-browser/src/calculateBodyLength.spec.ts b/packages/util-body-length-browser/src/calculateBodyLength.spec.ts index b915a5dbf901..7363894eacec 100644 --- a/packages/util-body-length-browser/src/calculateBodyLength.spec.ts +++ b/packages/util-body-length-browser/src/calculateBodyLength.spec.ts @@ -1,23 +1,30 @@ import { calculateBodyLength } from "./calculateBodyLength"; -const arrayBuffer = new ArrayBuffer(1); -const typedArray = new Uint8Array(1); - describe(calculateBodyLength.name, () => { - it.each([ - { desc: "basic", input: "foo", output: 3 }, - { desc: "emoji", input: "foo 🥺", output: 8 }, - { desc: "multi-byte characters", input: "2。", output: 4 }, - ])("should handle string input: %s", ({ input, output }) => { - expect(calculateBodyLength(input)).toEqual(output); + 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 inputs with byteLengths", () => { - expect(calculateBodyLength(arrayBuffer)).toEqual(1); - }); + describe("should handle input with byteLength", () => { + const sizes = [1, 256, 65536]; + + 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", () => { @@ -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}`); + }); }); });