utils: add invalidate_cache to force a new request to be made (#17056)
This commit is contained in:
parent
1f0b71edc6
commit
934f310fb2
|
@ -84,6 +84,7 @@ class Requests(RequestsSession):
|
|||
def request(self, method, url, **kwargs):
|
||||
remote_service = kwargs.pop('remote_service', None)
|
||||
cache_duration = kwargs.pop('cache_duration', 15)
|
||||
invalidate_cache = kwargs.pop('invalidate_cache', False)
|
||||
user = kwargs.pop('user', None)
|
||||
without_user = kwargs.pop('without_user', False)
|
||||
federation_key = kwargs.pop('federation_key', 'auto') # 'auto', 'email', 'nameid'
|
||||
|
@ -150,7 +151,7 @@ class Requests(RequestsSession):
|
|||
# handle cache
|
||||
cache_key = hashlib.md5(url).hexdigest()
|
||||
cache_content = cache.get(cache_key)
|
||||
if cache_content:
|
||||
if cache_content and not invalidate_cache:
|
||||
response = Response()
|
||||
response.status_code = 200
|
||||
response.raw = StringIO(cache_content)
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import mock
|
||||
import pytest
|
||||
import urlparse
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
|
||||
from combo.utils import requests, check_query
|
||||
from combo.utils import requests, check_query, NothingInCacheException
|
||||
|
||||
class MockSAMLUser(object):
|
||||
name_id = 'r2d2'
|
||||
|
@ -122,3 +123,23 @@ def test_sign_anonymous_user():
|
|||
assert query['email'][0] == ''
|
||||
assert query['orig'][0] == 'myself'
|
||||
assert check_query(querystring, 'secret') == True
|
||||
|
||||
def test_requests_cache():
|
||||
with mock.patch('combo.utils.RequestsSession.request') as requests_get:
|
||||
requests_get.return_value = mock.Mock(content='hello world', status_code=200)
|
||||
# default cache, nothing in there
|
||||
assert requests.get('http://cache.example.org/').content == 'hello world'
|
||||
assert requests_get.call_count == 1
|
||||
# now there's something in cache
|
||||
assert requests.get('http://cache.example.org/').content == 'hello world'
|
||||
assert requests_get.call_count == 1
|
||||
# value changed
|
||||
requests_get.return_value = mock.Mock(content='hello second world', status_code=200)
|
||||
assert requests.get('http://cache.example.org/').content == 'hello world'
|
||||
assert requests_get.call_count == 1
|
||||
# force cache invalidation
|
||||
assert requests.get('http://cache.example.org/', invalidate_cache=True).content == 'hello second world'
|
||||
assert requests_get.call_count == 2
|
||||
# check raise_if_not_cached
|
||||
with pytest.raises(NothingInCacheException):
|
||||
requests.get('http://cache.example.org/other', raise_if_not_cached=True)
|
||||
|
|
Loading…
Reference in New Issue