test: add unit tests for rate limiter middleware
This commit is contained in:
82
tests/middleware/rateLimit.test.ts
Normal file
82
tests/middleware/rateLimit.test.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { beforeEach, describe, expect, it } from "bun:test";
|
||||
import { Hono } from "hono";
|
||||
import { _resetForTesting, rateLimit } from "../../src/server/middleware/rateLimit";
|
||||
|
||||
function createApp() {
|
||||
const app = new Hono();
|
||||
app.post("/login", rateLimit, (c) => c.json({ ok: true }));
|
||||
app.post("/setup", rateLimit, (c) => c.json({ ok: true }));
|
||||
return app;
|
||||
}
|
||||
|
||||
function makeRequest(app: Hono, path: string, ip = "127.0.0.1") {
|
||||
return app.request(path, {
|
||||
method: "POST",
|
||||
headers: { "x-forwarded-for": ip },
|
||||
});
|
||||
}
|
||||
|
||||
describe("rateLimit middleware", () => {
|
||||
beforeEach(() => {
|
||||
_resetForTesting();
|
||||
});
|
||||
|
||||
it("allows first request through", async () => {
|
||||
const app = createApp();
|
||||
const res = await makeRequest(app, "/login");
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
it("allows up to 5 requests", async () => {
|
||||
const app = createApp();
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const res = await makeRequest(app, "/login");
|
||||
expect(res.status).toBe(200);
|
||||
}
|
||||
});
|
||||
|
||||
it("returns 429 after 5 requests", async () => {
|
||||
const app = createApp();
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await makeRequest(app, "/login");
|
||||
}
|
||||
const res = await makeRequest(app, "/login");
|
||||
expect(res.status).toBe(429);
|
||||
const body = await res.json();
|
||||
expect(body.error).toBe("Too many attempts. Try again later.");
|
||||
});
|
||||
|
||||
it("includes Retry-After header on 429", async () => {
|
||||
const app = createApp();
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await makeRequest(app, "/login");
|
||||
}
|
||||
const res = await makeRequest(app, "/login");
|
||||
expect(res.status).toBe(429);
|
||||
const retryAfter = res.headers.get("Retry-After");
|
||||
expect(retryAfter).toBeTruthy();
|
||||
expect(Number(retryAfter)).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("tracks different IPs independently", async () => {
|
||||
const app = createApp();
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await makeRequest(app, "/login", "10.0.0.1");
|
||||
}
|
||||
const blocked = await makeRequest(app, "/login", "10.0.0.1");
|
||||
expect(blocked.status).toBe(429);
|
||||
const allowed = await makeRequest(app, "/login", "10.0.0.2");
|
||||
expect(allowed.status).toBe(200);
|
||||
});
|
||||
|
||||
it("tracks different paths independently", async () => {
|
||||
const app = createApp();
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await makeRequest(app, "/login");
|
||||
}
|
||||
const blockedLogin = await makeRequest(app, "/login");
|
||||
expect(blockedLogin.status).toBe(429);
|
||||
const allowedSetup = await makeRequest(app, "/setup");
|
||||
expect(allowedSetup.status).toBe(200);
|
||||
});
|
||||
});
|
||||
79
tests/routes/params.test.ts
Normal file
79
tests/routes/params.test.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { beforeEach, describe, expect, it } from "bun:test";
|
||||
import { Hono } from "hono";
|
||||
import { categoryRoutes } from "../../src/server/routes/categories";
|
||||
import { itemRoutes } from "../../src/server/routes/items";
|
||||
import { setupRoutes } from "../../src/server/routes/setups";
|
||||
import { threadRoutes } from "../../src/server/routes/threads";
|
||||
import { createTestDb } from "../helpers/db";
|
||||
|
||||
function createTestApp() {
|
||||
const db = createTestDb();
|
||||
const app = new Hono();
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
await next();
|
||||
});
|
||||
app.route("/api/items", itemRoutes);
|
||||
app.route("/api/categories", categoryRoutes);
|
||||
app.route("/api/threads", threadRoutes);
|
||||
app.route("/api/setups", setupRoutes);
|
||||
return app;
|
||||
}
|
||||
|
||||
describe("Invalid ID parameter handling", () => {
|
||||
let app: Hono;
|
||||
|
||||
beforeEach(() => {
|
||||
app = createTestApp();
|
||||
});
|
||||
|
||||
describe("items", () => {
|
||||
it("GET /api/items/abc returns 400", async () => {
|
||||
const res = await app.request("/api/items/abc");
|
||||
expect(res.status).toBe(400);
|
||||
const body = await res.json();
|
||||
expect(body.error).toContain("Invalid");
|
||||
});
|
||||
|
||||
it("GET /api/items/0 returns 400", async () => {
|
||||
const res = await app.request("/api/items/0");
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it("GET /api/items/-1 returns 400", async () => {
|
||||
const res = await app.request("/api/items/-1");
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("categories", () => {
|
||||
it("DELETE /api/categories/abc returns 400", async () => {
|
||||
const res = await app.request("/api/categories/abc", { method: "DELETE" });
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("threads", () => {
|
||||
it("GET /api/threads/abc returns 400", async () => {
|
||||
const res = await app.request("/api/threads/abc");
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it("GET /api/threads/1.5 returns 400", async () => {
|
||||
const res = await app.request("/api/threads/1.5");
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setups", () => {
|
||||
it("GET /api/setups/abc returns 400", async () => {
|
||||
const res = await app.request("/api/setups/abc");
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it("GET /api/setups/0 returns 400", async () => {
|
||||
const res = await app.request("/api/setups/0");
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user