Skip to content

Commit

Permalink
Allow statusCode and statusMessage to be changed on IncomingMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Electroid committed Jan 22, 2024
1 parent eaa1cd5 commit 9937204
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/js/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ var isNextIncomingMessageHTTPS = false;

var typeSymbol = Symbol("type");
var reqSymbol = Symbol("req");
var statusSymbol = Symbol("status");
var statusMessageSymbol = Symbol("statusMessage");
var bodyStreamSymbol = Symbol("bodyStream");
var noBodySymbol = Symbol("noBody");
var abortedSymbol = Symbol("aborted");
Expand Down Expand Up @@ -771,13 +773,19 @@ Object.defineProperty(IncomingMessage.prototype, "connection", {

Object.defineProperty(IncomingMessage.prototype, "statusCode", {
get() {
return this[reqSymbol].status;
return this[statusSymbol] ?? this[reqSymbol].status;
},
set(value) {
this[statusSymbol] = value;
},
});

Object.defineProperty(IncomingMessage.prototype, "statusMessage", {
get() {
return STATUS_CODES[this[reqSymbol].status];
return this[statusMessageSymbol] ?? STATUS_CODES[this[reqSymbol].status];
},
set(value) {
this[statusMessageSymbol] = value;
},
});

Expand Down
31 changes: 31 additions & 0 deletions test/regression/issue/08258.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { test, expect, mock, afterAll } from "bun:test";
import type { AddressInfo } from "node:net";
import type { Server } from "node:http";
import { createServer } from "node:http";
import { reject } from "lodash";

let server: Server;

test("can set statusCode and statusMessage on IncomingMessage", async () => {
const fn = mock((req, res) => {
req.statusCode = 404;
expect(req.statusCode).toBe(404);
req.statusMessage = "Who dis?";
expect(req.statusMessage).toBe("Who dis?");
res.end();
});
server = createServer(fn).listen(0);
const url = await new Promise<string>(resolve => {
server.on("listening", async () => {
const { address, port, family } = server.address() as AddressInfo;
resolve(`http://${family === "IPv6" ? `[${address}]` : address}:${port}/`);
});
server.on("error", reject);
});
expect(fetch(url)).resolves.toBeInstanceOf(Response);
expect(fn).toHaveBeenCalledTimes(1);
});

afterAll(() => {
server?.close();
});

0 comments on commit 9937204

Please sign in to comment.