Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions apps/commandboard-api/src/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,34 @@ describe("CommandBoard API contracts", () => {
expect(Array.isArray(body.errors)).toBe(true);
});
});

describe("invalid JSON request bodies", () => {
const postEndpoints = [
"/api/tasks",
"/api/plugins/sh1pt/actions/publish",
"/api/plugins/c0mpute/jobs/dispatch",
"/api/plugins/c0mpute/quotes"
];

it.each(postEndpoints)("returns 400 (not 500) for malformed JSON on %s", async (endpoint) => {
const response = await fetch(`${baseUrl}${endpoint}`, {
method: "POST",
headers: { "content-type": "application/json" },
body: "{invalid json"
});
const body = await response.json() as { error: string };

expect(response.status).toBe(400);
expect(body).toEqual({ error: "Invalid JSON body" });
});

it("still returns 422 for well-formed JSON that fails schema validation", async () => {
const response = await fetch(`${baseUrl}/api/tasks`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ title: "missing required fields" })
});

expect(response.status).toBe(422);
});
});
17 changes: 16 additions & 1 deletion apps/commandboard-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export function createCommandBoardServer() {
try {
await route(request, response);
} catch (error) {
if (error instanceof InvalidJsonBodyError) {
json(response, 400, { error: error.message });
return;
}
json(response, 500, { error: error instanceof Error ? error.message : String(error) });
}
});
Expand Down Expand Up @@ -281,13 +285,24 @@ function text(response: ServerResponse, status: number, contentType: string, bod
response.end(body);
}

class InvalidJsonBodyError extends Error {
constructor() {
super("Invalid JSON body");
this.name = "InvalidJsonBodyError";
}
}

async function readJson(request: IncomingMessage) {
const chunks: Buffer[] = [];
for await (const chunk of request) {
chunks.push(Buffer.from(chunk));
}

return JSON.parse(Buffer.concat(chunks).toString("utf8")) as unknown;
try {
return JSON.parse(Buffer.concat(chunks).toString("utf8")) as unknown;
} catch {
throw new InvalidJsonBodyError();
}
}

function isRecord(value: unknown): value is Record<string, unknown> {
Expand Down
Loading