Compare commits

...

2 Commits

Author SHA1 Message Date
autofix-ci[bot]
d57507a95c [autofix.ci] apply automated fixes 2024-01-22 17:10:18 +00:00
Ashcon Partovi
9937204e69 Allow statusCode and statusMessage to be changed on IncomingMessage 2024-01-22 09:07:12 -08:00
3 changed files with 41 additions and 3 deletions

View File

@@ -34,5 +34,4 @@ group("ls .", () => {
});
});
await run();

View File

@@ -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");
@@ -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;
},
});

View File

@@ -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();
});