Files
bun.sh/test/js/web/fetch/fetch-leak.test.js
2024-06-11 10:36:32 -03:00

99 lines
2.4 KiB
JavaScript

import { test, expect, describe } from "bun:test";
import { join } from "node:path";
import { bunEnv, bunExe } from "harness";
import { tls as COMMON_CERT } from "harness";
describe("fetch doesn't leak", () => {
test("fixture #1", async () => {
const body = new Blob(["some body in here!".repeat(100)]);
var count = 0;
using server = Bun.serve({
port: 0,
fetch(req) {
count++;
return new Response(body);
},
});
const proc = Bun.spawn({
env: {
...bunEnv,
SERVER: `http://${server.hostname}:${server.port}`,
COUNT: "200",
},
stderr: "inherit",
stdout: "inherit",
cmd: [bunExe(), "--smol", join(import.meta.dir, "fetch-leak-test-fixture.js")],
});
const exitCode = await proc.exited;
expect(exitCode).toBe(0);
expect(count).toBe(200);
});
// This tests for body leakage and Response object leakage.
async function runTest(compressed, name) {
const body = !compressed
? new Blob(["some body in here!".repeat(2000000)])
: new Blob([Bun.deflateSync(crypto.getRandomValues(new Buffer(65123)))]);
const tls = name.includes("tls");
const headers = {
"Content-Type": "application/octet-stream",
};
if (compressed) {
headers["Content-Encoding"] = "deflate";
}
const serveOptions = {
port: 0,
fetch(req) {
return new Response(body, { headers });
},
};
if (tls) {
serveOptions.tls = { ...COMMON_CERT };
}
using server = Bun.serve(serveOptions);
const env = {
...bunEnv,
SERVER: `${tls ? "https" : "http"}://${server.hostname}:${server.port}`,
BUN_JSC_forceRAMSize: (1024 * 1024 * 64).toString("10"),
NAME: name,
};
if (tls) {
env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
}
if (compressed) {
env.COUNT = "5000";
}
const proc = Bun.spawn({
env,
stderr: "inherit",
stdout: "inherit",
cmd: [bunExe(), "--smol", join(import.meta.dir, "fetch-leak-test-fixture-2.js")],
});
const exitCode = await proc.exited;
expect(exitCode).toBe(0);
}
for (let compressed of [true, false]) {
describe(compressed ? "compressed" : "uncompressed", () => {
for (let name of ["tcp", "tls", "tls-with-client"]) {
describe(name, () => {
test("fixture #2", async () => {
await runTest(compressed, name);
}, 100000);
});
}
});
}
});