add REQUESTS_TIMEOUT settings for utils.Request (#20838)

This commit is contained in:
Thomas NOËL 2017-12-21 01:28:24 +01:00
parent 1d84c51fb1
commit 940b269a51
4 changed files with 32 additions and 0 deletions

View File

@ -179,6 +179,11 @@ MELLON_IDENTITY_PROVIDERS = []
# see http://docs.python-requests.org/en/latest/user/advanced/#proxies
REQUESTS_PROXIES = None
# timeout used in python-requests call, in seconds
# we use 25s by default: timeout just before web server, which is usually 30s,
# and before clients, which usually use 28s (see w.c.s. or Combo)
REQUESTS_TIMEOUT = 25
# List of passerelle.utils.Request response Content-Type to log
LOGGED_CONTENT_TYPES_MESSAGES = (
r'text/', r'application/(json|xml)'

View File

@ -211,6 +211,9 @@ class Request(RequestSession):
if settings.REQUESTS_PROXIES and 'proxies' not in kwargs:
kwargs['proxies'] = settings.REQUESTS_PROXIES
if 'timeout' not in kwargs:
kwargs['timeout'] = settings.REQUESTS_TIMEOUT
response = super(Request, self).request(method, url, **kwargs)
if method == 'GET' and cache_duration and (response.status_code // 100 == 2):

View File

@ -218,6 +218,7 @@ def test_resource_certificates(mocked_get, caplog, endpoint_response):
assert mocked_get.call_args[1].get('cert') == '/local.pem'
assert mocked_get.call_args[1].get('verify') is False
@mock.patch('passerelle.utils.RequestSession.request')
def test_requests_cache(mocked_get, caplog):
resource = MockResource()
@ -277,3 +278,25 @@ def test_requests_cache(mocked_get, caplog):
assert mocked_get.call_count == 1
assert request.get('http://cache.example.org/img', cache_duration=15).headers.get('content-type') == 'image/png'
assert mocked_get.call_count == 1 # got a cached response
@mock.patch('passerelle.utils.RequestSession.request')
def test_timeout(mocked_get, caplog, endpoint_response):
mocked_get.return_value = endpoint_response
logger = logging.getLogger('requests')
Request(logger=logger).get('http://example.net/whatever')
assert mocked_get.call_args[1]['timeout'] == 25
Request(logger=logger).get('http://example.net/whatever', timeout=42)
assert mocked_get.call_args[1]['timeout'] == 42
Request(logger=logger).get('http://example.net/whatever', timeout=None)
assert mocked_get.call_args[1]['timeout'] is None
with override_settings(REQUESTS_TIMEOUT=57):
Request(logger=logger).get('http://example.net/whatever')
assert mocked_get.call_args[1]['timeout'] == 57
Request(logger=logger).get('http://example.net/whatever', timeout=42)
assert mocked_get.call_args[1]['timeout'] == 42
Request(logger=logger).get('http://example.net/whatever', timeout=None)
assert mocked_get.call_args[1]['timeout'] is None

View File

@ -87,6 +87,7 @@ def test_solis_ping(app, solis, ping_response):
assert requests_get.call_args[1]['verify'] is True
assert 'cert' not in requests_get.call_args[1]
assert 'proxies' not in requests_get.call_args[1]
assert requests_get.call_args[1]['timeout'] == 25
# try certificates parameters
solis.verify_cert = False