From 23205c333cb0ddd3ffb5d4456d151f1345a8d9bc Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 2 Dec 2020 16:47:50 +0100 Subject: [PATCH 1/3] requests_wrapper: mind query params when caching (#49175) --- combo/utils/requests_wrapper.py | 3 ++- tests/test_requests.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/combo/utils/requests_wrapper.py b/combo/utils/requests_wrapper.py index d25d6a24..16e8836c 100644 --- a/combo/utils/requests_wrapper.py +++ b/combo/utils/requests_wrapper.py @@ -117,7 +117,8 @@ class Requests(RequestsSession): if method == 'GET' and cache_duration: # handle cache - cache_key = hashlib.md5(smart_bytes(url)).hexdigest() + params = urlencode(kwargs.get('params', {})) + cache_key = hashlib.md5(smart_bytes(url + params)).hexdigest() cache_content = cache.get(cache_key) if cache_content and not invalidate_cache: response = Response() diff --git a/tests/test_requests.py b/tests/test_requests.py index 70522698..d2027772 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -130,13 +130,19 @@ def test_requests_cache(): # now there's something in cache assert requests.get('http://cache.example.org/').content == b'hello world' assert requests_get.call_count == 1 + # passing parameters triggers new request + assert requests.get('http://cache.example.org/', params={'test': 'test'}).content == b'hello world' + assert requests_get.call_count == 2 + # if parameters are the same, cache is used + assert requests.get('http://cache.example.org/', params={'test': 'test'}).content == b'hello world' + assert requests_get.call_count == 2 # value changed requests_get.return_value = mock.Mock(content=b'hello second world', status_code=200) assert requests.get('http://cache.example.org/').content == b'hello world' - assert requests_get.call_count == 1 + assert requests_get.call_count == 2 # force cache invalidation assert requests.get('http://cache.example.org/', invalidate_cache=True).content == b'hello second world' - assert requests_get.call_count == 2 + assert requests_get.call_count == 3 # check raise_if_not_cached with pytest.raises(NothingInCacheException): requests.get('http://cache.example.org/other', raise_if_not_cached=True) -- 2.20.1