diff options
-rw-r--r-- | src/js/node/http.ts | 6 | ||||
-rw-r--r-- | test/js/node/http/node-http.test.ts | 33 |
2 files changed, 38 insertions, 1 deletions
diff --git a/src/js/node/http.ts b/src/js/node/http.ts index a745f9b32..c8fbf20d6 100644 --- a/src/js/node/http.ts +++ b/src/js/node/http.ts @@ -991,7 +991,10 @@ export class ServerResponse extends Writable { #finished = false; // Express "compress" package uses this - _implicitHeader() {} + _implicitHeader() { + const statusMessage = this.statusMessage ?? STATUS_CODES[this.statusCode]; + this.writeHead(this.statusCode, statusMessage, {}); + } _write(chunk, encoding, callback) { if (!this.#firstWrite && !this.headersSent) { @@ -1058,6 +1061,7 @@ export class ServerResponse extends Writable { var data = this.#firstWrite || ""; this.#firstWrite = undefined; this.#finished = true; + this._implicitHeader(); this._reply( new Response(data, { headers: this.#headers, diff --git a/test/js/node/http/node-http.test.ts b/test/js/node/http/node-http.test.ts index 3e7da9d34..167cb9883 100644 --- a/test/js/node/http/node-http.test.ts +++ b/test/js/node/http/node-http.test.ts @@ -146,6 +146,29 @@ describe("node:http", () => { res.end("Path correct!\n"); return; } + if (reqUrl.pathname === "/customWriteHead") { + function createWriteHead(prevWriteHead, listener) { + let fired = false; + return function writeHead() { + if (!fired) { + fired = true; + listener.call(this); + } + return prevWriteHead.apply(this, arguments); + }; + } + + function addPoweredBy() { + if (!this.getHeader("X-Powered-By")) { + this.setHeader("X-Powered-By", "Bun"); + } + } + + res.writeHead = createWriteHead(res.writeHead, addPoweredBy); + res.setHeader("Content-Type", "text/plain"); + res.end("Hello World"); + return; + } } res.writeHead(200, { "Content-Type": "text/plain" }); @@ -507,6 +530,16 @@ describe("node:http", () => { req.end(); }); }); + it("re-implemented writeHead, issue#3585", done => { + runTest(done, (server, serverPort, done) => { + const req = request(`http://localhost:${serverPort}/customWriteHead`, res => { + expect(res.headers["content-type"]).toBe("text/plain"); + expect(res.headers["x-powered-by"]).toBe("Bun"); + done(); + }); + req.end(); + }); + }); }); describe("signal", () => { |