Skip to content

Commit

Permalink
fix(sendNoContent): preserve custom status code if already set (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolodev authored Nov 20, 2023
1 parent 7a8209b commit 065a4df
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export function sendNoContent(event: H3Event, code?: number) {
if (event.handled) {
return;
}

if (!code && event.node.res.statusCode !== 200) {
// status code was set with setResponseStatus
code = event.node.res.statusCode;
}
const _code = sanitizeStatusCode(code, 204);
// 204 responses MUST NOT have a Content-Length header field
// https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2
Expand Down
113 changes: 113 additions & 0 deletions test/status.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { describe, it, expect, beforeEach } from "vitest";
import {
createApp,
App,
toPlainHandler,
PlainHandler,
eventHandler,
setResponseStatus,
} from "../src";

describe("setResponseStatus", () => {
let app: App;
let handler: PlainHandler;

beforeEach(() => {
app = createApp({ debug: true });
handler = toPlainHandler(app);
});

describe("content response", () => {
it("sets status 200 as default", async () => {
app.use(
"/test",
eventHandler(() => {
return "text";
}),
);

const res = await handler({
method: "POST",
path: "/test",
headers: [],
});

expect(res).toMatchObject({
status: 200,
statusText: "",
body: "text",
headers: [["content-type", "text/html"]],
});
});
it("override status and statusText with setResponeStatus method", async () => {
app.use(
"/test",
eventHandler((event) => {
setResponseStatus(event, 418, "status-text");
return "text";
}),
);

const res = await handler({
method: "POST",
path: "/test",
headers: [],
body: "",
});

expect(res).toMatchObject({
status: 418,
statusText: "status-text",
body: "text",
headers: [["content-type", "text/html"]],
});
});
});

describe("no content response", () => {
it("sets status 204 as default", async () => {
app.use(
"/test",
eventHandler(() => {
return null;
}),
);

const res = await handler({
method: "POST",
path: "/test",
headers: [],
});

expect(res).toMatchObject({
status: 204,
statusText: "",
body: undefined,
headers: [],
});
});
it("override status and statusText with setResponeStatus method", async () => {
app.use(
"/test",
eventHandler((event) => {
setResponseStatus(event, 418, "status-text");
return null;
}),
);

const res = await handler({
method: "POST",
path: "/test",
headers: [],
body: "",
});

expect(res).toMatchObject({
status: 418,
statusText: "status-text",
body: undefined,
headers: [],
});
});
});
});

0 comments on commit 065a4df

Please sign in to comment.