From 20dcc26d7d49d2ec8ee9571161a2722bb09fed25 Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Sat, 21 May 2022 12:54:56 -0500 Subject: Add support for Vary --- tests/unit/policy/test_actions.py | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'tests') diff --git a/tests/unit/policy/test_actions.py b/tests/unit/policy/test_actions.py index 317ecc8..f5d4c4a 100644 --- a/tests/unit/policy/test_actions.py +++ b/tests/unit/policy/test_actions.py @@ -4,6 +4,7 @@ from unittest.mock import patch import pytest from requests import PreparedRequest, Request +from requests_cache.cache_keys import create_key from requests_cache.models import CachedResponse from requests_cache.policy.actions import EXPIRE_IMMEDIATELY, CacheActions from requests_cache.policy.settings import CacheSettings @@ -235,6 +236,85 @@ def test_update_from_cached_response__stale_while_revalidate(): assert actions.resend_async is True +@pytest.mark.parametrize( + 'vary, cached_headers, new_headers, expected_match', + [ + ({}, {}, {}, True), + ({'Vary': 'Accept'}, {'Accept': 'application/json'}, {'Accept': 'application/json'}, True), + ({'Vary': 'Accept'}, {'Accept': 'application/json'}, {}, False), + ( + {'Vary': 'Accept'}, + {'Accept': 'application/json'}, + {'Accept': 'application/json', 'Accept-Language': 'en'}, + True, + ), + ( + {'Vary': 'Accept-Encoding'}, + {'Accept': 'application/json'}, + {'Accept': 'text/html'}, + True, + ), + ({'Vary': 'Accept'}, {'Accept': 'application/json'}, {'Accept': 'text/html'}, False), + ( + {'Vary': 'Accept-Encoding'}, + {'Accept-Encoding': 'gzip,deflate'}, + {'Accept-Encoding': 'gzip,deflate'}, + True, + ), + # Only basic header normalization is done in create_key() (whitespace, case, order) + ( + {'Vary': 'Accept-Encoding'}, + {'Accept-Encoding': 'gzip,deflate'}, + {'Accept-Encoding': 'dEfLaTe, GZIP, '}, + True, + ), + ( + {'Vary': 'Accept-Encoding'}, + {'Accept-Encoding': 'gzip,deflate'}, + {'Accept-Encoding': 'gzip,br'}, + False, + ), + ( + {'Vary': 'Accept, Accept-Encoding'}, + {'Accept': 'application/json', 'Accept-Encoding': 'gzip,deflate'}, + {'Accept': 'application/json', 'Accept-Encoding': 'gzip,deflate'}, + True, + ), + ( + {'Vary': 'Accept, Accept-Encoding'}, + {'Accept': 'application/json', 'Accept-Encoding': 'gzip,deflate'}, + {'Accept': 'application/json', 'Accept-Encoding': 'br'}, + False, + ), + ( + {'Vary': 'Accept, Accept-Encoding'}, + {'Accept': 'application/json', 'Accept-Encoding': 'gzip,deflate'}, + {'Accept': 'text/html', 'Accept-Encoding': 'gzip,deflate'}, + False, + ), + ( + {'Vary': 'Accept, Accept-Encoding'}, + {'Accept': 'application/json', 'Accept-Encoding': 'gzip,deflate'}, + {'Accept-Encoding': 'gzip,deflate'}, + False, + ), + ({'Vary': '*'}, {}, {}, False), + ({'Vary': '*'}, {'Accept': 'application/json'}, {'Accept': 'application/json'}, False), + ], +) +def test_update_from_cached_response__vary(vary, cached_headers, new_headers, expected_match): + cached_response = CachedResponse( + headers=vary, + request=Request(method='GET', url='https://site.com/img.jpg', headers=cached_headers), + ) + request = Request(method='GET', url='https://site.com/img.jpg', headers=new_headers) + actions = CacheActions.from_request('key', request) + actions.update_from_cached_response(cached_response, create_key=create_key) + + # If the headers don't match wrt. Vary, expect a new request to be sent (cache miss) + assert actions.send_request is not expected_match + + @pytest.mark.parametrize('max_stale, usable', [(5, False), (15, True)]) def test_is_usable__max_stale(max_stale, usable): """For a response that expired 10 seconds ago, it may be either accepted or rejected based on -- cgit v1.2.1