Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(HTTP Server): can't set custom statusText #10266

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/bun.js/api/server.zig
Expand Up @@ -1947,7 +1947,12 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
this.flags.has_written_status = true;

if (this.resp) |resp| {
if (HTTPStatusText.get(status)) |text| {
var response: *JSC.WebCore.Response = this.response_ptr.?;
const status_text = response.statusText();

if (status_text.length() != 0) {
resp.writeStatus(std.fmt.bufPrint(&status_text_buf, "{d} {s}", .{ status, status_text.byteSlice() }) catch unreachable);
} else if (HTTPStatusText.get(status)) |text| {
resp.writeStatus(text);
} else {
resp.writeStatus(std.fmt.bufPrint(&status_text_buf, "{d} HM", .{status}) catch unreachable);
Expand Down
4 changes: 4 additions & 0 deletions src/bun.js/webcore/response.zig
Expand Up @@ -116,6 +116,10 @@ pub const Response = struct {
return this.init.status_code;
}

pub inline fn statusText(this: *const Response) bun.String {
return this.init.status_text;
}

pub fn redirectLocation(this: *const Response) ?[]const u8 {
return this.header(.Location);
}
Expand Down
39 changes: 38 additions & 1 deletion test/js/bun/http/bun-server.test.ts
Expand Up @@ -3,7 +3,44 @@ import { bunExe, bunEnv } from "harness";
import path from "path";

describe("Server", () => {
test("normlizes incoming request URLs", async () => {
test("should set a custom statusText", async () => {
const server = Bun.serve({
fetch(request) {
return new Response(request.url, {
headers: {
"Connection": "close",
},
statusText: "customStatusText",
});
},
port: 0,
});

const response = await fetch(server.url);
expect(response.statusText).toBe("customStatusText");

server.stop(true);
});

test("should set the default statusText if not statusText is defined", async () => {
const server = Bun.serve({
fetch(request) {
return new Response(request.url, {
headers: {
"Connection": "close",
},
});
},
port: 0,
});

const response = await fetch(server.url);
expect(response.statusText).toBe("OK");

server.stop(true);
});

test("normalizes incoming request URLs", async () => {
const server = Bun.serve({
fetch(request) {
return new Response(request.url, {
Expand Down
36 changes: 36 additions & 0 deletions test/js/node/http/node-http.test.ts
Expand Up @@ -45,6 +45,42 @@ function listen(server: Server, protocol: string = "http"): Promise<URL> {

describe("node:http", () => {
describe("createServer", async () => {
it("should set a custom statusText", async () => {
try {
var server = createServer((req, res) => {
res.writeHead(200, "customStatusText");

res.end("Hello World");
});
const url = await listen(server);
const res = await fetch(new URL(url));

expect(res.statusText).toBe("customStatusText");
} catch (e) {
throw e;
} finally {
server.close();
}
});

it("should set the default statusText if not custom statusText is defined", async () => {
try {
var server = createServer((req, res) => {
res.writeHead(200);

res.end("Hello World");
});
const url = await listen(server);
const res = await fetch(new URL(url));

expect(res.statusText).toBe("OK");
} catch (e) {
throw e;
} finally {
server.close();
}
});

it("hello world", async () => {
try {
var server = createServer((req, res) => {
Expand Down