feat(api): standardize errors as problem details
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { GET, POST } from "./route";
|
||||
|
||||
describe("Velocity access API problems", () => {
|
||||
it("returns RFC 9457 for unsupported methods", async () => {
|
||||
const response = GET(new Request("http://localhost/api/velocity/access"));
|
||||
|
||||
expect(response.status).toBe(405);
|
||||
expect(response.headers.get("allow")).toBe("POST");
|
||||
expect(response.headers.get("content-type")).toBe("application/problem+json");
|
||||
expect(await response.json()).toMatchObject({
|
||||
type: "urn:error:method-not-allowed",
|
||||
title: "Method not allowed",
|
||||
status: 405,
|
||||
instance: "/api/velocity/access",
|
||||
});
|
||||
});
|
||||
|
||||
it("returns RFC 9457 when credentials are missing", async () => {
|
||||
const response = await POST(new Request("http://localhost/api/velocity/access", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({}),
|
||||
}));
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(response.headers.get("content-type")).toBe("application/problem+json");
|
||||
expect(await response.json()).toEqual({
|
||||
type: "urn:error:unauthorized",
|
||||
title: "Unauthorized",
|
||||
status: 401,
|
||||
detail: "A valid Velocity server credential is required.",
|
||||
instance: "/api/velocity/access",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects unsupported media types as Problem Details", async () => {
|
||||
const response = await POST(new Request("http://localhost/api/velocity/access", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
authorization: "Bearer test-token",
|
||||
"content-type": "text/plain",
|
||||
},
|
||||
body: "not json",
|
||||
}));
|
||||
|
||||
expect(response.status).toBe(415);
|
||||
expect(response.headers.get("content-type")).toBe("application/problem+json");
|
||||
expect(await response.json()).toMatchObject({
|
||||
type: "urn:error:unsupported-media-type",
|
||||
title: "Unsupported media type",
|
||||
status: 415,
|
||||
instance: "/api/velocity/access",
|
||||
});
|
||||
});
|
||||
|
||||
it("includes machine-readable validation issues", async () => {
|
||||
const response = await POST(new Request("http://localhost/api/velocity/access", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
authorization: "Bearer test-token",
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ serverId: "velocity-main" }),
|
||||
}));
|
||||
const problem = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.headers.get("content-type")).toBe("application/problem+json");
|
||||
expect(problem).toMatchObject({
|
||||
type: "urn:error:invalid-velocity-access-request",
|
||||
title: "Invalid Velocity access request",
|
||||
status: 400,
|
||||
instance: "/api/velocity/access",
|
||||
extensions: { issues: expect.any(Array) },
|
||||
});
|
||||
expect(problem.extensions.issues.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user