diff options
author | Jordan Cook <jordan.cook.git@proton.me> | 2023-03-24 16:46:08 -0700 |
---|---|---|
committer | Jordan Cook <jordan.cook.git@proton.me> | 2023-03-24 18:50:18 -0500 |
commit | 8e4de3c35ebb8028d24dd1a874ec3e3348ed0b6a (patch) | |
tree | ad8c7daec7cfab477e6129c7e4ff99b8000822ac | |
parent | 0dcf40989503df7041eb9f0c4789aecea78cbd5c (diff) | |
parent | 5bcb137ff944533210036d5fcefe0a5f87cde96c (diff) | |
download | requests-cache-8e4de3c35ebb8028d24dd1a874ec3e3348ed0b6a.tar.gz |
Merge pull request #805 from requests-cache/fix-must-revalidatev1.0.1
Ignore Cache-Control: must-revalidate and no-cache when `cache_control=False`
-rw-r--r-- | HISTORY.md | 3 | ||||
-rw-r--r-- | requests_cache/policy/actions.py | 13 | ||||
-rw-r--r-- | tests/unit/policy/test_actions.py | 23 |
3 files changed, 26 insertions, 13 deletions
@@ -3,6 +3,9 @@ ## Unreleased * Add support for regular expressions when using `urls_expire_after` +## 1.0.1 (2023-03-24) +* Ignore `Cache-Control: must-revalidate` and `no-cache` response headers with `cache_control=False` + ## 1.0.0 (2023-03-01) [See all unreleased issues and PRs](https://github.com/requests-cache/requests-cache/milestone/10?closed=1) diff --git a/requests_cache/policy/actions.py b/requests_cache/policy/actions.py index 50960cb..034cb72 100644 --- a/requests_cache/policy/actions.py +++ b/requests_cache/policy/actions.py @@ -268,16 +268,17 @@ class CacheActions(RichMixin): triggered by a stale response, request headers, or cached response headers. """ directives = CacheDirectives.from_headers(cached_response.headers) + # These conditions always apply revalidate = directives.has_validator and ( - cached_response.is_expired - or self._refresh - or directives.no_cache - or directives.must_revalidate - or (self._settings.always_revalidate and directives.has_validator) + cached_response.is_expired or self._refresh or self._settings.always_revalidate + ) + # These conditions only apply if cache_control=True + cc_revalidate = self._settings.cache_control and ( + directives.no_cache or directives.must_revalidate ) # Add the appropriate validation headers, if needed - if revalidate: + if revalidate or cc_revalidate: if directives.etag: self._validation_headers['If-None-Match'] = directives.etag if directives.last_modified: diff --git a/tests/unit/policy/test_actions.py b/tests/unit/policy/test_actions.py index 4ea133c..c50e212 100644 --- a/tests/unit/policy/test_actions.py +++ b/tests/unit/policy/test_actions.py @@ -184,24 +184,33 @@ def test_update_from_cached_response__revalidate(response_headers, expected_vali @pytest.mark.parametrize( - 'request_headers, response_headers', + 'response_headers', [ - ({}, {'Cache-Control': 'no-cache'}), - ({}, {'Cache-Control': 'max-age=0,must-revalidate'}), + {'Cache-Control': 'no-cache'}, + {'Cache-Control': 'max-age=0,must-revalidate'}, ], ) -def test_update_from_cached_response__refresh(request_headers, response_headers): +@pytest.mark.parametrize('cache_control', [True, False]) +def test_update_from_cached_response__force_revalidate(cache_control, response_headers): """Conditional request headers should be added if requested by response headers, even if the response is not expired """ actions = CacheActions.from_request( - 'key', Request(url='https://img.site.com/base/img.jpg', headers=request_headers) + 'key', + request=Request(url='https://img.site.com/base/img.jpg', headers={}), + settings=CacheSettings(cache_control=cache_control), ) cached_response = CachedResponse(headers={'ETag': ETAG, **response_headers}, expires=None) actions.update_from_cached_response(cached_response) - assert actions.send_request is True - assert actions._validation_headers == {'If-None-Match': ETAG} + + # cache_control=False overrides revalidation in this case + if cache_control is False: + assert actions.send_request is False + assert not actions._validation_headers + else: + assert actions.send_request is True + assert actions._validation_headers == {'If-None-Match': ETAG} def test_update_from_cached_response__no_revalidation(): |