Files
bun.sh/test/js/web/fetch/fetch-leak.test.js
Jarred Sumner 12bb5d03a2 Always propagate error in EventEmitter (#11210)
Co-authored-by: Ciro Spaciari <ciro.spaciari@gmail.com>
2024-05-22 16:39:42 -07:00

96 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, tls) {
const body = !compressed
? new Blob(["some body in here!".repeat(2000000)])
: new Blob([Bun.deflateSync(crypto.getRandomValues(new Buffer(65123)))]);
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"),
};
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 tls of [true, false]) {
describe(tls ? "tls" : "tcp", () => {
test("fixture #2", async () => {
await runTest(compressed, tls);
}, 100000);
});
}
});
}
});