mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-08-13 14:37:26 +00:00
Return 400 for invalid task JSON (#19)
This commit is contained in:
parent
301b4ed535
commit
aa7eb32c36
2 changed files with 22 additions and 1 deletions
|
|
@ -39,6 +39,20 @@ describe("CommandBoard API contracts", () => {
|
|||
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 () => {
|
||||
const response = await fetch(`${baseUrl}/api/plugins`);
|
||||
const body = await response.json() as {
|
||||
|
|
|
|||
|
|
@ -104,7 +104,14 @@ async function route(request: IncomingMessage, response: ServerResponse) {
|
|||
}
|
||||
|
||||
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);
|
||||
if (!result.ok) {
|
||||
json(response, 422, { errors: result.errors });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue