Files
lik 9a1242ffbc fix: 限流分桶按路由模式归一并统一清理过期计数
- 分桶键用路由模式(如 /health/escort-record/:id)而非真实 path,
  防止换订单 ID 绕开额度
- 计数桶统一登记、单定时器清理,windowMs 写入记录供过期判断
2026-11-08 12:00:26 +08:00

123 lines
3.2 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
rateLimit,
createUserLimiter,
sweepRateLimitStores,
normalizeDynamicPath,
} from "../middleware/ratelimit.js";
const ID_A = "64b000000000000000000001";
const ID_B = "64b000000000000000000002";
function unique(prefix) {
return `${prefix}-${Date.now()}-${Math.random()}`;
}
function createCtx({
userId = null,
method = "PUT",
path = "/health/test",
routerPath = undefined,
ip = "203.0.113.9",
} = {}) {
return {
state: { user: userId ? { _id: userId } : null },
method,
path,
routerPath,
ip,
headers: {},
status: 200,
body: null,
set(name, value) {
this.headers[name] = value;
},
};
}
test("限流按路由模式分桶,换订单 ID 不能绕过", async () => {
const user = unique("route");
const middleware = rateLimit(1, 60_000);
const first = createCtx({
userId: user,
path: `/health/escort-record/${ID_A}`,
routerPath: "/health/escort-record/:id",
});
const second = createCtx({
userId: user,
path: `/health/escort-record/${ID_B}`,
routerPath: "/health/escort-record/:id",
});
await middleware(first, async () => {});
await middleware(second, async () => {});
assert.equal(first.body, null);
assert.equal(second.body.code, 429);
assert.ok(Number(second.headers["Retry-After"]) > 0);
});
test("拿不到路由模式时把 ObjectId 路径段归一成 :id", async () => {
const user = unique("fallback");
const middleware = rateLimit(1, 60_000);
const first = createCtx({ userId: user, path: `/health/escort-record/${ID_A}` });
const second = createCtx({ userId: user, path: `/health/escort-record/${ID_B}` });
await middleware(first, async () => {});
await middleware(second, async () => {});
assert.equal(first.body, null);
assert.equal(second.body.code, 429);
assert.equal(
normalizeDynamicPath(`/health/escort-record/${ID_A}/pay`),
"/health/escort-record/:id/pay"
);
});
test("不同路由各自独立计数", async () => {
const user = unique("separate");
const middleware = rateLimit(1, 60_000);
const recordRoute = createCtx({
userId: user,
path: `/health/escort-record/${ID_A}`,
routerPath: "/health/escort-record/:id",
});
const profileRoute = createCtx({
userId: user,
path: `/health/health-profile/${ID_A}`,
routerPath: "/health/health-profile/:id",
});
await middleware(recordRoute, async () => {});
await middleware(profileRoute, async () => {});
assert.equal(recordRoute.body, null);
assert.equal(profileRoute.body, null);
});
test("按用户限流在窗口内生效,过期后可重新计数", () => {
const limiter = createUserLimiter(2, 60_000);
const userId = unique("ws");
assert.equal(limiter(userId).allowed, true);
assert.equal(limiter(userId).allowed, true);
assert.equal(limiter(userId).allowed, false);
// 推进到窗口之外,清理后重新计数
sweepRateLimitStores(Date.now() + 120_000);
assert.equal(limiter(userId).allowed, true);
});
test("统一清理会回收用户限流的过期桶", () => {
const limiter = createUserLimiter(5, 1000);
limiter(unique("sweep"));
const removed = sweepRateLimitStores(Date.now() + 5000);
assert.ok(removed >= 1);
});