Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 3 additions & 30 deletions tests/async_tests/test_interfaces.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import platform
import sys

import pytest

Expand Down Expand Up @@ -469,39 +468,13 @@ async def test_cannot_connect_uds(backend: str) -> None:
await http.arequest(method, url)


@pytest.mark.skipif(
sys.version_info[:2] < (3, 7),
reason="Hypercorn doesn't support python < 3.7 (this test is local-only)",
)
@pytest.mark.anyio
async def test_connection_timeout_tcp(backend: str, server: Server) -> None:
# we try to access http server using https. It caused some SSL timeouts
# in TLSStream.wrap inside inside AnyIOBackend.open_tcp_stream
async def test_connection_timeout_tcp(backend: str, busy_server: Server) -> None:
method = b"GET"
url = (b"https", *server.netloc, b"/")
headers = [server.host_header]
ext = {"timeout": {"connect": 0.1}}

async with httpcore.AsyncConnectionPool(backend=backend) as http:
with pytest.raises(httpcore.ConnectTimeout):
await http.arequest(method, url, headers, ext=ext)


@pytest.mark.skipif(
sys.version_info[:2] < (3, 7),
reason="Hypercorn doesn't support python < 3.7 (this test is local-only)",
)
@pytest.mark.anyio
async def test_connection_timeout_uds(
backend: str, uds_server: Server, uds: str
) -> None:
# we try to access http server using https. It caused some SSL timeouts
# in TLSStream.wrap inside AnyIOBackend.open_uds_stream
method = b"GET"
url = (b"https", b"localhost", None, b"/")
url = (b"http", *busy_server.netloc, b"/")
headers = [(b"host", b"localhost")]
ext = {"timeout": {"connect": 0.1}}

async with httpcore.AsyncConnectionPool(uds=uds, backend=backend) as http:
async with httpcore.AsyncConnectionPool(backend=backend) as http:
with pytest.raises(httpcore.ConnectTimeout):
await http.arequest(method, url, headers, ext=ext)
17 changes: 16 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

from httpcore._types import URL

from .utils import HypercornServer, LiveServer, Server, http_proxy_server
from .utils import (
HypercornServer,
LiveServer,
NotListeningServer,
Server,
http_proxy_server,
)

try:
import hypercorn
Expand All @@ -25,6 +31,8 @@
SERVER_HTTPS_PORT = 8003
HTTPS_SERVER_URL = f"https://localhost:{SERVER_HTTPS_PORT}"

SERVER_BUSY_PORT = 8004


@pytest.fixture(scope="session")
def proxy_server() -> typing.Iterator[URL]:
Expand Down Expand Up @@ -161,6 +169,13 @@ def https_server(
yield server


@pytest.fixture
def busy_server() -> typing.Iterator[Server]:
server = NotListeningServer(("localhost", SERVER_BUSY_PORT))
with server.serve():
yield server


@pytest.fixture(scope="function")
def too_many_open_files_minus_one() -> typing.Iterator[None]:
# Fixture for test regression on https://github.com/encode/httpcore/issues/182
Expand Down
33 changes: 3 additions & 30 deletions tests/sync_tests/test_interfaces.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import platform
import sys

import pytest

Expand Down Expand Up @@ -469,39 +468,13 @@ def test_cannot_connect_uds(backend: str) -> None:
http.request(method, url)


@pytest.mark.skipif(
sys.version_info[:2] < (3, 7),
reason="Hypercorn doesn't support python < 3.7 (this test is local-only)",
)

def test_connection_timeout_tcp(backend: str, server: Server) -> None:
# we try to access http server using https. It caused some SSL timeouts
# in TLSStream.wrap inside inside AnyIOBackend.open_tcp_stream
def test_connection_timeout_tcp(backend: str, busy_server: Server) -> None:
method = b"GET"
url = (b"https", *server.netloc, b"/")
headers = [server.host_header]
ext = {"timeout": {"connect": 0.1}}

with httpcore.SyncConnectionPool(backend=backend) as http:
with pytest.raises(httpcore.ConnectTimeout):
http.request(method, url, headers, ext=ext)


@pytest.mark.skipif(
sys.version_info[:2] < (3, 7),
reason="Hypercorn doesn't support python < 3.7 (this test is local-only)",
)

def test_connection_timeout_uds(
backend: str, uds_server: Server, uds: str
) -> None:
# we try to access http server using https. It caused some SSL timeouts
# in TLSStream.wrap inside AnyIOBackend.open_uds_stream
method = b"GET"
url = (b"https", b"localhost", None, b"/")
url = (b"http", *busy_server.netloc, b"/")
headers = [(b"host", b"localhost")]
ext = {"timeout": {"connect": 0.1}}

with httpcore.SyncConnectionPool(uds=uds, backend=backend) as http:
with httpcore.SyncConnectionPool(backend=backend) as http:
with pytest.raises(httpcore.ConnectTimeout):
http.request(method, url, headers, ext=ext)
21 changes: 21 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,27 @@ def serve_in_thread(self) -> Iterator[None]:
thread.join()


class NotListeningServer(Server):
"""
A test server that binds to a TCP port but never listens for or accepts incoming
connections. For testing connection timeouts.
"""

def __init__(self, address: Tuple[str, int]) -> None:
self._address = address

@property
def netloc(self) -> Tuple[bytes, int]:
host, port = self._address
return (host.encode("ascii"), port)

@contextlib.contextmanager
def serve(self) -> Iterator[None]:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(self._address)
yield


@contextlib.contextmanager
def http_proxy_server(proxy_host: str, proxy_port: int):
"""
Expand Down