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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { RateLimiter } from "./index";
describe("RateLimiter", () => {
let rateLimiter: RateLimiter;
beforeEach(() => {
rateLimiter = new RateLimiter();
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
rateLimiter.clear();
});
describe("checkRateLimit", () => {
it("should allow requests within rate limit", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 3,
};
const result1 = rateLimiter.checkRateLimit(config, "user1");
const result2 = rateLimiter.checkRateLimit(config, "user1");
const result3 = rateLimiter.checkRateLimit(config, "user1");
expect(result1.allowed).toBe(true);
expect(result2.allowed).toBe(true);
expect(result3.allowed).toBe(true);
});
it("should block requests exceeding rate limit", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 2,
};
const result1 = rateLimiter.checkRateLimit(config, "user1");
const result2 = rateLimiter.checkRateLimit(config, "user1");
const result3 = rateLimiter.checkRateLimit(config, "user1");
expect(result1.allowed).toBe(true);
expect(result2.allowed).toBe(true);
expect(result3.allowed).toBe(false);
expect(result3.resetInSeconds).toBeDefined();
expect(result3.resetInSeconds).toBeGreaterThan(0);
});
it("should reset after window expires", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 2,
};
// First two requests allowed
const result1 = rateLimiter.checkRateLimit(config, "user1");
const result2 = rateLimiter.checkRateLimit(config, "user1");
expect(result1.allowed).toBe(true);
expect(result2.allowed).toBe(true);
// Third request blocked
const result3 = rateLimiter.checkRateLimit(config, "user1");
expect(result3.allowed).toBe(false);
// Advance time past the window
vi.advanceTimersByTime(61000);
// Should allow request after window reset
const result4 = rateLimiter.checkRateLimit(config, "user1");
expect(result4.allowed).toBe(true);
});
it("should isolate rate limits by identifier", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 1,
};
const result1 = rateLimiter.checkRateLimit(config, "user1");
const result2 = rateLimiter.checkRateLimit(config, "user2");
expect(result1.allowed).toBe(true);
expect(result2.allowed).toBe(true);
});
it("should isolate rate limits by key", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 1,
};
const result1 = rateLimiter.checkRateLimit(config, "user1:/api/v1");
const result2 = rateLimiter.checkRateLimit(config, "user1:/api/v2");
expect(result1.allowed).toBe(true);
expect(result2.allowed).toBe(true);
});
it("should isolate rate limits by config name", () => {
const config1 = {
name: "api",
windowMs: 60000,
maxRequests: 1,
};
const config2 = {
name: "auth",
windowMs: 60000,
maxRequests: 1,
};
const result1 = rateLimiter.checkRateLimit(config1, "user1");
const result2 = rateLimiter.checkRateLimit(config2, "user1");
expect(result1.allowed).toBe(true);
expect(result2.allowed).toBe(true);
});
it("should calculate correct resetInSeconds", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 1,
};
// First request allowed
rateLimiter.checkRateLimit(config, "user1");
// Advance time by 30 seconds
vi.advanceTimersByTime(30000);
// Second request blocked
const result = rateLimiter.checkRateLimit(config, "user1");
expect(result.allowed).toBe(false);
// Should have ~30 seconds remaining
expect(result.resetInSeconds).toBeGreaterThan(29);
expect(result.resetInSeconds).toBeLessThanOrEqual(30);
});
});
describe("reset", () => {
it("should reset rate limit for specific identifier", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 1,
};
// Use up the limit
rateLimiter.checkRateLimit(config, "user1");
const result1 = rateLimiter.checkRateLimit(config, "user1");
expect(result1.allowed).toBe(false);
// Reset the limit
rateLimiter.reset(config, "user1");
// Should allow request again
const result2 = rateLimiter.checkRateLimit(config, "user1");
expect(result2.allowed).toBe(true);
});
it("should reset rate limit for specific key", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 1,
};
// Use up the limit for key1
rateLimiter.checkRateLimit(config, "user1:/path1");
const result1 = rateLimiter.checkRateLimit(config, "user1:/path1");
expect(result1.allowed).toBe(false);
// Reset only key1
rateLimiter.reset(config, "user1:/path1");
// key1 should be allowed
const result2 = rateLimiter.checkRateLimit(config, "user1:/path1");
expect(result2.allowed).toBe(true);
});
it("should not affect other identifiers", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 1,
};
// Use up limits for both users
rateLimiter.checkRateLimit(config, "user1");
rateLimiter.checkRateLimit(config, "user2");
// Reset only user1
rateLimiter.reset(config, "user1");
const result1 = rateLimiter.checkRateLimit(config, "user1");
const result2 = rateLimiter.checkRateLimit(config, "user2");
expect(result1.allowed).toBe(true);
expect(result2.allowed).toBe(false);
});
});
describe("clear", () => {
it("should clear all rate limits", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 1,
};
// Use up limits for multiple users
rateLimiter.checkRateLimit(config, "user1");
rateLimiter.checkRateLimit(config, "user2");
// Clear all limits
rateLimiter.clear();
// All should be allowed
const result1 = rateLimiter.checkRateLimit(config, "user1");
const result2 = rateLimiter.checkRateLimit(config, "user2");
expect(result1.allowed).toBe(true);
expect(result2.allowed).toBe(true);
});
});
describe("cleanup", () => {
it("should cleanup expired entries", () => {
const config = {
name: "test",
windowMs: 60000,
maxRequests: 1,
};
// Create an entry
rateLimiter.checkRateLimit(config, "user1");
// Advance time past window + cleanup interval
vi.advanceTimersByTime(61000 + 60000);
// Entry should be cleaned up and new request allowed
const result = rateLimiter.checkRateLimit(config, "user1");
expect(result.allowed).toBe(true);
});
});
});
|