requests_wrapper: mind query params when caching (#49175)

This commit is contained in:
Valentin Deniaud 2020-12-02 16:47:50 +01:00
parent d8fbda7d42
commit 249c133d07
2 changed files with 10 additions and 3 deletions

View File

@ -118,7 +118,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()

View File

@ -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)