Files
bun.sh/test/js/node/http/client-timeout-error.test.ts
2025-11-25 00:00:54 -08:00

62 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { once } from "node:events";
import { createServer, request } from "node:http";
describe("node:http client timeout", () => {
it("should emit timeout event when timeout is reached", async () => {
const server = createServer((req, res) => {
// Intentionally not sending response to trigger timeout
}).listen(0);
try {
await once(server, "listening");
const port = (server.address() as any).port;
const req = request({
port,
host: "localhost",
path: "/",
timeout: 50, // Set a short timeout
});
req.end();
await once(req, "timeout");
} finally {
server.close();
}
});
it("should clear timeout when explicitly set to 0", async () => {
const server = createServer((req, res) => {
res.end("OK");
}).listen(0);
try {
await once(server, "listening");
const port = (server.address() as any).port;
const req = request({
port,
host: "localhost",
path: "/",
});
let timeoutEventEmitted = false;
req.on("timeout", () => {
timeoutEventEmitted = true;
});
// Set and then clear timeout
req.setTimeout(50);
req.setTimeout(0);
req.end();
// Wait longer than the original timeout
await new Promise(resolve => setTimeout(resolve, 100));
expect(timeoutEventEmitted).toBe(false);
} finally {
server.close();
}
});
});