Return 400 for invalid task JSON (#19)

This commit is contained in:
Autowebassat-blip 2026-06-12 06:04:10 +02:00 committed by GitHub
parent 301b4ed535
commit aa7eb32c36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View file

@ -39,6 +39,20 @@ describe("CommandBoard API contracts", () => {
expect(body).toEqual({ ok: true, service: "commandboard-api" }); expect(body).toEqual({ ok: true, service: "commandboard-api" });
}); });
it("rejects malformed task JSON with a client error", async () => {
const response = await fetch(`${baseUrl}/api/tasks`, {
method: "POST",
headers: { "content-type": "application/json" },
body: "{not-json"
});
const body = await response.json() as { error: string };
expect(response.status).toBe(400);
expect(body).toEqual({ error: "Invalid JSON body" });
});
it("exposes default plugin contract including product plugins", async () => { it("exposes default plugin contract including product plugins", async () => {
const response = await fetch(`${baseUrl}/api/plugins`); const response = await fetch(`${baseUrl}/api/plugins`);
const body = await response.json() as { const body = await response.json() as {

View file

@ -104,7 +104,14 @@ async function route(request: IncomingMessage, response: ServerResponse) {
} }
if (request.method === "POST" && url.pathname === "/api/tasks") { if (request.method === "POST" && url.pathname === "/api/tasks") {
const body = await readJson(request); let body: unknown;
try {
body = await readJson(request);
} catch {
json(response, 400, { error: "Invalid JSON body" });
return;
}
const result = validate("task", body); const result = validate("task", body);
if (!result.ok) { if (!result.ok) {
json(response, 422, { errors: result.errors }); json(response, 422, { errors: result.errors });