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
// backend. Using these two we can determine the true absolute root.
const url = new URL(
req.query.base || req.body.base || "/",
req.query.href || req.body.href || "http://" + (req.headers.host || "localhost"),
req.query.base || req.body?.base || "/",
req.query.href || req.body?.href || "http://" + (req.headers.host || "localhost"),
)
return {
domain: getCookieDomain(url.host, req.args["proxy-domain"]),

View File

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

View File

@ -20,11 +20,11 @@ export interface EditorSessionEntry {
}
interface DeleteSessionRequest {
socketPath: string
socketPath?: string
}
interface AddSessionRequest {
entry: EditorSessionEntry
entry?: EditorSessionEntry
}
interface GetSessionResponse {
@ -40,37 +40,42 @@ export async function makeEditorSessionManagerServer(
// eslint-disable-next-line import/no-named-as-default-member
router.use(express.json())
router.get("/session", async (req, res) => {
const filePath = req.query.filePath as string
if (!filePath) {
res.status(HttpCode.BadRequest).send("filePath is required")
router.get<{}, GetSessionResponse | string | unknown, undefined, { filePath?: string }>(
"/session",
async (req, res) => {
const filePath = req.query.filePath
if (!filePath) {
res.status(HttpCode.BadRequest).send("filePath is required")
return
}
try {
const socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
const response: GetSessionResponse = { socketPath }
res.json(response)
} catch (error: unknown) {
res.status(HttpCode.ServerError).send(error)
}
},
)
router.post<{}, string, AddSessionRequest | undefined>("/add-session", async (req, res) => {
const entry = req.body?.entry
if (!entry) {
res.status(400).send("entry is required")
return
}
try {
const socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
const response: GetSessionResponse = { socketPath }
res.json(response)
} catch (error: unknown) {
res.status(HttpCode.ServerError).send(error)
}
editorSessionManager.addSession(entry)
res.status(200).send("session added")
})
router.post("/add-session", async (req, res) => {
const request = req.body as AddSessionRequest
if (!request.entry) {
res.status(400).send("entry is required")
}
editorSessionManager.addSession(request.entry)
res.status(200).send()
})
router.post("/delete-session", async (req, res) => {
const request = req.body as DeleteSessionRequest
if (!request.socketPath) {
router.post<{}, string, DeleteSessionRequest | undefined>("/delete-session", async (req, res) => {
const socketPath = req.body?.socketPath
if (!socketPath) {
res.status(400).send("socketPath is required")
return
}
editorSessionManager.deleteSession(request.socketPath)
res.status(200).send()
editorSessionManager.deleteSession(socketPath)
res.status(200).send("session deleted")
})
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" })
expect(resp.status).toBe(200)
const htmlContent = await resp.text()
expect(resp.status).toBe(200)
expect(htmlContent).toContain("Missing password")
})