forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handling.test.ts
More file actions
57 lines (47 loc) · 1.66 KB
/
Copy patherror-handling.test.ts
File metadata and controls
57 lines (47 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import request from "supertest";
import { describe, expect, it } from "vitest";
import { createApp } from "../src/app";
describe("http error handling", () => {
const app = createApp();
it("returns 400 for malformed JSON bodies", async () => {
const response = await request(app)
.post("/api/v1/agents")
.set("Content-Type", "application/json")
.send('{"name": "Broken"');
expect(response.status).toBe(400);
expect(response.body).toMatchObject({
success: false,
message: "Malformed JSON request body",
});
});
it("returns 413 for oversized JSON bodies", async () => {
const response = await request(app)
.post("/api/v1/agents")
.send({ description: "a".repeat(2 * 1024 * 1024) });
expect(response.status).toBe(413);
expect(response.body).toMatchObject({
success: false,
message: "Request body too large",
});
});
it("returns 403 with the standard envelope for denied CORS origins", async () => {
const response = await request(app)
.get("/api/v1/health")
.set("Origin", "https://evil.example");
expect(response.status).toBe(403);
expect(response.headers["access-control-allow-origin"]).toBeUndefined();
expect(response.body).toMatchObject({
success: false,
message: "Origin is not allowed by CORS policy",
});
});
it("keeps allowed-origin CORS behavior unchanged", async () => {
const response = await request(app)
.get("/api/v1/health")
.set("Origin", "http://localhost:3000");
expect(response.status).toBe(200);
expect(response.headers["access-control-allow-origin"]).toBe(
"http://localhost:3000",
);
});
});