@@ -112,80 +112,18 @@ def index():
112112 set_rate_limits (self .max_token_requests , self .max_ip_requests , self .ratelimit_window )
113113 self .make_requests (client , self .max_token_requests , token = "Token %s" % valid_user )
114114
115- def test_custom_ip_limit (self ):
116- """Test that per_ip_limit parameter overrides global limit."""
117- custom_limit = 2
118-
119- @self .app .route ("/custom" )
120- @ratelimit (per_ip_limit = custom_limit , window = 60 )
121- def custom_endpoint ():
122- return "OK"
123-
124- client = self .app .test_client ()
125-
126- # First request should succeed
127- response = client .get ("/custom" )
128- self .assertEqual (response .status_code , 200 )
129- self .assertEqual (response .headers ["X-RateLimit-Limit" ], str (custom_limit ))
130- self .assertEqual (response .headers ["X-RateLimit-Remaining" ], "1" )
131-
132- response = client .get ("/custom" )
133- self .assertEqual (response .status_code , 200 )
134- self .assertEqual (response .headers ["X-RateLimit-Remaining" ], "0" )
135-
136- response = client .get ("/custom" )
137- self .assertEqual (response .status_code , 429 )
138-
139- def test_custom_window (self ):
140- """Test that window parameter works correctly."""
141- @self .app .route ("/short-window" )
142- @ratelimit (per_ip_limit = 1 , window = 2 )
143- def short_window_endpoint ():
144- return "OK"
145-
146- client = self .app .test_client ()
147-
148- response = client .get ("/short-window" )
149- self .assertEqual (response .status_code , 200 )
150- response = client .get ("/short-window" )
151- self .assertEqual (response .status_code , 429 )
152-
153- sleep (2.5 )
154- response = client .get ("/short-window" )
155- self .assertEqual (response .status_code , 200 )
156-
157- def test_headers_contain_correct_values (self ):
158- """Test that rate limit headers contain expected values."""
159- limit = 5
160- window = 30
161-
162- @self .app .route ("/headers" )
163- @ratelimit (per_ip_limit = limit , window = window )
164- def headers_endpoint ():
165- return "OK"
166-
167- client = self .app .test_client ()
168- response = client .get ("/headers" )
169-
170- self .assertEqual (response .status_code , 200 )
171- self .assertIn ("X-RateLimit-Limit" , response .headers )
172- self .assertIn ("X-RateLimit-Remaining" , response .headers )
173- self .assertIn ("X-RateLimit-Reset-In" , response .headers )
174-
175- self .assertEqual (response .headers ["X-RateLimit-Limit" ], str (limit ))
176- self .assertEqual (response .headers ["X-RateLimit-Remaining" ], str (limit - 1 ))
177- self .assertLessEqual (int (response .headers ["X-RateLimit-Reset-In" ]), window )
178- self .assertIn ("X-RateLimit-Reset" , response .headers )
179-
180115 def test_scope_isolation (self ):
181116 """Test that different scopes have independent rate limit buckets."""
117+ set_rate_limits (per_token = 100 , per_ip = 2 , window = 60 , scope = "scope_a" )
118+ set_rate_limits (per_token = 100 , per_ip = 2 , window = 60 , scope = "scope_b" )
119+
182120 @self .app .route ("/scope-a" )
183- @ratelimit (scope = "scope_a" , per_ip_limit = 2 , window = 60 )
121+ @ratelimit (scope = "scope_a" )
184122 def scope_a_endpoint ():
185123 return "A"
186124
187125 @self .app .route ("/scope-b" )
188- @ratelimit (scope = "scope_b" , per_ip_limit = 2 , window = 60 )
126+ @ratelimit (scope = "scope_b" )
189127 def scope_b_endpoint ():
190128 return "B"
191129
@@ -245,13 +183,16 @@ def shared_2_endpoint():
245183
246184 def test_no_scope_vs_scoped (self ):
247185 """Test that unscoped and scoped endpoints have separate buckets."""
186+ set_rate_limits (per_token = 100 , per_ip = 1 , window = 60 )
187+ set_rate_limits (per_token = 100 , per_ip = 1 , window = 60 , scope = "my_scope" )
188+
248189 @self .app .route ("/unscoped" )
249- @ratelimit (per_ip_limit = 1 , window = 60 )
190+ @ratelimit ()
250191 def unscoped_endpoint ():
251192 return "Unscoped"
252193
253194 @self .app .route ("/scoped" )
254- @ratelimit (scope = "my_scope" , per_ip_limit = 1 , window = 60 )
195+ @ratelimit (scope = "my_scope" )
255196 def scoped_endpoint ():
256197 return "Scoped"
257198
@@ -290,31 +231,7 @@ def test_set_and_get_scope_limits(self):
290231 self .assertIsNotNone (result )
291232 self .assertEqual (result ["per_token" ], None )
292233 self .assertEqual (result ["per_ip" ], None )
293- self .assertEqual (result ["window" ], None
294- )
295-
296- def test_decorator_overrides_cache_scope_limits (self ):
297- """Test that decorator parameters override scope limits from cache."""
298- scope = "override_scope"
299- set_rate_limits (per_token = 100 , per_ip = 10 , window = 60 , scope = scope )
300-
301- @self .app .route ("/override" )
302- @ratelimit (scope = scope , per_ip_limit = 2 )
303- def override_endpoint ():
304- return "OK"
305-
306- client = self .app .test_client ()
307-
308- response = client .get ("/override" )
309- self .assertEqual (response .status_code , 200 )
310- self .assertEqual (response .headers ["X-RateLimit-Limit" ], "2" )
311-
312- response = client .get ("/override" )
313- self .assertEqual (response .status_code , 200 )
314-
315- # 3rd request should fail (limit is 2, not 10)
316- response = client .get ("/override" )
317- self .assertEqual (response .status_code , 429 )
234+ self .assertEqual (result ["window" ], None )
318235
319236 def test_scope_cache_values_stored_correctly (self ):
320237 """Test that scope limits are stored in cache with correct keys."""
@@ -359,11 +276,6 @@ def global_endpoint():
359276 def scope_priority_endpoint ():
360277 return "OK"
361278
362- @self .app .route ("/decorator-priority" )
363- @ratelimit (scope = scope , per_ip_limit = 2 )
364- def decorator_priority_endpoint ():
365- return "OK"
366-
367279 client = self .app .test_client ()
368280
369281 response = client .get ("/global" )
@@ -373,7 +285,3 @@ def decorator_priority_endpoint():
373285 response = client .get ("/scope-priority" )
374286 self .assertEqual (response .status_code , 200 )
375287 self .assertEqual (response .headers ["X-RateLimit-Limit" ], "3" )
376-
377- response = client .get ("/decorator-priority" )
378- self .assertEqual (response .status_code , 200 )
379- self .assertEqual (response .headers ["X-RateLimit-Limit" ], "2" )
0 commit comments