From 25c652a4389241844a80540932fcd5fd173e8b16 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 22 Apr 2019 15:45:56 +0100 Subject: [PATCH 1/5] Push SSL context loading into config.py --- httpcore/config.py | 53 ++++++++++++++++++++++++++++++++++++++ httpcore/pool.py | 63 ++++------------------------------------------ 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/httpcore/config.py b/httpcore/config.py index e2a18b4e..f694fc9a 100644 --- a/httpcore/config.py +++ b/httpcore/config.py @@ -1,3 +1,4 @@ +import ssl import typing import certifi @@ -32,6 +33,58 @@ class SSLConfig: class_name = self.__class__.__name__ return f"{class_name}(cert={self.cert}, verify={self.verify})" + async def load_ssl_context(self) -> ssl.SSLContext: + if not hasattr(self, "ssl_context"): + if not self.verify: + self.ssl_context = self.load_ssl_context_no_verify() + else: + # Run the SSL loading in a threadpool, since it makes disk accesses. + loop = asyncio.get_event_loop() + self.ssl_context = await loop.run_in_executor( + None, self.load_ssl_context_verify + ) + + return self.ssl_context + + def load_ssl_context_no_verify(self) -> ssl.SSLContext: + """ + Return an SSL context for unverified connections. + """ + context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) + context.options |= ssl.OP_NO_SSLv2 + context.options |= ssl.OP_NO_SSLv3 + context.options |= ssl.OP_NO_COMPRESSION + context.set_default_verify_paths() + return context + + def load_ssl_context_verify(self) -> ssl.SSLContext: + """ + Return an SSL context for verified connections. + """ + if isinstance(self.verify, bool): + ca_bundle_path = DEFAULT_CA_BUNDLE_PATH + elif os.path.exists(self.verify): + ca_bundle_path = self.verify + else: + raise IOError( + "Could not find a suitable TLS CA certificate bundle, " + "invalid path: {}".format(self.verify) + ) + + context = ssl.create_default_context() + if os.path.isfile(ca_bundle_path): + context.load_verify_locations(cafile=ca_bundle_path) + elif os.path.isdir(ca_bundle_path): + context.load_verify_locations(capath=ca_bundle_path) + + if self.cert is not None: + if isinstance(self.cert, str): + context.load_cert_chain(certfile=self.cert) + else: + context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1]) + + return context + class TimeoutConfig: """ diff --git a/httpcore/pool.py b/httpcore/pool.py index 74c19490..f09e01e0 100644 --- a/httpcore/pool.py +++ b/httpcore/pool.py @@ -99,7 +99,11 @@ class ConnectionPool: self.num_active_connections += 1 except (KeyError, IndexError): - ssl_context = await self.get_ssl_context(url, ssl) + if url.is_secure: + ssl_context = await ssl.load_ssl_context() + else: + ssl_context = None + try: await asyncio.wait_for( self._max_connections.acquire(), timeout.pool_timeout @@ -134,63 +138,6 @@ class ConnectionPool: except KeyError: self._keepalive_connections[key] = [connection] - async def get_ssl_context( - self, url: URL, config: SSLConfig - ) -> typing.Optional[ssl.SSLContext]: - if not url.is_secure: - return None - - if not hasattr(self, "ssl_context"): - if not config.verify: - self.ssl_context = self.get_ssl_context_no_verify() - else: - # Run the SSL loading in a threadpool, since it makes disk accesses. - loop = asyncio.get_event_loop() - self.ssl_context = await loop.run_in_executor( - None, self.get_ssl_context_verify - ) - - return self.ssl_context - - def get_ssl_context_no_verify(self) -> ssl.SSLContext: - """ - Return an SSL context for unverified connections. - """ - context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) - context.options |= ssl.OP_NO_SSLv2 - context.options |= ssl.OP_NO_SSLv3 - context.options |= ssl.OP_NO_COMPRESSION - context.set_default_verify_paths() - return context - - def get_ssl_context_verify(self, config: SSLConfig) -> ssl.SSLContext: - """ - Return an SSL context for verified connections. - """ - if isinstance(config.verify, bool): - ca_bundle_path = DEFAULT_CA_BUNDLE_PATH - elif os.path.exists(config.verify): - ca_bundle_path = config.verify - else: - raise IOError( - "Could not find a suitable TLS CA certificate bundle, " - "invalid path: {}".format(config.verify) - ) - - context = ssl.create_default_context() - if os.path.isfile(ca_bundle_path): - context.load_verify_locations(cafile=ca_bundle_path) - elif os.path.isdir(ca_bundle_path): - context.load_verify_locations(capath=ca_bundle_path) - - if config.cert is not None: - if isinstance(config.cert, str): - context.load_cert_chain(certfile=config.cert) - else: - context.load_cert_chain(certfile=config.cert[0], keyfile=config.cert[1]) - - return context - async def close(self) -> None: self.is_closed = True From be774980cea4cadb205a53a6bda3a2d40bfd9086 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 22 Apr 2019 17:33:43 +0100 Subject: [PATCH 2/5] Refactor to request = Client.send(request) --- httpcore/__init__.py | 3 +- httpcore/config.py | 2 + httpcore/connections.py | 136 ++++++++++++++++++++++++------------- httpcore/datastructures.py | 36 ++++++++++ httpcore/pool.py | 77 ++++++++------------- httpcore/sync.py | 0 tests/test_api.py | 22 +++--- tests/test_connections.py | 22 ++++++ tests/test_pool.py | 112 ++++++++++++++++-------------- tests/test_responses.py | 67 +++--------------- tests/test_timeouts.py | 18 +++-- 11 files changed, 273 insertions(+), 222 deletions(-) create mode 100644 httpcore/sync.py create mode 100644 tests/test_connections.py diff --git a/httpcore/__init__.py b/httpcore/__init__.py index d4f6465f..32ff66d6 100644 --- a/httpcore/__init__.py +++ b/httpcore/__init__.py @@ -1,5 +1,6 @@ from .config import PoolLimits, SSLConfig, TimeoutConfig -from .datastructures import URL, Request, Response +from .connections import Connection +from .datastructures import URL, Origin, Request, Response from .exceptions import ( BadResponse, ConnectTimeout, diff --git a/httpcore/config.py b/httpcore/config.py index f694fc9a..8cc784b3 100644 --- a/httpcore/config.py +++ b/httpcore/config.py @@ -1,3 +1,5 @@ +import asyncio +import os import ssl import typing diff --git a/httpcore/connections.py b/httpcore/connections.py index 16f91408..dadcfd90 100644 --- a/httpcore/connections.py +++ b/httpcore/connections.py @@ -1,11 +1,10 @@ import asyncio -import ssl import typing import h11 -from .config import TimeoutConfig -from .datastructures import Request, Response +from .config import DEFAULT_SSL_CONFIG, DEFAULT_TIMEOUT_CONFIG, SSLConfig, TimeoutConfig +from .datastructures import Client, Origin, Request, Response from .exceptions import ConnectTimeout, ReadTimeout H11Event = typing.Union[ @@ -19,34 +18,47 @@ H11Event = typing.Union[ class Connection: - def __init__(self, timeout: TimeoutConfig, on_release: typing.Callable = None): - self.reader = None - self.writer = None - self.state = h11.Connection(our_role=h11.CLIENT) + def __init__( + self, + origin: typing.Union[str, Origin], + ssl: SSLConfig = DEFAULT_SSL_CONFIG, + timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG, + on_release: typing.Callable = None, + ): + self.origin = Origin(origin) if isinstance(origin, str) else origin + self.ssl = ssl self.timeout = timeout self.on_release = on_release + self._reader = None + self._writer = None + self._h11_state = h11.Connection(our_role=h11.CLIENT) @property def is_closed(self) -> bool: - return self.state.our_state in (h11.CLOSED, h11.ERROR) + return self._h11_state.our_state in (h11.CLOSED, h11.ERROR) - async def open( - self, hostname: str, port: int, *, ssl: typing.Optional[ssl.SSLContext] = None - ) -> None: - try: - self.reader, self.writer = await asyncio.wait_for( # type: ignore - asyncio.open_connection(hostname, port, ssl=ssl), - self.timeout.connect_timeout, - ) - except asyncio.TimeoutError: - raise ConnectTimeout() + async def send( + self, + request: Request, + ssl: typing.Optional[SSLConfig] = None, + timeout: typing.Optional[TimeoutConfig] = None, + stream: bool = False, + ) -> Response: + assert request.url.origin == self.origin - async def send(self, request: Request) -> Response: + if ssl is None: + ssl = self.ssl + if timeout is None: + timeout = self.timeout + + # Make the connection + if self._reader is None: + await self._connect(ssl, timeout) + + #  Start sending the request. method = request.method.encode() target = request.url.target headers = request.headers - - #  Start sending the request. event = h11.Request(method=method, target=target, headers=headers) await self._send_event(event) @@ -64,69 +76,97 @@ class Connection: await self._send_event(event) # Start getting the response. - event = await self._receive_event() + event = await self._receive_event(timeout) if isinstance(event, h11.InformationalResponse): - event = await self._receive_event() + event = await self._receive_event(timeout) assert isinstance(event, h11.Response) - reason = event.reason.decode('latin1') + reason = event.reason.decode("latin1") status_code = event.status_code headers = event.headers - body = self._body_iter() - return Response( - status_code=status_code, reason=reason, headers=headers, body=body, on_close=self._release + body = self._body_iter(timeout) + response = Response( + status_code=status_code, + reason=reason, + headers=headers, + body=body, + on_close=self._release, ) - async def _body_iter(self) -> typing.AsyncIterator[bytes]: - event = await self._receive_event() + if not stream: + # Read the response body. + try: + await response.read() + finally: + await response.close() + + return response + + async def _connect(self, ssl: SSLConfig, timeout: TimeoutConfig) -> None: + ssl_context = await ssl.load_ssl_context() if self.origin.is_secure else None + + try: + self._reader, self._writer = await asyncio.wait_for( # type: ignore + asyncio.open_connection( + self.origin.hostname, self.origin.port, ssl=ssl_context + ), + timeout.connect_timeout, + ) + except asyncio.TimeoutError: + raise ConnectTimeout() + + async def _body_iter(self, timeout: TimeoutConfig) -> typing.AsyncIterator[bytes]: + event = await self._receive_event(timeout) while isinstance(event, h11.Data): yield event.data - event = await self._receive_event() + event = await self._receive_event(timeout) assert isinstance(event, h11.EndOfMessage) async def _send_event(self, event: H11Event) -> None: - assert self.writer is not None + assert self._writer is not None - data = self.state.send(event) - self.writer.write(data) + data = self._h11_state.send(event) + self._writer.write(data) - async def _receive_event(self) -> H11Event: - assert self.reader is not None + async def _receive_event(self, timeout: TimeoutConfig) -> H11Event: + assert self._reader is not None - event = self.state.next_event() + event = self._h11_state.next_event() while event is h11.NEED_DATA: try: data = await asyncio.wait_for( - self.reader.read(2048), self.timeout.read_timeout + self._reader.read(2048), timeout.read_timeout ) except asyncio.TimeoutError: raise ReadTimeout() - self.state.receive_data(data) - event = self.state.next_event() + self._h11_state.receive_data(data) + event = self._h11_state.next_event() return event async def _release(self) -> None: - assert self.writer is not None + assert self._writer is not None - if self.state.our_state is h11.DONE and self.state.their_state is h11.DONE: - self.state.start_next_cycle() + if ( + self._h11_state.our_state is h11.DONE + and self._h11_state.their_state is h11.DONE + ): + self._h11_state.start_next_cycle() else: - self.close() + await self.close() if self.on_release is not None: await self.on_release(self) - def close(self) -> None: - assert self.writer is not None - + async def close(self) -> None: event = h11.ConnectionClosed() try: # If we're in h11.MUST_CLOSE then we'll end up in h11.CLOSED. - self.state.send(event) + self._h11_state.send(event) except h11.ProtocolError: # If we're in some other state then it's a premature close, # and we'll end up in h11.ERROR. pass - self.writer.close() + if self._writer is not None: + self._writer.close() diff --git a/httpcore/datastructures.py b/httpcore/datastructures.py index 49f85bdc..d6cadc42 100644 --- a/httpcore/datastructures.py +++ b/httpcore/datastructures.py @@ -61,6 +61,34 @@ class URL: def is_secure(self) -> bool: return self.components.scheme == "https" + @property + def origin(self) -> "Origin": + return Origin(self) + + +class Origin: + def __init__(self, url: typing.Union[str, URL]) -> None: + if isinstance(url, str): + url = URL(url) + self.scheme = url.scheme + self.hostname = url.hostname + self.port = url.port + + @property + def is_secure(self) -> bool: + return self.scheme == "https" + + def __eq__(self, other: typing.Any) -> bool: + return ( + isinstance(other, self.__class__) + and self.scheme == other.scheme + and self.hostname == other.hostname + and self.port == other.port + ) + + def __hash__(self) -> int: + return hash((self.scheme, self.hostname, self.port)) + class Request: def __init__( @@ -207,3 +235,11 @@ class Response: self.is_closed = True if self.on_close is not None: await self.on_close() + + +class Client: + async def send(self, request: Request, **options: typing.Any) -> Response: + raise NotImplementedError() # pragma: nocover + + async def close(self) -> None: + raise NotImplementedError() # pragma: nocover diff --git a/httpcore/pool.py b/httpcore/pool.py index f09e01e0..1a846628 100644 --- a/httpcore/pool.py +++ b/httpcore/pool.py @@ -1,7 +1,4 @@ import asyncio -import functools -import os -import ssl import typing from types import TracebackType @@ -15,11 +12,9 @@ from .config import ( TimeoutConfig, ) from .connections import Connection -from .datastructures import URL, Request, Response +from .datastructures import Client, Origin, Request, Response from .exceptions import PoolTimeout -ConnectionKey = typing.Tuple[str, str, int, SSLConfig, TimeoutConfig] - class ConnectionSemaphore: def __init__(self, max_connections: int = None): @@ -43,7 +38,7 @@ class ConnectionPool: timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG, limits: PoolLimits = DEFAULT_POOL_LIMITS, ): - self.ssl_config = ssl + self.ssl = ssl self.timeout = timeout self.limits = limits self.is_closed = False @@ -51,36 +46,22 @@ class ConnectionPool: self.num_keepalive_connections = 0 self._keepalive_connections = ( {} - ) # type: typing.Dict[ConnectionKey, typing.List[Connection]] + ) # type: typing.Dict[Origin, typing.List[Connection]] self._max_connections = ConnectionSemaphore( max_connections=self.limits.hard_limit ) - async def request( + async def send( self, - method: str, - url: str, - *, - headers: typing.Sequence[typing.Tuple[bytes, bytes]] = (), - body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"", - stream: bool = False, + request: Request, ssl: typing.Optional[SSLConfig] = None, timeout: typing.Optional[TimeoutConfig] = None, + stream: bool = False, ) -> Response: - if ssl is None: - ssl = self.ssl_config - if timeout is None: - timeout = self.timeout - - parsed_url = URL(url) - request = Request(method, parsed_url, headers=headers, body=body) - connection = await self.acquire_connection(parsed_url, ssl=ssl, timeout=timeout) - response = await connection.send(request) - if not stream: - try: - await response.read() - finally: - await response.close() + connection = await self.acquire_connection(request.url.origin, timeout=timeout) + response = await connection.send( + request, ssl=ssl, timeout=timeout, stream=stream + ) return response @property @@ -88,38 +69,36 @@ class ConnectionPool: return self.num_active_connections + self.num_keepalive_connections async def acquire_connection( - self, url: URL, ssl: SSLConfig, timeout: TimeoutConfig + self, origin: Origin, timeout: typing.Optional[TimeoutConfig] = None ) -> Connection: - key = (url.scheme, url.hostname, url.port, ssl, timeout) try: - connection = self._keepalive_connections[key].pop() - if not self._keepalive_connections[key]: - del self._keepalive_connections[key] + connection = self._keepalive_connections[origin].pop() + if not self._keepalive_connections[origin]: + del self._keepalive_connections[origin] self.num_keepalive_connections -= 1 self.num_active_connections += 1 except (KeyError, IndexError): - if url.is_secure: - ssl_context = await ssl.load_ssl_context() + if timeout is None: + pool_timeout = self.timeout.pool_timeout else: - ssl_context = None + pool_timeout = timeout.pool_timeout try: - await asyncio.wait_for( - self._max_connections.acquire(), timeout.pool_timeout - ) + await asyncio.wait_for(self._max_connections.acquire(), pool_timeout) except asyncio.TimeoutError: raise PoolTimeout() - release = functools.partial(self.release_connection, key=key) - connection = Connection(timeout=timeout, on_release=release) + connection = Connection( + origin, + ssl=self.ssl, + timeout=self.timeout, + on_release=self.release_connection, + ) self.num_active_connections += 1 - await connection.open(url.hostname, url.port, ssl=ssl_context) return connection - async def release_connection( - self, connection: Connection, key: ConnectionKey - ) -> None: + async def release_connection(self, connection: Connection) -> None: if connection.is_closed: self._max_connections.release() self.num_active_connections -= 1 @@ -129,14 +108,14 @@ class ConnectionPool: ): self._max_connections.release() self.num_active_connections -= 1 - connection.close() + await connection.close() else: self.num_active_connections -= 1 self.num_keepalive_connections += 1 try: - self._keepalive_connections[key].append(connection) + self._keepalive_connections[connection.origin].append(connection) except KeyError: - self._keepalive_connections[key] = [connection] + self._keepalive_connections[connection.origin] = [connection] async def close(self) -> None: self.is_closed = True diff --git a/httpcore/sync.py b/httpcore/sync.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_api.py b/tests/test_api.py index 6b80587d..30199c93 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -5,25 +5,28 @@ import httpcore @pytest.mark.asyncio async def test_get(server): - async with httpcore.ConnectionPool() as http: - response = await http.request("GET", "http://127.0.0.1:8000/") + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request) assert response.status_code == 200 assert response.body == b"Hello, world!" @pytest.mark.asyncio async def test_post(server): - async with httpcore.ConnectionPool() as http: - response = await http.request( + async with httpcore.ConnectionPool() as client: + request = httpcore.Request( "POST", "http://127.0.0.1:8000/", body=b"Hello, world!" ) + response = await client.send(request) assert response.status_code == 200 @pytest.mark.asyncio async def test_stream_response(server): - async with httpcore.ConnectionPool() as http: - response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request, stream=True) assert response.status_code == 200 assert not hasattr(response, "body") body = await response.read() @@ -36,8 +39,7 @@ async def test_stream_request(server): yield b"Hello, " yield b"world!" - async with httpcore.ConnectionPool() as http: - response = await http.request( - "POST", "http://127.0.0.1:8000/", body=hello_world() - ) + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("POST", "http://127.0.0.1:8000/", body=hello_world()) + response = await client.send(request) assert response.status_code == 200 diff --git a/tests/test_connections.py b/tests/test_connections.py new file mode 100644 index 00000000..958c1b52 --- /dev/null +++ b/tests/test_connections.py @@ -0,0 +1,22 @@ +import pytest + +import httpcore + + +@pytest.mark.asyncio +async def test_get(server): + client = httpcore.Connection(origin="http://127.0.0.1:8000/") + request = httpcore.Request(method="GET", url="http://127.0.0.1:8000/") + response = await client.send(request) + assert response.status_code == 200 + assert response.body == b"Hello, world!" + + +@pytest.mark.asyncio +async def test_post(server): + client = httpcore.Connection(origin="http://127.0.0.1:8000/") + request = httpcore.Request( + method="POST", url="http://127.0.0.1:8000/", body=b"Hello, world!" + ) + response = await client.send(request) + assert response.status_code == 200 diff --git a/tests/test_pool.py b/tests/test_pool.py index 77a22157..de25fe65 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -8,14 +8,16 @@ async def test_keepalive_connections(server): """ Connections should default to staying in a keep-alive state. """ - async with httpcore.ConnectionPool() as http: - response = await http.request("GET", "http://127.0.0.1:8000/") - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 1 + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request) + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 1 - response = await http.request("GET", "http://127.0.0.1:8000/") - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 1 + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request) + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 1 @pytest.mark.asyncio @@ -23,14 +25,16 @@ async def test_differing_connection_keys(server): """ Connnections to differing connection keys should result in multiple connections. """ - async with httpcore.ConnectionPool() as http: - response = await http.request("GET", "http://127.0.0.1:8000/") - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 1 + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request) + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 1 - response = await http.request("GET", "http://localhost:8000/") - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 2 + request = httpcore.Request("GET", "http://localhost:8000/") + response = await client.send(request) + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 2 @pytest.mark.asyncio @@ -40,14 +44,16 @@ async def test_soft_limit(server): """ limits = httpcore.PoolLimits(soft_limit=1) - async with httpcore.ConnectionPool(limits=limits) as http: - response = await http.request("GET", "http://127.0.0.1:8000/") - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 1 + async with httpcore.ConnectionPool(limits=limits) as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request) + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 1 - response = await http.request("GET", "http://localhost:8000/") - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 1 + request = httpcore.Request("GET", "http://localhost:8000/") + response = await client.send(request) + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 1 @pytest.mark.asyncio @@ -55,15 +61,16 @@ async def test_streaming_response_holds_connection(server): """ A streaming request should hold the connection open until the response is read. """ - async with httpcore.ConnectionPool() as http: - response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) - assert http.num_active_connections == 1 - assert http.num_keepalive_connections == 0 + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request, stream=True) + assert client.num_active_connections == 1 + assert client.num_keepalive_connections == 0 await response.read() - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 1 + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 1 @pytest.mark.asyncio @@ -71,22 +78,24 @@ async def test_multiple_concurrent_connections(server): """ Multiple conncurrent requests should open multiple conncurrent connections. """ - async with httpcore.ConnectionPool() as http: - response_a = await http.request("GET", "http://127.0.0.1:8000/", stream=True) - assert http.num_active_connections == 1 - assert http.num_keepalive_connections == 0 + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response_a = await client.send(request, stream=True) + assert client.num_active_connections == 1 + assert client.num_keepalive_connections == 0 - response_b = await http.request("GET", "http://127.0.0.1:8000/", stream=True) - assert http.num_active_connections == 2 - assert http.num_keepalive_connections == 0 + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response_b = await client.send(request, stream=True) + assert client.num_active_connections == 2 + assert client.num_keepalive_connections == 0 await response_b.read() - assert http.num_active_connections == 1 - assert http.num_keepalive_connections == 1 + assert client.num_active_connections == 1 + assert client.num_keepalive_connections == 1 await response_a.read() - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 2 + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 2 @pytest.mark.asyncio @@ -95,10 +104,11 @@ async def test_close_connections(server): Using a `Connection: close` header should close the connection. """ headers = [(b"connection", b"close")] - async with httpcore.ConnectionPool() as http: - response = await http.request("GET", "http://127.0.0.1:8000/", headers=headers) - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 0 + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/", headers=headers) + response = await client.send(request) + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 0 @pytest.mark.asyncio @@ -106,12 +116,13 @@ async def test_standard_response_close(server): """ A standard close should keep the connection open. """ - async with httpcore.ConnectionPool() as http: - response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request, stream=True) await response.read() await response.close() - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 1 + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 1 @pytest.mark.asyncio @@ -119,8 +130,9 @@ async def test_premature_response_close(server): """ A premature close should close the connection. """ - async with httpcore.ConnectionPool() as http: - response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) + async with httpcore.ConnectionPool() as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request, stream=True) await response.close() - assert http.num_active_connections == 0 - assert http.num_keepalive_connections == 0 + assert client.num_active_connections == 0 + assert client.num_keepalive_connections == 0 diff --git a/tests/test_responses.py b/tests/test_responses.py index 3efef890..bb930bdb 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -3,26 +3,13 @@ import pytest import httpcore -class MockHTTP(httpcore.ConnectionPool): - async def request( - self, method, url, *, headers=(), body=b"", stream=False - ) -> httpcore.Response: - if stream: - - async def streaming_body(): - yield b"Hello, " - yield b"world!" - - return httpcore.Response(200, body=streaming_body()) - return httpcore.Response(200, body=b"Hello, world!") +async def streaming_body(): + yield b"Hello, " + yield b"world!" -http = MockHTTP() - - -@pytest.mark.asyncio -async def test_request(): - response = await http.request("GET", "http://example.com") +def test_response(): + response = httpcore.Response(200, body=b"Hello, world!") assert response.status_code == 200 assert response.reason == "OK" assert response.body == b"Hello, world!" @@ -31,7 +18,7 @@ async def test_request(): @pytest.mark.asyncio async def test_read_response(): - response = await http.request("GET", "http://example.com") + response = httpcore.Response(200, body=b"Hello, world!") assert response.status_code == 200 assert response.body == b"Hello, world!" @@ -45,25 +32,8 @@ async def test_read_response(): @pytest.mark.asyncio -async def test_stream_response(): - response = await http.request("GET", "http://example.com") - - assert response.status_code == 200 - assert response.body == b"Hello, world!" - assert response.is_closed - - body = b"" - async for part in response.stream(): - body += part - - assert body == b"Hello, world!" - assert response.body == b"Hello, world!" - assert response.is_closed - - -@pytest.mark.asyncio -async def test_read_streaming_response(): - response = await http.request("GET", "http://example.com", stream=True) +async def test_streaming_response(): + response = httpcore.Response(200, body=streaming_body()) assert response.status_code == 200 assert not hasattr(response, "body") @@ -76,26 +46,9 @@ async def test_read_streaming_response(): assert response.is_closed -@pytest.mark.asyncio -async def test_stream_streaming_response(): - response = await http.request("GET", "http://example.com", stream=True) - - assert response.status_code == 200 - assert not hasattr(response, "body") - assert not response.is_closed - - body = b"" - async for part in response.stream(): - body += part - - assert body == b"Hello, world!" - assert not hasattr(response, "body") - assert response.is_closed - - @pytest.mark.asyncio async def test_cannot_read_after_stream_consumed(): - response = await http.request("GET", "http://example.com", stream=True) + response = httpcore.Response(200, body=streaming_body()) body = b"" async for part in response.stream(): @@ -107,7 +60,7 @@ async def test_cannot_read_after_stream_consumed(): @pytest.mark.asyncio async def test_cannot_read_after_response_closed(): - response = await http.request("GET", "http://example.com", stream=True) + response = httpcore.Response(200, body=streaming_body()) await response.close() diff --git a/tests/test_timeouts.py b/tests/test_timeouts.py index b1ceef93..5b61aee2 100644 --- a/tests/test_timeouts.py +++ b/tests/test_timeouts.py @@ -7,19 +7,21 @@ import httpcore async def test_read_timeout(server): timeout = httpcore.TimeoutConfig(read_timeout=0.0001) - async with httpcore.ConnectionPool(timeout=timeout) as http: + async with httpcore.ConnectionPool(timeout=timeout) as client: with pytest.raises(httpcore.ReadTimeout): - await http.request("GET", "http://127.0.0.1:8000/slow_response") + request = httpcore.Request("GET", "http://127.0.0.1:8000/slow_response") + await client.send(request) @pytest.mark.asyncio async def test_connect_timeout(server): timeout = httpcore.TimeoutConfig(connect_timeout=0.0001) - async with httpcore.ConnectionPool(timeout=timeout) as http: + async with httpcore.ConnectionPool(timeout=timeout) as client: with pytest.raises(httpcore.ConnectTimeout): # See https://stackoverflow.com/questions/100841/ - await http.request("GET", "http://10.255.255.1/") + request = httpcore.Request("GET", "http://10.255.255.1/") + await client.send(request) @pytest.mark.asyncio @@ -27,10 +29,12 @@ async def test_pool_timeout(server): timeout = httpcore.TimeoutConfig(pool_timeout=0.0001) limits = httpcore.PoolLimits(hard_limit=1) - async with httpcore.ConnectionPool(timeout=timeout, limits=limits) as http: - response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) + async with httpcore.ConnectionPool(timeout=timeout, limits=limits) as client: + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + response = await client.send(request, stream=True) with pytest.raises(httpcore.PoolTimeout): - await http.request("GET", "http://localhost:8000/") + request = httpcore.Request("GET", "http://127.0.0.1:8000/") + await client.send(request) await response.read() From 9a362c0b017e764a5d0411b270ed22debad8d4fd Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 22 Apr 2019 22:12:46 +0100 Subject: [PATCH 3/5] Add client.request --- httpcore/connections.py | 1 + httpcore/datastructures.py | 24 +++++++++++++++++++++++- httpcore/pool.py | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/httpcore/connections.py b/httpcore/connections.py index dadcfd90..c9b69573 100644 --- a/httpcore/connections.py +++ b/httpcore/connections.py @@ -40,6 +40,7 @@ class Connection: async def send( self, request: Request, + *, ssl: typing.Optional[SSLConfig] = None, timeout: typing.Optional[TimeoutConfig] = None, stream: bool = False, diff --git a/httpcore/datastructures.py b/httpcore/datastructures.py index d6cadc42..09c288bc 100644 --- a/httpcore/datastructures.py +++ b/httpcore/datastructures.py @@ -2,6 +2,7 @@ import http import typing from urllib.parse import urlsplit +from .config import SSLConfig, TimeoutConfig from .decoders import ( ACCEPT_ENCODING, SUPPORTED_DECODERS, @@ -238,7 +239,28 @@ class Response: class Client: - async def send(self, request: Request, **options: typing.Any) -> Response: + async def request( + self, + method: str, + url: typing.Union[str, URL], + *, + headers: typing.Sequence[typing.Tuple[bytes, bytes]] = (), + body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"", + ssl: typing.Optional[SSLConfig] = None, + timeout: typing.Optional[TimeoutConfig] = None, + stream: bool = False, + ) -> Response: + request = Request(method, url, headers=headers, body=body) + return await self.send(request, ssl=ssl, timeout=timeout, stream=stream) + + async def send( + self, + request: Request, + *, + ssl: typing.Optional[SSLConfig] = None, + timeout: typing.Optional[TimeoutConfig] = None, + stream: bool = False, + ) -> Response: raise NotImplementedError() # pragma: nocover async def close(self) -> None: diff --git a/httpcore/pool.py b/httpcore/pool.py index 1a846628..3f9ecd70 100644 --- a/httpcore/pool.py +++ b/httpcore/pool.py @@ -54,6 +54,7 @@ class ConnectionPool: async def send( self, request: Request, + *, ssl: typing.Optional[SSLConfig] = None, timeout: typing.Optional[TimeoutConfig] = None, stream: bool = False, From 275443c4a11290a00497340dc8b572289ee05c75 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 23 Apr 2019 09:34:41 +0100 Subject: [PATCH 4/5] Add client.request --- httpcore/connections.py | 14 +---- httpcore/datastructures.py | 21 ++++++- httpcore/pool.py | 43 +++++--------- tests/test_api.py | 22 ++++---- tests/test_connections.py | 12 ++-- tests/test_pool.py | 112 +++++++++++++++++-------------------- tests/test_timeouts.py | 18 +++--- 7 files changed, 107 insertions(+), 135 deletions(-) diff --git a/httpcore/connections.py b/httpcore/connections.py index c9b69573..6ddea6da 100644 --- a/httpcore/connections.py +++ b/httpcore/connections.py @@ -17,7 +17,7 @@ H11Event = typing.Union[ ] -class Connection: +class Connection(Client): def __init__( self, origin: typing.Union[str, Origin], @@ -43,7 +43,6 @@ class Connection: *, ssl: typing.Optional[SSLConfig] = None, timeout: typing.Optional[TimeoutConfig] = None, - stream: bool = False, ) -> Response: assert request.url.origin == self.origin @@ -85,7 +84,7 @@ class Connection: status_code = event.status_code headers = event.headers body = self._body_iter(timeout) - response = Response( + return Response( status_code=status_code, reason=reason, headers=headers, @@ -93,15 +92,6 @@ class Connection: on_close=self._release, ) - if not stream: - # Read the response body. - try: - await response.read() - finally: - await response.close() - - return response - async def _connect(self, ssl: SSLConfig, timeout: TimeoutConfig) -> None: ssl_context = await ssl.load_ssl_context() if self.origin.is_secure else None diff --git a/httpcore/datastructures.py b/httpcore/datastructures.py index 09c288bc..7389d451 100644 --- a/httpcore/datastructures.py +++ b/httpcore/datastructures.py @@ -1,5 +1,6 @@ import http import typing +from types import TracebackType from urllib.parse import urlsplit from .config import SSLConfig, TimeoutConfig @@ -251,7 +252,13 @@ class Client: stream: bool = False, ) -> Response: request = Request(method, url, headers=headers, body=body) - return await self.send(request, ssl=ssl, timeout=timeout, stream=stream) + response = await self.send(request, ssl=ssl, timeout=timeout) + if not stream: + try: + await response.read() + finally: + await response.close() + return response async def send( self, @@ -259,9 +266,19 @@ class Client: *, ssl: typing.Optional[SSLConfig] = None, timeout: typing.Optional[TimeoutConfig] = None, - stream: bool = False, ) -> Response: raise NotImplementedError() # pragma: nocover async def close(self) -> None: raise NotImplementedError() # pragma: nocover + + async def __aenter__(self) -> "Client": + return self + + async def __aexit__( + self, + exc_type: typing.Type[BaseException] = None, + exc_value: BaseException = None, + traceback: TracebackType = None, + ) -> None: + await self.close() diff --git a/httpcore/pool.py b/httpcore/pool.py index 3f9ecd70..a1365718 100644 --- a/httpcore/pool.py +++ b/httpcore/pool.py @@ -1,6 +1,5 @@ import asyncio import typing -from types import TracebackType from .config import ( DEFAULT_CA_BUNDLE_PATH, @@ -16,21 +15,7 @@ from .datastructures import Client, Origin, Request, Response from .exceptions import PoolTimeout -class ConnectionSemaphore: - def __init__(self, max_connections: int = None): - if max_connections is not None: - self.semaphore = asyncio.BoundedSemaphore(value=max_connections) - - async def acquire(self) -> None: - if hasattr(self, "semaphore"): - await self.semaphore.acquire() - - def release(self) -> None: - if hasattr(self, "semaphore"): - self.semaphore.release() - - -class ConnectionPool: +class ConnectionPool(Client): def __init__( self, *, @@ -57,12 +42,9 @@ class ConnectionPool: *, ssl: typing.Optional[SSLConfig] = None, timeout: typing.Optional[TimeoutConfig] = None, - stream: bool = False, ) -> Response: connection = await self.acquire_connection(request.url.origin, timeout=timeout) - response = await connection.send( - request, ssl=ssl, timeout=timeout, stream=stream - ) + response = await connection.send(request, ssl=ssl, timeout=timeout) return response @property @@ -121,13 +103,16 @@ class ConnectionPool: async def close(self) -> None: self.is_closed = True - async def __aenter__(self) -> "ConnectionPool": - return self - async def __aexit__( - self, - exc_type: typing.Type[BaseException] = None, - exc_value: BaseException = None, - traceback: TracebackType = None, - ) -> None: - await self.close() +class ConnectionSemaphore: + def __init__(self, max_connections: int = None): + if max_connections is not None: + self.semaphore = asyncio.BoundedSemaphore(value=max_connections) + + async def acquire(self) -> None: + if hasattr(self, "semaphore"): + await self.semaphore.acquire() + + def release(self) -> None: + if hasattr(self, "semaphore"): + self.semaphore.release() diff --git a/tests/test_api.py b/tests/test_api.py index 30199c93..6b80587d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -5,28 +5,25 @@ import httpcore @pytest.mark.asyncio async def test_get(server): - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request) + async with httpcore.ConnectionPool() as http: + response = await http.request("GET", "http://127.0.0.1:8000/") assert response.status_code == 200 assert response.body == b"Hello, world!" @pytest.mark.asyncio async def test_post(server): - async with httpcore.ConnectionPool() as client: - request = httpcore.Request( + async with httpcore.ConnectionPool() as http: + response = await http.request( "POST", "http://127.0.0.1:8000/", body=b"Hello, world!" ) - response = await client.send(request) assert response.status_code == 200 @pytest.mark.asyncio async def test_stream_response(server): - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request, stream=True) + async with httpcore.ConnectionPool() as http: + response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) assert response.status_code == 200 assert not hasattr(response, "body") body = await response.read() @@ -39,7 +36,8 @@ async def test_stream_request(server): yield b"Hello, " yield b"world!" - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("POST", "http://127.0.0.1:8000/", body=hello_world()) - response = await client.send(request) + async with httpcore.ConnectionPool() as http: + response = await http.request( + "POST", "http://127.0.0.1:8000/", body=hello_world() + ) assert response.status_code == 200 diff --git a/tests/test_connections.py b/tests/test_connections.py index 958c1b52..5cfca611 100644 --- a/tests/test_connections.py +++ b/tests/test_connections.py @@ -5,18 +5,16 @@ import httpcore @pytest.mark.asyncio async def test_get(server): - client = httpcore.Connection(origin="http://127.0.0.1:8000/") - request = httpcore.Request(method="GET", url="http://127.0.0.1:8000/") - response = await client.send(request) + http = httpcore.Connection(origin="http://127.0.0.1:8000/") + response = await http.request("GET", "http://127.0.0.1:8000/") assert response.status_code == 200 assert response.body == b"Hello, world!" @pytest.mark.asyncio async def test_post(server): - client = httpcore.Connection(origin="http://127.0.0.1:8000/") - request = httpcore.Request( - method="POST", url="http://127.0.0.1:8000/", body=b"Hello, world!" + http = httpcore.Connection(origin="http://127.0.0.1:8000/") + response = await http.request( + "POST", "http://127.0.0.1:8000/", body=b"Hello, world!" ) - response = await client.send(request) assert response.status_code == 200 diff --git a/tests/test_pool.py b/tests/test_pool.py index de25fe65..77a22157 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -8,16 +8,14 @@ async def test_keepalive_connections(server): """ Connections should default to staying in a keep-alive state. """ - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request) - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 1 + async with httpcore.ConnectionPool() as http: + response = await http.request("GET", "http://127.0.0.1:8000/") + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 1 - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request) - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 1 + response = await http.request("GET", "http://127.0.0.1:8000/") + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 1 @pytest.mark.asyncio @@ -25,16 +23,14 @@ async def test_differing_connection_keys(server): """ Connnections to differing connection keys should result in multiple connections. """ - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request) - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 1 + async with httpcore.ConnectionPool() as http: + response = await http.request("GET", "http://127.0.0.1:8000/") + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 1 - request = httpcore.Request("GET", "http://localhost:8000/") - response = await client.send(request) - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 2 + response = await http.request("GET", "http://localhost:8000/") + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 2 @pytest.mark.asyncio @@ -44,16 +40,14 @@ async def test_soft_limit(server): """ limits = httpcore.PoolLimits(soft_limit=1) - async with httpcore.ConnectionPool(limits=limits) as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request) - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 1 + async with httpcore.ConnectionPool(limits=limits) as http: + response = await http.request("GET", "http://127.0.0.1:8000/") + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 1 - request = httpcore.Request("GET", "http://localhost:8000/") - response = await client.send(request) - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 1 + response = await http.request("GET", "http://localhost:8000/") + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 1 @pytest.mark.asyncio @@ -61,16 +55,15 @@ async def test_streaming_response_holds_connection(server): """ A streaming request should hold the connection open until the response is read. """ - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request, stream=True) - assert client.num_active_connections == 1 - assert client.num_keepalive_connections == 0 + async with httpcore.ConnectionPool() as http: + response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) + assert http.num_active_connections == 1 + assert http.num_keepalive_connections == 0 await response.read() - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 1 + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 1 @pytest.mark.asyncio @@ -78,24 +71,22 @@ async def test_multiple_concurrent_connections(server): """ Multiple conncurrent requests should open multiple conncurrent connections. """ - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response_a = await client.send(request, stream=True) - assert client.num_active_connections == 1 - assert client.num_keepalive_connections == 0 + async with httpcore.ConnectionPool() as http: + response_a = await http.request("GET", "http://127.0.0.1:8000/", stream=True) + assert http.num_active_connections == 1 + assert http.num_keepalive_connections == 0 - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response_b = await client.send(request, stream=True) - assert client.num_active_connections == 2 - assert client.num_keepalive_connections == 0 + response_b = await http.request("GET", "http://127.0.0.1:8000/", stream=True) + assert http.num_active_connections == 2 + assert http.num_keepalive_connections == 0 await response_b.read() - assert client.num_active_connections == 1 - assert client.num_keepalive_connections == 1 + assert http.num_active_connections == 1 + assert http.num_keepalive_connections == 1 await response_a.read() - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 2 + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 2 @pytest.mark.asyncio @@ -104,11 +95,10 @@ async def test_close_connections(server): Using a `Connection: close` header should close the connection. """ headers = [(b"connection", b"close")] - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/", headers=headers) - response = await client.send(request) - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 0 + async with httpcore.ConnectionPool() as http: + response = await http.request("GET", "http://127.0.0.1:8000/", headers=headers) + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 0 @pytest.mark.asyncio @@ -116,13 +106,12 @@ async def test_standard_response_close(server): """ A standard close should keep the connection open. """ - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request, stream=True) + async with httpcore.ConnectionPool() as http: + response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) await response.read() await response.close() - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 1 + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 1 @pytest.mark.asyncio @@ -130,9 +119,8 @@ async def test_premature_response_close(server): """ A premature close should close the connection. """ - async with httpcore.ConnectionPool() as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request, stream=True) + async with httpcore.ConnectionPool() as http: + response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) await response.close() - assert client.num_active_connections == 0 - assert client.num_keepalive_connections == 0 + assert http.num_active_connections == 0 + assert http.num_keepalive_connections == 0 diff --git a/tests/test_timeouts.py b/tests/test_timeouts.py index 5b61aee2..e9003195 100644 --- a/tests/test_timeouts.py +++ b/tests/test_timeouts.py @@ -7,21 +7,19 @@ import httpcore async def test_read_timeout(server): timeout = httpcore.TimeoutConfig(read_timeout=0.0001) - async with httpcore.ConnectionPool(timeout=timeout) as client: + async with httpcore.ConnectionPool(timeout=timeout) as http: with pytest.raises(httpcore.ReadTimeout): - request = httpcore.Request("GET", "http://127.0.0.1:8000/slow_response") - await client.send(request) + await http.request("GET", "http://127.0.0.1:8000/slow_response") @pytest.mark.asyncio async def test_connect_timeout(server): timeout = httpcore.TimeoutConfig(connect_timeout=0.0001) - async with httpcore.ConnectionPool(timeout=timeout) as client: + async with httpcore.ConnectionPool(timeout=timeout) as http: with pytest.raises(httpcore.ConnectTimeout): # See https://stackoverflow.com/questions/100841/ - request = httpcore.Request("GET", "http://10.255.255.1/") - await client.send(request) + await http.request("GET", "http://10.255.255.1/") @pytest.mark.asyncio @@ -29,12 +27,10 @@ async def test_pool_timeout(server): timeout = httpcore.TimeoutConfig(pool_timeout=0.0001) limits = httpcore.PoolLimits(hard_limit=1) - async with httpcore.ConnectionPool(timeout=timeout, limits=limits) as client: - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - response = await client.send(request, stream=True) + async with httpcore.ConnectionPool(timeout=timeout, limits=limits) as http: + response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) with pytest.raises(httpcore.PoolTimeout): - request = httpcore.Request("GET", "http://127.0.0.1:8000/") - await client.send(request) + await http.request("GET", "http://127.0.0.1:8000/") await response.read() From 5b2f3ed656bf81e4d5f550b74c06393a62e8f536 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 23 Apr 2019 09:37:10 +0100 Subject: [PATCH 5/5] Use different hosts in pool acquiry timeout test. --- tests/test_timeouts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_timeouts.py b/tests/test_timeouts.py index e9003195..b1ceef93 100644 --- a/tests/test_timeouts.py +++ b/tests/test_timeouts.py @@ -31,6 +31,6 @@ async def test_pool_timeout(server): response = await http.request("GET", "http://127.0.0.1:8000/", stream=True) with pytest.raises(httpcore.PoolTimeout): - await http.request("GET", "http://127.0.0.1:8000/") + await http.request("GET", "http://localhost:8000/") await response.read()