Files
bun.sh/test/js/bun/test/parallel/test-http-4415-IncomingMessage-es5.ts
2025-09-30 17:25:17 -07:00

33 lines
1.0 KiB
TypeScript

import { createTest } from "node-harness";
import { once } from "node:events";
import { IncomingMessage, Server } from "node:http";
const { expect } = createTest(import.meta.path);
// This matches Node.js:
const im = Object.create(IncomingMessage.prototype);
IncomingMessage.call(im, { url: "/foo" });
expect(im.url).toBe("");
let didCall = false;
function Subclass(...args) {
IncomingMessage.apply(this, args);
didCall = true;
}
Object.setPrototypeOf(Subclass.prototype, IncomingMessage.prototype);
Object.setPrototypeOf(Subclass, IncomingMessage);
await using server = new Server({ IncomingMessage: Subclass }, (req, res) => {
if (req instanceof Subclass && didCall) {
expect(req.url).toBe("/foo");
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("hello");
} else {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("bye");
}
});
server.listen(0);
await once(server, "listening");
const response = await fetch(`http://localhost:${server.address().port}/foo`, { method: "GET" });
expect(response.status).toBe(200);