Skip to content
Myr

Merge pull request #3943 from leo-b/timeout

Before adoption: this commit came in with the history the project adopted, so no landing records it.

HTTPAdapter.send() timeout defaults

authored by Cory Benfield · committed by GitHub · parents 1be8e19, 1be6a17 · Browse files · 2 files+32 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+32 added−9 removed

  • tests:+29 added−8 removed
  • other:+3 added−1 removed

2 files+32 added−9 removed

modified:requests/adapters.py+3 added-1 removed
389 unmodified lines
390 :param stream: (optional) Whether to stream the request content.391 :param timeout: (optional) How long to wait for the server to send392 data before giving up, as a float, or a :ref:`(connect timeout,393 read timeout) <timeouts>` tuple.394 :type timeout: float or tuple394 :type timeout: float or tuple or urllib3 Timeout object395 :param verify: (optional) Either a boolean, in which case it controls whether396 we verify the server's TLS certificate, or a string, in which case it397 must be a path to a CA bundle to use398 :param cert: (optional) Any user-provided SSL certificate to be trusted.
18 unmodified lines
417 err = ("Invalid timeout {0}. Pass a (connect, read) "418 "timeout tuple, or a single float to set "419 "both timeouts to the same value".format(timeout))420 raise ValueError(err)421 elif isinstance(timeout, TimeoutSauce):422 pass423 else:424 timeout = TimeoutSauce(connect=timeout, read=timeout)425426 try:
92 unmodified lines
modified:tests/test_requests.py+29 added-8 removed
31 unmodified lines
32from requests.hooks import default_hooks3334from .compat import StringIO, u35from .utils import override_environ36from requests.packages.urllib3.util import Timeout as Urllib3Timeout3738# Requests to this URL should always fail with a connection timeout (nothing39# listening on that port)40TARPIT = 'http://10.255.255.1'
1972 unmodified lines
2013 with pytest.raises(ValueError) as e:2014 requests.get(httpbin('get'), timeout=timeout)2015 assert error_text in str(e)20162017 @pytest.mark.parametrize(2018 'timeout', (2019 None,2020 Urllib3Timeout(connect=None, read=None)2021 ))2016 def test_none_timeout(self, httpbin):2022 def test_none_timeout(self, httpbin, timeout):2023 """Check that you can set None as a valid timeout value.20242025 To actually test this behavior, we'd want to check that setting the2026 timeout to None actually lets the request block past the system default2027 timeout. However, this would make the test suite unbearably slow.2028 Instead we verify that setting the timeout to None does not prevent the2029 request from succeeding.2030 """2025 r = requests.get(httpbin('get'), timeout=None)2031 r = requests.get(httpbin('get'), timeout=timeout)2032 assert r.status_code == 20020332034 @pytest.mark.parametrize(2035 'timeout', (2036 (None, 0.1),2037 Urllib3Timeout(connect=None, read=0.1)2038 ))2028 def test_read_timeout(self, httpbin):2039 def test_read_timeout(self, httpbin, timeout):2040 try:2030 requests.get(httpbin('delay/10'), timeout=(None, 0.1))2041 requests.get(httpbin('delay/10'), timeout=timeout)2042 pytest.fail('The recv() request should time out.')2043 except ReadTimeout:2044 pass20452046 @pytest.mark.parametrize(2047 'timeout', (2048 (0.1, None),2049 Urllib3Timeout(connect=0.1, read=None)2050 ))2035 def test_connect_timeout(self):2051 def test_connect_timeout(self, timeout):2052 try:2037 requests.get(TARPIT, timeout=(0.1, None))2053 requests.get(TARPIT, timeout=timeout)2054 pytest.fail('The connect() request should time out.')2055 except ConnectTimeout as e:2056 assert isinstance(e, ConnectionError)2057 assert isinstance(e, Timeout)20582059 @pytest.mark.parametrize(2060 'timeout', (2061 (0.1, 0.1),2062 Urllib3Timeout(connect=0.1, read=0.1)2063 ))2043 def test_total_timeout_connect(self):2064 def test_total_timeout_connect(self, timeout):2065 try:2045 requests.get(TARPIT, timeout=(0.1, 0.1))2066 requests.get(TARPIT, timeout=timeout)2067 pytest.fail('The connect() request should time out.')2068 except ConnectTimeout:2069 pass2070
305 unmodified lines