summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2022-06-16 15:09:04 -0500
committerJordan Cook <jordan.cook@pioneer.com>2022-06-16 16:25:58 -0500
commitb92f7645fea16c34be6ae04a1a663f8e9d56e9d9 (patch)
tree8da4886ad3e1483ff1ad0000360b67c7a592448e /tests
parent20dcc26d7d49d2ec8ee9571161a2722bb09fed25 (diff)
downloadrequests-cache-b92f7645fea16c34be6ae04a1a663f8e9d56e9d9.tar.gz
Some additional logging and tests
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py8
-rw-r--r--tests/unit/test_session.py16
2 files changed, 24 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index edf9f28..e09d7f1 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -72,6 +72,7 @@ MOCKED_URL_HTTPS = 'https+mock://requests-cache.com/text'
MOCKED_URL_JSON = 'http+mock://requests-cache.com/json'
MOCKED_URL_REDIRECT = 'http+mock://requests-cache.com/redirect'
MOCKED_URL_REDIRECT_TARGET = 'http+mock://requests-cache.com/redirect_target'
+MOCKED_URL_VARY = 'http+mock://requests-cache.com/vary'
MOCKED_URL_404 = 'http+mock://requests-cache.com/nonexistent'
MOCKED_URL_500 = 'http+mock://requests-cache.com/answer?q=this-statement-is-false'
MOCK_PROTOCOLS = ['mock://', 'http+mock://', 'https+mock://']
@@ -206,6 +207,13 @@ def get_mock_adapter() -> Adapter:
text='mock redirected response',
status_code=200,
)
+ adapter.register_uri(
+ ANY_METHOD,
+ MOCKED_URL_VARY,
+ headers={'Content-Type': 'text/plain', 'Vary': 'Accept'},
+ text='mock response with Vary header',
+ status_code=200,
+ )
adapter.register_uri(ANY_METHOD, MOCKED_URL_404, status_code=404)
adapter.register_uri(ANY_METHOD, MOCKED_URL_500, status_code=500)
return adapter
diff --git a/tests/unit/test_session.py b/tests/unit/test_session.py
index 9a2e0dc..b2ed786 100644
--- a/tests/unit/test_session.py
+++ b/tests/unit/test_session.py
@@ -28,6 +28,7 @@ from tests.conftest import (
MOCKED_URL_JSON,
MOCKED_URL_REDIRECT,
MOCKED_URL_REDIRECT_TARGET,
+ MOCKED_URL_VARY,
patch_normalize_url,
)
@@ -306,6 +307,21 @@ def test_match_headers__list(mock_session):
assert mock_session.get(MOCKED_URL, headers=headers_3).from_cache is False
+def test_match_headers__vary(mock_session):
+ """Vary should be used to validate headers, if available.
+ It should also override `match_headers` for the secondary cache key, if both are provided.
+ """
+ # mock_session.settings.match_headers = ['Accept-Encoding']
+ headers_1 = {'Accept': 'application/json', 'User-Agent': 'qutebrowser'}
+ headers_2 = {'Accept': 'application/json', 'User-Agent': 'Firefox'}
+ headers_3 = {'Accept': 'text/plain', 'User-Agent': 'qutebrowser'}
+
+ assert mock_session.get(MOCKED_URL_VARY, headers=headers_1).from_cache is False
+ assert mock_session.get(MOCKED_URL_VARY, headers=headers_1).from_cache is True
+ assert mock_session.get(MOCKED_URL_VARY, headers=headers_2).from_cache is True
+ assert mock_session.get(MOCKED_URL_VARY, headers=headers_3).from_cache is False
+
+
def test_include_get_headers():
"""include_get_headers is aliased to match_headers for backwards-compatibility"""
session = CachedSession(include_get_headers=True, backend='memory')