Skip to content

Commit

Permalink
Consolidate the HTTP request timeout in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Sep 29, 2022
1 parent e66d1fd commit 58e73cc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 6 additions & 0 deletions cheroot/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions cheroot/test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions cheroot/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 == (
Expand All @@ -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:
Expand Down

0 comments on commit 58e73cc

Please sign in to comment.