-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sendNoContent): preserve custom status code if already set (#577)
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [], | ||
}); | ||
}); | ||
}); | ||
}); |