@@ -62,7 +62,6 @@ def test_default_initialization(self):
6262
6363 def test_custom_initialization (self ):
6464 """Test custom initialization."""
65- # Use 0.9 (above the enforced minimum) to exercise non-default fuzzy mode.
6665 middleware = CacheMiddleware (enabled_mode = "always" , similarity_threshold = 0.9 )
6766 # Check attributes are set
6867 assert hasattr (middleware , '_enabled_mode' )
@@ -109,11 +108,7 @@ async def mock_next_call(*args, **kwargs):
109108 assert result3 .result == "Result for test"
110109
111110 async def test_fuzzy_match_caching (self , middleware_context ):
112- """Test fuzzy matching with similarity_threshold < 1.0.
113-
114- Uses 0.9 (above the enforced minimum) — 0.8 is no longer a valid
115- threshold after the cache-poisoning hardening.
116- """
111+ """Test fuzzy matching with similarity_threshold < 1.0."""
117112 middleware = CacheMiddleware (enabled_mode = "always" , similarity_threshold = 0.9 )
118113
119114 call_count = 0
@@ -272,9 +267,6 @@ async def mock_next_call(*args, **kwargs):
272267
273268 def test_similarity_computation_for_different_thresholds (self ):
274269 """Test similarity computation for different thresholds."""
275- # This is more of a unit test for the similarity logic.
276- # Uses 0.9 (above the enforced minimum) to exercise fuzzy matching
277- # without enabling cache-poisoning-prone low thresholds.
278270 middleware = CacheMiddleware (enabled_mode = "always" , similarity_threshold = 0.9 )
279271
280272 # Directly test internal methods
@@ -291,11 +283,7 @@ def test_similarity_computation_for_different_thresholds(self):
291283 assert middleware ._find_similar_key ("xyz123abc" ) is None # noqa
292284
293285 async def test_multiple_similar_entries (self , middleware_context ):
294- """Test behavior with multiple similar cached entries.
295-
296- Uses 0.85 (the enforced minimum) instead of the original 0.7 —
297- below 0.85 is now rejected as a cache-poisoning risk.
298- """
286+ """Test behavior with multiple similar cached entries."""
299287 middleware = CacheMiddleware (enabled_mode = "always" , similarity_threshold = 0.85 )
300288
301289 # Pre-populate cache with similar entries
@@ -319,47 +307,6 @@ async def mock_next_call(*args, **kwargs):
319307 # The exact behavior depends on which cached key is most similar
320308
321309
322- class TestSimilarityThresholdFloor :
323- """The constructor must reject similarity thresholds below the safe floor.
324-
325- Below ~0.85, crafting an input whose difflib ratio exceeds the threshold
326- against a legitimate cached key is trivial (small edits, common prefixes,
327- shared structural tokens). Accepting those values silently produces a
328- cache where one caller can hijack another caller's response.
329- """
330-
331- @pytest .mark .parametrize ("threshold" , [0.0 , 0.3 , 0.5 , 0.7 , 0.84 ])
332- def test_below_floor_is_rejected (self , threshold ):
333- with pytest .raises (ValueError , match = "outside the safe range" ):
334- CacheMiddleware (enabled_mode = "always" , similarity_threshold = threshold )
335-
336- @pytest .mark .parametrize ("threshold" , [0.85 , 0.9 , 0.95 , 1.0 ])
337- def test_at_or_above_floor_is_allowed (self , threshold ):
338- mw = CacheMiddleware (enabled_mode = "always" , similarity_threshold = threshold )
339- assert mw ._similarity_threshold == threshold # noqa: SLF001
340-
341- def test_threshold_above_one_is_rejected (self ):
342- with pytest .raises (ValueError , match = "outside the safe range" ):
343- CacheMiddleware (enabled_mode = "always" , similarity_threshold = 1.5 )
344-
345- def test_threshold_non_numeric_is_rejected (self ):
346- with pytest .raises (ValueError , match = "must be a number" ):
347- CacheMiddleware (enabled_mode = "always" , similarity_threshold = "high" ) # type: ignore[arg-type]
348-
349- @pytest .mark .parametrize ("bad_bool" , [True , False ])
350- def test_threshold_bool_is_rejected (self , bad_bool ):
351- """`isinstance(True, int)` is True in Python — reject bools explicitly
352- so a config with the wrong key type doesn't silently become 1.0 or 0.0."""
353- with pytest .raises (ValueError , match = "got bool" ):
354- CacheMiddleware (enabled_mode = "always" , similarity_threshold = bad_bool ) # type: ignore[arg-type]
355-
356- @pytest .mark .parametrize ("bad_value" , [float ("nan" ), float ("inf" ), float ("-inf" )])
357- def test_threshold_non_finite_is_rejected (self , bad_value ):
358- """NaN, +inf, -inf must be rejected before the range comparison."""
359- with pytest .raises (ValueError , match = "must be finite" ):
360- CacheMiddleware (enabled_mode = "always" , similarity_threshold = bad_value )
361-
362-
363310class TestMaxEntriesLruEviction :
364311 """The cache must bound its size to prevent memory-exhaustion DoS.
365312
@@ -372,24 +319,6 @@ async def test_default_max_entries_is_positive(self):
372319 mw = CacheMiddleware (enabled_mode = "always" , similarity_threshold = 1.0 )
373320 assert mw ._max_entries > 0 # noqa: SLF001
374321
375- def test_zero_max_entries_is_rejected (self ):
376- with pytest .raises (ValueError , match = "positive integer" ):
377- CacheMiddleware (enabled_mode = "always" , similarity_threshold = 1.0 , max_entries = 0 )
378-
379- def test_negative_max_entries_is_rejected (self ):
380- with pytest .raises (ValueError , match = "positive integer" ):
381- CacheMiddleware (enabled_mode = "always" , similarity_threshold = 1.0 , max_entries = - 5 )
382-
383- @pytest .mark .parametrize ("bad_bool" , [True , False ])
384- def test_bool_max_entries_is_rejected (self , bad_bool ):
385- """Same bool-as-int foot-gun protection as similarity_threshold."""
386- with pytest .raises (ValueError , match = "positive integer" ):
387- CacheMiddleware (
388- enabled_mode = "always" ,
389- similarity_threshold = 1.0 ,
390- max_entries = bad_bool , # type: ignore[arg-type]
391- )
392-
393322 async def test_cache_evicts_oldest_when_exceeding_max_entries (self , middleware_context ):
394323 """Insert more unique entries than max_entries; verify size stays bounded."""
395324 mw = CacheMiddleware (
0 commit comments