diff options
author | Manuel Eggimann <manuel.eggimann@gmail.com> | 2021-11-30 15:17:58 +0100 |
---|---|---|
committer | Manuel Eggimann <manuel.eggimann@gmail.com> | 2021-11-30 15:17:58 +0100 |
commit | 137e68579331446c6413c3f67f81ffd6d6a32a58 (patch) | |
tree | 732a6d0639aac12f9972650ee5faf5400dc9d568 | |
parent | aa154a5f1e333730aed4869b08bd464a87cd7de3 (diff) | |
download | requests-cache-137e68579331446c6413c3f67f81ffd6d6a32a58.tar.gz |
Add unit test to test revalidation refreshening behavior
-rw-r--r-- | tests/integration/base_cache_test.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/integration/base_cache_test.py b/tests/integration/base_cache_test.py index 9b84a14..e35af90 100644 --- a/tests/integration/base_cache_test.py +++ b/tests/integration/base_cache_test.py @@ -220,6 +220,39 @@ class BaseCacheTest: assert response.from_cache is True assert response.is_expired is True + @pytest.mark.parametrize('validator_headers', [{'ETag': ETAG}, {'Last-Modified': LAST_MODIFIED}]) + @pytest.mark.parametrize('cache_headers', [{'Cache-Control': 'max-age=0'}]) + def test_conditional_request_refreshenes_expire_date(self, cache_headers, validator_headers): + """Test that revalidation attempt with 304 responses causes stale entry to become fresh again considering + Cache-Control header of the 304 response.""" + url = httpbin('response-headers') + first_response_headers = {**cache_headers, **validator_headers} + session = self.init_session(cache_control=True) + + # This endpoint returns request params as response headers + session.get(url, params=first_response_headers) + + # Add different Response Header to mocked return value of the session.send() function. + updated_response_headers = {**first_response_headers, 'Cache-Control': 'max-age=60'} + with patch.object( + Session, 'send', return_value=MagicMock(status_code=304, headers=updated_response_headers) + ): + response = session.get(url, params=first_response_headers) + assert response.from_cache is True + assert response.is_expired is False + + # Make sure an immediate subsequent request will be served from the cache for another max-age==60 secondss + try: + with patch.object(Session, 'send', side_effect=AssertionError): + response = session.get(url, params=first_response_headers) + except AssertionError: + assert False, ( + "Session tried to perform re-validation although cached response should have been " + "refreshened." + ) + assert response.from_cache is True + assert response.is_expired is False + @pytest.mark.parametrize('stream', [True, False]) def test_response_decode(self, stream): """Test that gzip-compressed raw responses (including streamed responses) can be manually |