Skip to content

Commit 666c76e

Browse files
committed
Fix tests
1 parent 6cb8b54 commit 666c76e

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

src/RateLimit.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ Deno.test("RateLimiter - group initialization", () => {
110110

111111
// Check that non-configured groups get empty options (will use defaults)
112112
const otherOptions = rateLimiter.getGroupOptions("other.com");
113-
assertEquals(otherOptions.maxRequests, undefined);
114-
assertEquals(otherOptions.windowSeconds, undefined);
113+
assertEquals(otherOptions.maxRequests, 5);
114+
assertEquals(otherOptions.windowSeconds, 1);
115115

116116
// Test that the group-specific limits are actually used
117117
assertEquals(rateLimiter.isAllowed("https://example.com/test"), true);

src/RateLimitMiddleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export class RateLimitMiddleware {
100100

101101
// Create a 429 Too Many Requests response
102102
const groupOptions = this.rateLimiter.getGroupOptions(group);
103-
const maxRequests = groupOptions.maxRequests ?? 0;
104-
const windowSeconds = groupOptions.windowSeconds ?? 0;
103+
const maxRequests = groupOptions.maxRequests!;
104+
const windowSeconds = groupOptions.windowSeconds!;
105105

106106
// Create IETF standard rate limit headers
107107
const resetSeconds = Math.ceil((resetTime - Date.now()) / 1000);

src/RateLimiter.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ export class RateLimiter {
9797
const now = Date.now();
9898

9999
// Use group-specific options if available, otherwise fall back to global options
100-
const maxRequests = groupOptions.maxRequests ?? this.options.maxRequests;
101-
const windowSeconds = groupOptions.windowSeconds ??
102-
this.options.windowSeconds;
100+
const maxRequests = groupOptions.maxRequests ?? 0;
101+
const windowSeconds = groupOptions.windowSeconds ?? 0;
103102
const onRateLimitExceeded = groupOptions.onRateLimitExceeded ??
104103
this.options.onRateLimitExceeded;
105104

@@ -147,8 +146,7 @@ export class RateLimiter {
147146
}
148147

149148
const now = Date.now();
150-
const windowSeconds = groupOptions.windowSeconds ??
151-
this.options.windowSeconds;
149+
const windowSeconds = groupOptions.windowSeconds ?? 0;
152150
const windowStart = now - (windowSeconds * 1000);
153151
return bucket.requests.filter((time) => time > windowStart).length;
154152
}
@@ -161,7 +159,7 @@ export class RateLimiter {
161159
public getRemainingRequests(url: string): number {
162160
const key = this.options.getGroupFunc(url);
163161
const groupOptions = this.getGroupOptions(key);
164-
const maxRequests = groupOptions.maxRequests ?? this.options.maxRequests;
162+
const maxRequests = groupOptions.maxRequests ?? 0;
165163

166164
return Math.max(
167165
0,
@@ -199,12 +197,28 @@ export class RateLimiter {
199197
}
200198

201199
/**
202-
* Gets the options for a specific group.
200+
* Gets the options for a specific group. Falls back to global options if not set.
203201
* @param group - The group key
204202
* @returns The options for the group
205203
*/
206204
public getGroupOptions(group: string): GroupRateLimiterOptions {
207-
return this.groupOptions.get(group) || {};
205+
const options = this.groupOptions.get(group);
206+
if (!options) {
207+
return {
208+
maxRequests: this.options.maxRequests,
209+
windowSeconds: this.options.windowSeconds,
210+
};
211+
}
212+
return options;
213+
}
214+
215+
/**
216+
* Checks if a group has specific options set.
217+
* @param group - The group key
218+
* @returns True if the group has options, false otherwise
219+
*/
220+
public hasGroupOptions(group: string): boolean {
221+
return this.groupOptions.has(group);
208222
}
209223

210224
/**
@@ -252,7 +266,10 @@ export class RateLimiter {
252266
* @param headers - The response headers containing rate limit information
253267
*/
254268
public updateFromHeaders(group: string, headers: Headers): void {
255-
const currentOptions = this.getGroupOptions(group);
269+
// Get existing group-specific options (not global fallback)
270+
const currentOptions = this.hasGroupOptions(group)
271+
? this.groupOptions.get(group)!
272+
: {};
256273
const newOptions: GroupRateLimiterOptions = { ...currentOptions };
257274

258275
// Parse IETF standard rate limit headers first, then fall back to x-ratelimit headers

0 commit comments

Comments
 (0)