Handle undefined body

In the latest Express it seems the body is undefined when no data is
passed (instead of being empty).
This commit is contained in:
Asher 2024-04-16 11:13:36 -08:00
parent 3d8d544f89
commit fb2afbd9d6
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
4 changed files with 38 additions and 36 deletions

View File

@ -319,8 +319,8 @@ export const getCookieOptions = (req: express.Request): express.CookieOptions =>
// URL of that page) and the relative path to the root as given to it by the // URL of that page) and the relative path to the root as given to it by the
// backend. Using these two we can determine the true absolute root. // backend. Using these two we can determine the true absolute root.
const url = new URL( const url = new URL(
req.query.base || req.body.base || "/", req.query.base || req.body?.base || "/",
req.query.href || req.body.href || "http://" + (req.headers.host || "localhost"), req.query.href || req.body?.href || "http://" + (req.headers.host || "localhost"),
) )
return { return {
domain: getCookieDomain(url.host, req.args["proxy-domain"]), domain: getCookieDomain(url.host, req.args["proxy-domain"]),

View File

@ -68,8 +68,8 @@ router.get("/", async (req, res) => {
res.send(await getRoot(req)) res.send(await getRoot(req))
}) })
router.post<{}, string, { password: string; base?: string }, { to?: string }>("/", async (req, res) => { router.post<{}, string, { password?: string; base?: string } | undefined, { to?: string }>("/", async (req, res) => {
const password = sanitizeString(req.body.password) const password = sanitizeString(req.body?.password)
const hashedPasswordFromArgs = req.args["hashed-password"] const hashedPasswordFromArgs = req.args["hashed-password"]
try { try {

View File

@ -20,11 +20,11 @@ export interface EditorSessionEntry {
} }
interface DeleteSessionRequest { interface DeleteSessionRequest {
socketPath: string socketPath?: string
} }
interface AddSessionRequest { interface AddSessionRequest {
entry: EditorSessionEntry entry?: EditorSessionEntry
} }
interface GetSessionResponse { interface GetSessionResponse {
@ -40,8 +40,10 @@ export async function makeEditorSessionManagerServer(
// eslint-disable-next-line import/no-named-as-default-member // eslint-disable-next-line import/no-named-as-default-member
router.use(express.json()) router.use(express.json())
router.get("/session", async (req, res) => { router.get<{}, GetSessionResponse | string | unknown, undefined, { filePath?: string }>(
const filePath = req.query.filePath as string "/session",
async (req, res) => {
const filePath = req.query.filePath
if (!filePath) { if (!filePath) {
res.status(HttpCode.BadRequest).send("filePath is required") res.status(HttpCode.BadRequest).send("filePath is required")
return return
@ -53,24 +55,27 @@ export async function makeEditorSessionManagerServer(
} catch (error: unknown) { } catch (error: unknown) {
res.status(HttpCode.ServerError).send(error) res.status(HttpCode.ServerError).send(error)
} }
}) },
)
router.post("/add-session", async (req, res) => { router.post<{}, string, AddSessionRequest | undefined>("/add-session", async (req, res) => {
const request = req.body as AddSessionRequest const entry = req.body?.entry
if (!request.entry) { if (!entry) {
res.status(400).send("entry is required") res.status(400).send("entry is required")
return
} }
editorSessionManager.addSession(request.entry) editorSessionManager.addSession(entry)
res.status(200).send() res.status(200).send("session added")
}) })
router.post("/delete-session", async (req, res) => { router.post<{}, string, DeleteSessionRequest | undefined>("/delete-session", async (req, res) => {
const request = req.body as DeleteSessionRequest const socketPath = req.body?.socketPath
if (!request.socketPath) { if (!socketPath) {
res.status(400).send("socketPath is required") res.status(400).send("socketPath is required")
return
} }
editorSessionManager.deleteSession(request.socketPath) editorSessionManager.deleteSession(socketPath)
res.status(200).send() res.status(200).send("session deleted")
}) })
const server = http.createServer(router) const server = http.createServer(router)

View File

@ -68,13 +68,10 @@ describe("login", () => {
} }
}) })
it("should return HTML with 'Missing password' message", async () => { it("should return 'Missing password' without body", async () => {
const resp = await codeServer().fetch("/login", { method: "POST" }) const resp = await codeServer().fetch("/login", { method: "POST" })
expect(resp.status).toBe(200)
const htmlContent = await resp.text() const htmlContent = await resp.text()
expect(resp.status).toBe(200)
expect(htmlContent).toContain("Missing password") expect(htmlContent).toContain("Missing password")
}) })