aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/body-stream.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test/bun.js/body-stream.test.ts')
-rw-r--r--test/bun.js/body-stream.test.ts167
1 files changed, 40 insertions, 127 deletions
diff --git a/test/bun.js/body-stream.test.ts b/test/bun.js/body-stream.test.ts
index 5f505143c..9a68ad3bc 100644
--- a/test/bun.js/body-stream.test.ts
+++ b/test/bun.js/body-stream.test.ts
@@ -21,49 +21,26 @@ var port = 4021;
}`, () => {
const inputFixture = [
[JSON.stringify("Hello World"), JSON.stringify("Hello World")],
+ [JSON.stringify("Hello World 123"), Buffer.from(JSON.stringify("Hello World 123")).buffer],
+ [JSON.stringify("Hello World 456"), Buffer.from(JSON.stringify("Hello World 456"))],
[
- JSON.stringify("Hello World 123"),
- Buffer.from(JSON.stringify("Hello World 123")).buffer,
- ],
- [
- JSON.stringify("Hello World 456"),
- Buffer.from(JSON.stringify("Hello World 456")),
- ],
- [
- JSON.stringify(
- "EXTREMELY LONG VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(
- 100,
- ),
- ),
+ JSON.stringify("EXTREMELY LONG VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(100)),
Buffer.from(
- JSON.stringify(
- "EXTREMELY LONG VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(
- 100,
- ),
- ),
+ JSON.stringify("EXTREMELY LONG VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(100)),
),
],
[
- JSON.stringify(
- "EXTREMELY LONG 🔥 UTF16 🔥 VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(
- 100,
- ),
- ),
+ JSON.stringify("EXTREMELY LONG 🔥 UTF16 🔥 VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(100)),
Buffer.from(
JSON.stringify(
- "EXTREMELY LONG 🔥 UTF16 🔥 VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(
- 100,
- ),
+ "EXTREMELY LONG 🔥 UTF16 🔥 VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(100),
),
),
],
];
for (const [name, input] of inputFixture) {
- test(`${name.slice(
- 0,
- Math.min(name.length ?? name.byteLength, 64),
- )}`, async () => {
+ test(`${name.slice(0, Math.min(name.length ?? name.byteLength, 64))}`, async () => {
await runInServer(
{
async fetch(req) {
@@ -75,24 +52,18 @@ var port = 4021;
expect(result.length).toBe(name.length);
expect(result).toBe(name);
} else if (result && result instanceof Blob) {
- expect(result.size).toBe(
- new TextEncoder().encode(name).byteLength,
- );
+ expect(result.size).toBe(new TextEncoder().encode(name).byteLength);
expect(await result.text()).toBe(name);
} else {
- expect(result.byteLength).toBe(
- Buffer.from(input).byteLength,
- );
- expect(Bun.SHA1.hash(result, "base64")).toBe(
- Bun.SHA1.hash(input, "base64"),
- );
+ expect(result.byteLength).toBe(Buffer.from(input).byteLength);
+ expect(Bun.SHA1.hash(result, "base64")).toBe(Bun.SHA1.hash(input, "base64"));
}
return new Response(result, {
headers: req.headers,
});
},
},
- async (url) => {
+ async url => {
var response;
// once, then batch of 5
@@ -119,9 +90,7 @@ var port = 4021;
}
expect(response.status).toBe(200);
- expect(response.headers.get("content-length")).toBe(
- String(Buffer.from(input).byteLength),
- );
+ expect(response.headers.get("content-length")).toBe(String(Buffer.from(input).byteLength));
expect(response.headers.get("content-type")).toBe("text/plain");
expect(await response.text()).toBe(name);
@@ -155,12 +124,8 @@ var port = 4021;
for (let i = 0; i < 5; i++) {
const response = results[i];
expect(response.status).toBe(200);
- expect(response.headers.get("content-length")).toBe(
- String(Buffer.from(input).byteLength),
- );
- expect(response.headers.get("content-type")).toBe(
- "text/plain",
- );
+ expect(response.headers.get("content-length")).toBe(String(Buffer.from(input).byteLength));
+ expect(response.headers.get("content-type")).toBe("text/plain");
expect(response.headers.get("x-counter")).toBe(String(i));
expect(await response.text()).toBe(name);
}
@@ -174,10 +139,7 @@ var port = 4021;
}
var existingServer;
-async function runInServer(
- opts: ServeOptions,
- cb: (url: string) => void | Promise<void>,
-) {
+async function runInServer(opts: ServeOptions, cb: (url: string) => void | Promise<void>) {
var server;
const handler = {
...opts,
@@ -241,16 +203,7 @@ describe("reader", function () {
// - less than the InlineBlob limit
// - multiple chunks
// - backpressure
- for (let inputLength of [
- 0,
- 1,
- 2,
- 12,
- 95,
- 1024,
- 1024 * 1024,
- 1024 * 1024 * 2,
- ]) {
+ for (let inputLength of [0, 1, 2, 12, 95, 1024, 1024 * 1024, 1024 * 1024 * 2]) {
var bytes = new Uint8Array(inputLength);
{
const chunk = Math.min(bytes.length, 256);
@@ -283,10 +236,7 @@ describe("reader", function () {
new Int32Array(bytes).subarray(1),
new Int32Array(bytes).subarray(0, new Int32Array(bytes).byteLength - 1),
new Float32Array(bytes).subarray(1),
- new Float32Array(bytes).subarray(
- 0,
- new Float32Array(bytes).byteLength - 1,
- ),
+ new Float32Array(bytes).subarray(0, new Float32Array(bytes).byteLength - 1),
new Int16Array(bytes).subarray(0, 1),
new Int32Array(bytes).subarray(0, 1),
new Float32Array(bytes).subarray(0, 1),
@@ -302,13 +252,9 @@ describe("reader", function () {
const expectedHash =
huge instanceof Blob
- ? Bun.SHA1.hash(
- new Uint8Array(await huge.arrayBuffer()),
- "base64",
- )
+ ? Bun.SHA1.hash(new Uint8Array(await huge.arrayBuffer()), "base64")
: Bun.SHA1.hash(huge, "base64");
- const expectedSize =
- huge instanceof Blob ? huge.size : huge.byteLength;
+ const expectedSize = huge instanceof Blob ? huge.size : huge.byteLength;
const out = await runInServer(
{
@@ -316,16 +262,12 @@ describe("reader", function () {
try {
expect(req.headers.get("x-custom")).toBe("hello");
expect(req.headers.get("content-type")).toBe("text/plain");
- expect(req.headers.get("user-agent")).toBe(
- navigator.userAgent,
- );
+ expect(req.headers.get("user-agent")).toBe(navigator.userAgent);
gc();
expect(req.headers.get("x-custom")).toBe("hello");
expect(req.headers.get("content-type")).toBe("text/plain");
- expect(req.headers.get("user-agent")).toBe(
- navigator.userAgent,
- );
+ expect(req.headers.get("user-agent")).toBe(navigator.userAgent);
var reader = req.body.getReader();
called = true;
@@ -338,14 +280,10 @@ describe("reader", function () {
const out = new Blob(buffers);
gc();
expect(out.size).toBe(expectedSize);
- expect(Bun.SHA1.hash(await out.arrayBuffer(), "base64")).toBe(
- expectedHash,
- );
+ expect(Bun.SHA1.hash(await out.arrayBuffer(), "base64")).toBe(expectedHash);
expect(req.headers.get("x-custom")).toBe("hello");
expect(req.headers.get("content-type")).toBe("text/plain");
- expect(req.headers.get("user-agent")).toBe(
- navigator.userAgent,
- );
+ expect(req.headers.get("user-agent")).toBe(navigator.userAgent);
gc();
return new Response(out, {
headers: req.headers,
@@ -356,7 +294,7 @@ describe("reader", function () {
}
},
},
- async (url) => {
+ async url => {
gc();
const response = await fetch(url, {
body: huge,
@@ -369,9 +307,7 @@ describe("reader", function () {
});
huge = undefined;
expect(response.status).toBe(200);
- const response_body = new Uint8Array(
- await response.arrayBuffer(),
- );
+ const response_body = new Uint8Array(await response.arrayBuffer());
expect(response_body.byteLength).toBe(expectedSize);
expect(Bun.SHA1.hash(response_body, "base64")).toBe(expectedHash);
@@ -388,7 +324,7 @@ describe("reader", function () {
for (let isDirectStream of [true, false]) {
const positions = ["begin", "end"];
- const inner = (thisArray) => {
+ const inner = thisArray => {
for (let position of positions) {
it(`streaming back ${thisArray.constructor.name}(${
thisArray.byteLength ?? thisArray.size
@@ -399,13 +335,9 @@ describe("reader", function () {
const expectedHash =
huge instanceof Blob
- ? Bun.SHA1.hash(
- new Uint8Array(await huge.arrayBuffer()),
- "base64",
- )
+ ? Bun.SHA1.hash(new Uint8Array(await huge.arrayBuffer()), "base64")
: Bun.SHA1.hash(huge, "base64");
- const expectedSize =
- huge instanceof Blob ? huge.size : huge.byteLength;
+ const expectedSize = huge instanceof Blob ? huge.size : huge.byteLength;
const out = await runInServer(
{
@@ -423,21 +355,13 @@ describe("reader", function () {
}
expect(req.headers.get("x-custom")).toBe("hello");
- expect(req.headers.get("content-type")).toBe(
- "text/plain",
- );
- expect(req.headers.get("user-agent")).toBe(
- navigator.userAgent,
- );
+ expect(req.headers.get("content-type")).toBe("text/plain");
+ expect(req.headers.get("user-agent")).toBe(navigator.userAgent);
gc();
expect(req.headers.get("x-custom")).toBe("hello");
- expect(req.headers.get("content-type")).toBe(
- "text/plain",
- );
- expect(req.headers.get("user-agent")).toBe(
- navigator.userAgent,
- );
+ expect(req.headers.get("content-type")).toBe("text/plain");
+ expect(req.headers.get("user-agent")).toBe(navigator.userAgent);
const direct = {
type: "direct",
@@ -469,19 +393,16 @@ describe("reader", function () {
},
};
- return new Response(
- new ReadableStream(isDirectStream ? direct : web),
- {
- headers: req.headers,
- },
- );
+ return new Response(new ReadableStream(isDirectStream ? direct : web), {
+ headers: req.headers,
+ });
} catch (e) {
console.error(e);
throw e;
}
},
},
- async (url) => {
+ async url => {
gc();
const response = await fetch(url, {
body: huge,
@@ -494,25 +415,17 @@ describe("reader", function () {
});
huge = undefined;
expect(response.status).toBe(200);
- const response_body = new Uint8Array(
- await response.arrayBuffer(),
- );
+ const response_body = new Uint8Array(await response.arrayBuffer());
expect(response_body.byteLength).toBe(expectedSize);
- expect(Bun.SHA1.hash(response_body, "base64")).toBe(
- expectedHash,
- );
+ expect(Bun.SHA1.hash(response_body, "base64")).toBe(expectedHash);
gc();
if (!response.headers.has("content-type")) {
- console.error(
- Object.fromEntries(response.headers.entries()),
- );
+ console.error(Object.fromEntries(response.headers.entries()));
}
- expect(response.headers.get("content-type")).toBe(
- "text/plain",
- );
+ expect(response.headers.get("content-type")).toBe("text/plain");
gc();
},
);