diff --git a/cachecontrol/controller.py b/cachecontrol/controller.py index 03b2218..dc034d1 100644 --- a/cachecontrol/controller.py +++ b/cachecontrol/controller.py @@ -149,8 +149,8 @@ def _load_from_cache(self, request: PreparedRequest) -> HTTPResponse | None: if "Range" in request.headers: return None - cache_url = request.url - assert cache_url is not None + assert request.url is not None + cache_url = self.cache_url(request.url) cache_data = self.cache.get(cache_url) if cache_data is None: logger.debug("No cache entry available") diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 78e8aaa..be7c74a 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -21,6 +21,21 @@ def test_file_cache_recognizes_consumed_file_handle(self, url): s.close() +class TestCacheKeyNormalization: + def test_fragment_in_url_still_hits_the_cache(self, url): + """The cache is keyed on the normalized URL, which drops the fragment. + + Reads have to normalize too, otherwise a request for a URL carrying a + fragment stores an entry it can never look up again. + """ + s = CacheControl(Session()) + the_url = url + "cache_60#section" + s.get(the_url) + r = s.get(the_url) + assert r.from_cache + s.close() + + def test_getattr_during_gc(): s = CallbackFileWrapper(None, None) # normal behavior: