forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate-limiter-envelope.test.ts
More file actions
76 lines (68 loc) · 2.75 KB
/
Copy pathrate-limiter-envelope.test.ts
File metadata and controls
76 lines (68 loc) · 2.75 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { describe, it, expect } from "vitest";
import express from "express";
import request from "supertest";
import rateLimit from "express-rate-limit";
describe("Rate limiter standard error envelope (issue #79)", () => {
const createAppWithLimiter = () => {
const app = express();
app.use(express.json());
const limiter = rateLimit({
windowMs: 60_000,
limit: 2,
standardHeaders: true,
legacyHeaders: false,
handler: (_req, res) => {
const retryAfter = res.getHeader("Retry-After");
res.status(429).json({
success: false,
message: "Too many requests, please try again later.",
details: retryAfter !== undefined ? { retryAfterSeconds: Number(retryAfter) } : undefined,
});
},
});
app.get("/api/v1/test", limiter, (_req, res) => {
res.json({ success: true, data: { ok: true } });
});
return app;
};
it("should return standard error envelope on 429", async () => {
const app = createAppWithLimiter();
await request(app).get("/api/v1/test");
await request(app).get("/api/v1/test");
const res = await request(app).get("/api/v1/test");
expect(res.status).toBe(429);
expect(res.body.success).toBe(false);
expect(res.body.message).toBe("Too many requests, please try again later.");
});
it("should include Retry-After header on 429 responses", async () => {
const app = createAppWithLimiter();
await request(app).get("/api/v1/test");
await request(app).get("/api/v1/test");
const res = await request(app).get("/api/v1/test");
expect(res.status).toBe(429);
expect(res.headers["retry-after"]).toBeDefined();
});
it("should include retryAfterSeconds in details when Retry-After is set", async () => {
const app = createAppWithLimiter();
await request(app).get("/api/v1/test");
await request(app).get("/api/v1/test");
const res = await request(app).get("/api/v1/test");
expect(res.status).toBe(429);
expect(res.body.details).toBeDefined();
expect(typeof res.body.details.retryAfterSeconds).toBe("number");
expect(res.body.details.retryAfterSeconds).toBeGreaterThan(0);
});
it("should not include details field when no extra context is available", async () => {
// This test documents that details is optional and only present
// when Retry-After header is available from the rate limiter
const app = createAppWithLimiter();
await request(app).get("/api/v1/test");
await request(app).get("/api/v1/test");
const res = await request(app).get("/api/v1/test");
expect(res.status).toBe(429);
// details may or may not be present depending on timing
if (res.body.details) {
expect(res.body.details).toHaveProperty("retryAfterSeconds");
}
});
});