Skip to content

Commit

Permalink
test: add more stream decoding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Aug 4, 2023
1 parent 58626c7 commit 9efa1d0
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,42 @@ describe("engine.io-parser", () => {
expect(areArraysEqual(value.data, Uint8Array.of(1, 2, 3)));
});

it("should decode a binary packet (ArrayBuffer) (medium)", async () => {
const stream = createPacketDecoderStream(1e6, "arraybuffer");
const payload = new Uint8Array(12345);

const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();

writer.write(Uint8Array.of(254));
writer.write(Uint8Array.of(48, 57));
writer.write(payload);

const { value } = await reader.read();

expect(value.type).to.eql("message");
expect(value.data).to.be.an(ArrayBuffer);
expect(areArraysEqual(value.data, payload));
});

it("should decode a binary packet (ArrayBuffer) (big)", async () => {
const stream = createPacketDecoderStream(1e10, "arraybuffer");
const payload = new Uint8Array(123456789);

const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();

writer.write(Uint8Array.of(255));
writer.write(Uint8Array.of(0, 0, 0, 0, 7, 91, 205, 21));
writer.write(payload);

const { value } = await reader.read();

expect(value.type).to.eql("message");
expect(value.data).to.be.an(ArrayBuffer);
expect(areArraysEqual(value.data, payload));
});

it("should return an error packet if the length of the payload is too big", async () => {
const stream = createPacketDecoderStream(10, "arraybuffer");

Expand All @@ -286,6 +322,18 @@ describe("engine.io-parser", () => {
const packet = await reader.read();
expect(packet.value).to.eql({ type: "error", data: "parser error" });
});

it("should return an error packet if the length of the payload is bigger than Number.MAX_SAFE_INTEGER", async () => {
const stream = createPacketDecoderStream(1e6, "arraybuffer");

const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();

writer.write(Uint8Array.of(255, 1, 0, 0, 0, 0, 0, 0, 0, 0));

const packet = await reader.read();
expect(packet.value).to.eql({ type: "error", data: "parser error" });
});
});
}
});

0 comments on commit 9efa1d0

Please sign in to comment.