From 58e73cc8418ec90a9f08ad863620029b8246426f Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Thu, 29 Sep 2022 18:54:23 +0200 Subject: [PATCH] Consolidate the HTTP request timeout in tests --- cheroot/test/conftest.py | 6 ++++++ cheroot/test/test_server.py | 13 ++++++++----- cheroot/test/test_ssl.py | 14 +++++++++----- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cheroot/test/conftest.py b/cheroot/test/conftest.py index e12f8a73c4..f7ecdc8122 100644 --- a/cheroot/test/conftest.py +++ b/cheroot/test/conftest.py @@ -19,6 +19,12 @@ from ..testing import get_server_client +@pytest.fixture +def http_request_timeout(): + """Return a common HTTP request timeout for tests with queries.""" + return 0.1 + + @pytest.fixture # pylint: disable=redefined-outer-name def wsgi_server_client(wsgi_server): # noqa: F811 diff --git a/cheroot/test/test_server.py b/cheroot/test/test_server.py index b6b0b009a0..8babf88542 100644 --- a/cheroot/test/test_server.py +++ b/cheroot/test/test_server.py @@ -259,7 +259,7 @@ def peercreds_enabled_server(http_server, unix_sock_file): @unix_only_sock_test @non_macos_sock_test -def test_peercreds_unix_sock(peercreds_enabled_server): +def test_peercreds_unix_sock(http_request_timeout, peercreds_enabled_server): """Check that ``PEERCRED`` lookup works when enabled.""" httpserver = peercreds_enabled_server bind_addr = httpserver.bind_addr @@ -277,14 +277,14 @@ def test_peercreds_unix_sock(peercreds_enabled_server): with requests_unixsocket.monkeypatch(): peercreds_resp = requests.get( unix_base_uri + PEERCRED_IDS_URI, - timeout=0.1, + timeout=http_request_timeout, ) peercreds_resp.raise_for_status() assert peercreds_resp.text == expected_peercreds peercreds_text_resp = requests.get( unix_base_uri + PEERCRED_TEXTS_URI, - timeout=0.1, + timeout=http_request_timeout, ) assert peercreds_text_resp.status_code == 500 @@ -296,7 +296,10 @@ def test_peercreds_unix_sock(peercreds_enabled_server): ) @unix_only_sock_test @non_macos_sock_test -def test_peercreds_unix_sock_with_lookup(peercreds_enabled_server): +def test_peercreds_unix_sock_with_lookup( + http_request_timeout, + peercreds_enabled_server, +): """Check that ``PEERCRED`` resolution works when enabled.""" httpserver = peercreds_enabled_server httpserver.peercreds_resolve_enabled = True @@ -320,7 +323,7 @@ def test_peercreds_unix_sock_with_lookup(peercreds_enabled_server): with requests_unixsocket.monkeypatch(): peercreds_text_resp = requests.get( unix_base_uri + PEERCRED_TEXTS_URI, - timeout=0.1, + timeout=http_request_timeout, ) peercreds_text_resp.raise_for_status() assert peercreds_text_resp.text == expected_textcreds diff --git a/cheroot/test/test_ssl.py b/cheroot/test/test_ssl.py index b8b033dac3..13e3b1a52f 100644 --- a/cheroot/test/test_ssl.py +++ b/cheroot/test/test_ssl.py @@ -213,6 +213,7 @@ def thread_exceptions(): ), ) def test_ssl_adapters( + http_request_timeout, tls_http_server, adapter_type, tls_certificate, tls_certificate_chain_pem_path, @@ -241,7 +242,7 @@ def test_ssl_adapters( resp = requests.get( 'https://{host!s}:{port!s}/'.format(host=interface, port=port), - timeout=0.1, + timeout=http_request_timeout, verify=tls_ca_certificate_pem_path, ) @@ -279,6 +280,7 @@ def test_ssl_adapters( ) def test_tls_client_auth( # noqa: C901, WPS213 # FIXME # FIXME: remove twisted logic, separate tests + http_request_timeout, mocker, tls_http_server, adapter_type, ca, @@ -333,7 +335,7 @@ def test_tls_client_auth( # noqa: C901, WPS213 # FIXME 'https://{host!s}:{port!s}/'.format(host=interface, port=port), # Don't wait for the first byte forever: - timeout=0.1, + timeout=http_request_timeout, # Server TLS certificate verification: verify=tls_ca_certificate_pem_path, @@ -497,6 +499,7 @@ def test_ssl_env( # noqa: C901 # FIXME thread_exceptions, recwarn, mocker, + http_request_timeout, tls_http_server, adapter_type, ca, tls_verify_mode, tls_certificate, tls_certificate_chain_pem_path, @@ -537,7 +540,7 @@ def test_ssl_env( # noqa: C901 # FIXME resp = requests.get( 'https://' + interface + ':' + str(port) + '/env', - timeout=0.1, + timeout=http_request_timeout, verify=tls_ca_certificate_pem_path, cert=cl_pem if use_client_cert else None, ) @@ -665,6 +668,7 @@ def test_https_over_http_error(http_server, ip_addr): ) @pytest.mark.flaky(reruns=3, reruns_delay=2) def test_http_over_https_error( + http_request_timeout, tls_http_server, adapter_type, ca, ip_addr, tls_certificate, @@ -734,7 +738,7 @@ def test_http_over_https_error( if expect_fallback_response_over_plain_http: resp = requests.get( 'http://{host!s}:{port!s}/'.format(host=fqdn, port=port), - timeout=0.1, + timeout=http_request_timeout, ) assert resp.status_code == 400 assert resp.text == ( @@ -746,7 +750,7 @@ def test_http_over_https_error( with pytest.raises(requests.exceptions.ConnectionError) as ssl_err: requests.get( # FIXME: make stdlib ssl behave like PyOpenSSL 'http://{host!s}:{port!s}/'.format(host=fqdn, port=port), - timeout=0.1, + timeout=http_request_timeout, ) if IS_LINUX: