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(sendNoContent): preserve custom status code if already set #577

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: [],
});
});
});
});