diff --git a/httpcore/_async/connection.py b/httpcore/_async/connection.py index 2f439cf0..3a7d18d5 100644 --- a/httpcore/_async/connection.py +++ b/httpcore/_async/connection.py @@ -67,7 +67,8 @@ def __init__( async def handle_async_request(self, request: Request) -> Response: if not self.can_handle_request(request.url.origin): raise RuntimeError( - f"Attempted to send request to {request.url.origin} on connection to {self._origin}" + f"Attempted to send request to {request.url.origin}" + f" on connection to {self._origin}" ) try: @@ -118,7 +119,12 @@ async def _connect(self, request: Request) -> AsyncNetworkStream: "timeout": timeout, "socket_options": self._socket_options, } - async with Trace("connect_tcp", logger, request, kwargs) as trace: + async with Trace( + "connect_tcp", + logger, + request, + kwargs, + ) as trace: stream = await self._network_backend.connect_tcp(**kwargs) trace.return_value = stream else: @@ -128,10 +134,13 @@ async def _connect(self, request: Request) -> AsyncNetworkStream: "socket_options": self._socket_options, } async with Trace( - "connect_unix_socket", logger, request, kwargs + "connect_unix_socket", + logger, + request, + kwargs, ) as trace: stream = await self._network_backend.connect_unix_socket( - **kwargs + **kwargs, ) trace.return_value = stream diff --git a/httpcore/_async/connection_pool.py b/httpcore/_async/connection_pool.py index 018b0ba2..60248a0a 100644 --- a/httpcore/_async/connection_pool.py +++ b/httpcore/_async/connection_pool.py @@ -1,7 +1,14 @@ import ssl import sys from types import TracebackType -from typing import AsyncIterable, AsyncIterator, Iterable, List, Optional, Type +from typing import ( # noqa: F811 + AsyncIterable, + AsyncIterator, + Iterable, + List, + Optional, + Type, +) from .._backends.auto import AutoBackend from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend @@ -19,7 +26,8 @@ def __init__(self, request: Request) -> None: self._connection_acquired = AsyncEvent() def assign_to_connection( - self, connection: Optional[AsyncConnectionInterface] + self, + connection: Optional[AsyncConnectionInterface], ) -> None: self.connection = connection self._connection_acquired.set() @@ -119,9 +127,10 @@ def __init__( self._connections: List[AsyncConnectionInterface] = [] self._requests: List[AsyncPoolRequest] = [] - # We only mutate the state of the connection pool within an 'optional_thread_lock' - # context. This holds a threading lock unless we're running in async mode, - # in which case it is a no-op. + # We only mutate the state of the connection pool + # within an 'optional_thread_lock' context. + # This holds a threading lock unless we're running in + # async mode, in which case it is a no-op. self._optional_thread_lock = AsyncThreadLock() def create_connection(self, origin: Origin) -> AsyncConnectionInterface: @@ -148,9 +157,12 @@ def connections(self) -> List[AsyncConnectionInterface]: ```python >>> pool.connections [ - , - , - , + , + , + , ] ``` """ @@ -160,7 +172,7 @@ async def handle_async_request(self, request: Request) -> Response: """ Send an HTTP request, and return an HTTP response. - This is the core implementation that is called into by `.request()` or `.stream()`. + The core implementation that is called into by `.request()` or `.stream()`. """ scheme = request.url.scheme.decode() if scheme == "": @@ -194,7 +206,7 @@ async def handle_async_request(self, request: Request) -> Response: try: # Send the request on the assigned connection. response = await connection.handle_async_request( - pool_request.request + pool_request.request, ) except ConnectionNotAvailable: # In some cases a connection may initially be available to diff --git a/httpcore/_async/http11.py b/httpcore/_async/http11.py index 0493a923..d2b18d20 100644 --- a/httpcore/_async/http11.py +++ b/httpcore/_async/http11.py @@ -88,7 +88,10 @@ async def handle_async_request(self, request: Request) -> Response: kwargs = {"request": request} try: async with Trace( - "send_request_headers", logger, request, kwargs + "send_request_headers", + logger, + request, + kwargs, ) as trace: await self._send_request_headers(**kwargs) async with Trace("send_request_body", logger, request, kwargs) as trace: @@ -102,7 +105,10 @@ async def handle_async_request(self, request: Request) -> Response: pass async with Trace( - "receive_response_headers", logger, request, kwargs + "receive_response_headers", + logger, + request, + kwargs, ) as trace: ( http_version, @@ -168,7 +174,9 @@ async def _send_request_body(self, request: Request) -> None: await self._send_event(h11.EndOfMessage(), timeout=timeout) async def _send_event( - self, event: h11.Event, timeout: Optional[float] = None + self, + event: h11.Event, + timeout: Optional[float] = None, ) -> None: bytes_to_send = self._h11_state.send(event) if bytes_to_send is not None: @@ -222,7 +230,8 @@ async def _receive_event( if event is h11.NEED_DATA: data = await self._network_stream.read( - self.READ_NUM_BYTES, timeout=timeout + self.READ_NUM_BYTES, + timeout=timeout, ) # If we feed this case through h11 we'll raise an exception like: diff --git a/httpcore/_async/http2.py b/httpcore/_async/http2.py index c201ee4c..25e0ba20 100644 --- a/httpcore/_async/http2.py +++ b/httpcore/_async/http2.py @@ -143,7 +143,10 @@ async def handle_async_request(self, request: Request) -> Response: async with Trace("send_request_body", logger, request, kwargs): await self._send_request_body(request=request, stream_id=stream_id) async with Trace( - "receive_response_headers", logger, request, kwargs + "receive_response_headers", + logger, + request, + kwargs, ) as trace: status, headers = await self._receive_response( request=request, stream_id=stream_id @@ -261,7 +264,10 @@ async def _send_request_body(self, request: Request, stream_id: int) -> None: await self._send_end_stream(request, stream_id) async def _send_stream_data( - self, request: Request, stream_id: int, data: bytes + self, + request: Request, + stream_id: int, + data: bytes, ) -> None: """ Send a single chunk of data in one or more data frames. @@ -362,7 +368,9 @@ async def _receive_events( for event in events: if isinstance(event, h2.events.RemoteSettingsChanged): async with Trace( - "receive_remote_settings", logger, request + "receive_remote_settings", + logger, + request, ) as trace: await self._receive_remote_settings_change(event) trace.return_value = event @@ -426,7 +434,8 @@ async def aclose(self) -> None: # Wrappers around network read/write operations... async def _read_incoming_data( - self, request: Request + self, + request: Request, ) -> typing.List[h2.events.Event]: timeouts = request.extensions.get("timeout", {}) timeout = timeouts.get("read", None) @@ -470,10 +479,10 @@ async def _write_outgoing_data(self, request: Request) -> None: except Exception as exc: # pragma: nocover # If we get a network error we should: # - # 1. Save the exception and just raise it immediately on any future write. - # (For example, this means that a single write timeout or disconnect will - # immediately close all pending streams. Without requiring multiple - # sequential timeouts.) + # 1. Save the exception and just raise it immediately on any + # future write. (For example, this means that a single write timeout + # or disconnect will immediately close all pending streams, + # without requiring multiple sequential timeouts.) # 2. Mark the connection as errored, so that we don't accept any other # incoming requests. self._write_exception = exc diff --git a/httpcore/_async/http_proxy.py b/httpcore/_async/http_proxy.py index 4aa7d874..9c98fb56 100644 --- a/httpcore/_async/http_proxy.py +++ b/httpcore/_async/http_proxy.py @@ -89,7 +89,8 @@ def __init__( ssl_context: An SSL context to use for verifying connections. If not specified, the default `httpcore.default_ssl_context()` will be used. - proxy_ssl_context: The same as `ssl_context`, but for a proxy server rather than a remote origin. + proxy_ssl_context: The same as `ssl_context`, but for a proxy server + rather than a remote origin. max_connections: The maximum number of concurrent HTTP connections that the pool should allow. Any attempt to send a request on a pool that would exceed this amount will block until a connection is available. @@ -287,7 +288,7 @@ async def handle_async_request(self, request: Request) -> Response: extensions=request.extensions, ) connect_response = await self._connection.handle_async_request( - connect_request + connect_request, ) if connect_response.status < 200 or connect_response.status > 299: diff --git a/httpcore/_backends/trio.py b/httpcore/_backends/trio.py index b1626d28..4cecef6e 100644 --- a/httpcore/_backends/trio.py +++ b/httpcore/_backends/trio.py @@ -82,7 +82,8 @@ async def start_tls( def get_extra_info(self, info: str) -> typing.Any: if info == "ssl_object" and isinstance(self._stream, trio.SSLStream): - # Type checkers cannot see `_ssl_object` attribute because trio._ssl.SSLStream uses __getattr__/__setattr__. + # Type checkers cannot see `_ssl_object` attribute + # because `trio._ssl.SSLStream` uses `__getattr__`/`__setattr__`. # Tracked at https://github.com/python-trio/trio/issues/542 return self._stream._ssl_object # type: ignore[attr-defined] if info == "client_addr": diff --git a/httpcore/_sync/connection.py b/httpcore/_sync/connection.py index c3890f34..6c813f42 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -67,7 +67,8 @@ def __init__( def handle_request(self, request: Request) -> Response: if not self.can_handle_request(request.url.origin): raise RuntimeError( - f"Attempted to send request to {request.url.origin} on connection to {self._origin}" + f"Attempted to send request to {request.url.origin}" + f" on connection to {self._origin}" ) try: @@ -118,7 +119,12 @@ def _connect(self, request: Request) -> NetworkStream: "timeout": timeout, "socket_options": self._socket_options, } - with Trace("connect_tcp", logger, request, kwargs) as trace: + with Trace( + "connect_tcp", + logger, + request, + kwargs, + ) as trace: stream = self._network_backend.connect_tcp(**kwargs) trace.return_value = stream else: @@ -128,10 +134,13 @@ def _connect(self, request: Request) -> NetworkStream: "socket_options": self._socket_options, } with Trace( - "connect_unix_socket", logger, request, kwargs + "connect_unix_socket", + logger, + request, + kwargs, ) as trace: stream = self._network_backend.connect_unix_socket( - **kwargs + **kwargs, ) trace.return_value = stream diff --git a/httpcore/_sync/connection_pool.py b/httpcore/_sync/connection_pool.py index 8dcf348c..62dda925 100644 --- a/httpcore/_sync/connection_pool.py +++ b/httpcore/_sync/connection_pool.py @@ -1,7 +1,14 @@ import ssl import sys from types import TracebackType -from typing import Iterable, Iterator, Iterable, List, Optional, Type +from typing import ( # noqa: F811 + Iterable, + Iterator, + Iterable, + List, + Optional, + Type, +) from .._backends.sync import SyncBackend from .._backends.base import SOCKET_OPTION, NetworkBackend @@ -19,7 +26,8 @@ def __init__(self, request: Request) -> None: self._connection_acquired = Event() def assign_to_connection( - self, connection: Optional[ConnectionInterface] + self, + connection: Optional[ConnectionInterface], ) -> None: self.connection = connection self._connection_acquired.set() @@ -119,9 +127,10 @@ def __init__( self._connections: List[ConnectionInterface] = [] self._requests: List[PoolRequest] = [] - # We only mutate the state of the connection pool within an 'optional_thread_lock' - # context. This holds a threading lock unless we're running in async mode, - # in which case it is a no-op. + # We only mutate the state of the connection pool + # within an 'optional_thread_lock' context. + # This holds a threading lock unless we're running in + # async mode, in which case it is a no-op. self._optional_thread_lock = ThreadLock() def create_connection(self, origin: Origin) -> ConnectionInterface: @@ -148,9 +157,12 @@ def connections(self) -> List[ConnectionInterface]: ```python >>> pool.connections [ - , - , - , + , + , + , ] ``` """ @@ -160,7 +172,7 @@ def handle_request(self, request: Request) -> Response: """ Send an HTTP request, and return an HTTP response. - This is the core implementation that is called into by `.request()` or `.stream()`. + The core implementation that is called into by `.request()` or `.stream()`. """ scheme = request.url.scheme.decode() if scheme == "": @@ -194,7 +206,7 @@ def handle_request(self, request: Request) -> Response: try: # Send the request on the assigned connection. response = connection.handle_request( - pool_request.request + pool_request.request, ) except ConnectionNotAvailable: # In some cases a connection may initially be available to diff --git a/httpcore/_sync/http11.py b/httpcore/_sync/http11.py index a74ff8e8..7671d575 100644 --- a/httpcore/_sync/http11.py +++ b/httpcore/_sync/http11.py @@ -88,7 +88,10 @@ def handle_request(self, request: Request) -> Response: kwargs = {"request": request} try: with Trace( - "send_request_headers", logger, request, kwargs + "send_request_headers", + logger, + request, + kwargs, ) as trace: self._send_request_headers(**kwargs) with Trace("send_request_body", logger, request, kwargs) as trace: @@ -102,7 +105,10 @@ def handle_request(self, request: Request) -> Response: pass with Trace( - "receive_response_headers", logger, request, kwargs + "receive_response_headers", + logger, + request, + kwargs, ) as trace: ( http_version, @@ -168,7 +174,9 @@ def _send_request_body(self, request: Request) -> None: self._send_event(h11.EndOfMessage(), timeout=timeout) def _send_event( - self, event: h11.Event, timeout: Optional[float] = None + self, + event: h11.Event, + timeout: Optional[float] = None, ) -> None: bytes_to_send = self._h11_state.send(event) if bytes_to_send is not None: @@ -222,7 +230,8 @@ def _receive_event( if event is h11.NEED_DATA: data = self._network_stream.read( - self.READ_NUM_BYTES, timeout=timeout + self.READ_NUM_BYTES, + timeout=timeout, ) # If we feed this case through h11 we'll raise an exception like: diff --git a/httpcore/_sync/http2.py b/httpcore/_sync/http2.py index 1ee4bbb3..a17ad112 100644 --- a/httpcore/_sync/http2.py +++ b/httpcore/_sync/http2.py @@ -143,7 +143,10 @@ def handle_request(self, request: Request) -> Response: with Trace("send_request_body", logger, request, kwargs): self._send_request_body(request=request, stream_id=stream_id) with Trace( - "receive_response_headers", logger, request, kwargs + "receive_response_headers", + logger, + request, + kwargs, ) as trace: status, headers = self._receive_response( request=request, stream_id=stream_id @@ -261,7 +264,10 @@ def _send_request_body(self, request: Request, stream_id: int) -> None: self._send_end_stream(request, stream_id) def _send_stream_data( - self, request: Request, stream_id: int, data: bytes + self, + request: Request, + stream_id: int, + data: bytes, ) -> None: """ Send a single chunk of data in one or more data frames. @@ -362,7 +368,9 @@ def _receive_events( for event in events: if isinstance(event, h2.events.RemoteSettingsChanged): with Trace( - "receive_remote_settings", logger, request + "receive_remote_settings", + logger, + request, ) as trace: self._receive_remote_settings_change(event) trace.return_value = event @@ -426,7 +434,8 @@ def close(self) -> None: # Wrappers around network read/write operations... def _read_incoming_data( - self, request: Request + self, + request: Request, ) -> typing.List[h2.events.Event]: timeouts = request.extensions.get("timeout", {}) timeout = timeouts.get("read", None) @@ -470,10 +479,10 @@ def _write_outgoing_data(self, request: Request) -> None: except Exception as exc: # pragma: nocover # If we get a network error we should: # - # 1. Save the exception and just raise it immediately on any future write. - # (For example, this means that a single write timeout or disconnect will - # immediately close all pending streams. Without requiring multiple - # sequential timeouts.) + # 1. Save the exception and just raise it immediately on any + # future write. (For example, this means that a single write timeout + # or disconnect will immediately close all pending streams, + # without requiring multiple sequential timeouts.) # 2. Mark the connection as errored, so that we don't accept any other # incoming requests. self._write_exception = exc diff --git a/httpcore/_sync/http_proxy.py b/httpcore/_sync/http_proxy.py index 6acac9a7..ea31c9fa 100644 --- a/httpcore/_sync/http_proxy.py +++ b/httpcore/_sync/http_proxy.py @@ -89,7 +89,8 @@ def __init__( ssl_context: An SSL context to use for verifying connections. If not specified, the default `httpcore.default_ssl_context()` will be used. - proxy_ssl_context: The same as `ssl_context`, but for a proxy server rather than a remote origin. + proxy_ssl_context: The same as `ssl_context`, but for a proxy server + rather than a remote origin. max_connections: The maximum number of concurrent HTTP connections that the pool should allow. Any attempt to send a request on a pool that would exceed this amount will block until a connection is available. @@ -287,7 +288,7 @@ def handle_request(self, request: Request) -> Response: extensions=request.extensions, ) connect_response = self._connection.handle_request( - connect_request + connect_request, ) if connect_response.status < 200 or connect_response.status > 299: diff --git a/pyproject.toml b/pyproject.toml index 85c78740..d65e3f07 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,7 +60,7 @@ include = [ "/httpcore", "/CHANGELOG.md", "/README.md", - "/tests" + "/tests", ] [tool.hatch.metadata.hooks.fancy-pypi-readme] @@ -96,22 +96,26 @@ filterwarnings = ["error"] [tool.coverage.run] omit = [ - "venv/*", - "httpcore/_sync/*" + "venv/*", + "httpcore/_sync/*", ] -include = ["httpcore/*", "tests/*"] - -[tool.ruff] -exclude = [ - "httpcore/_sync", - "tests/_sync", +include = [ + "httpcore/*", + "tests/*", ] [tool.ruff.lint] -select = ["E", "F", "W", "I"] - -[tool.ruff.lint.pycodestyle] -max-line-length = 120 +select = [ + "E", + "F", + "W", + "I", +] [tool.ruff.lint.isort] combine-as-imports = true + +[tool.ruff.lint.per-file-ignores] +# needed for unasync.py +"httpcore/_sync/*" = ["I001"] +"tests/_sync/*" = ["I001"] diff --git a/scripts/check b/scripts/check index cfc90918..511f4f6c 100755 --- a/scripts/check +++ b/scripts/check @@ -4,7 +4,7 @@ export PREFIX="" if [ -d 'venv' ] ; then export PREFIX="venv/bin/" fi -export SOURCE_FILES="httpcore tests" +export SOURCE_FILES="httpcore scripts tests" set -x diff --git a/scripts/lint b/scripts/lint index e40524f5..a57414b3 100755 --- a/scripts/lint +++ b/scripts/lint @@ -4,7 +4,7 @@ export PREFIX="" if [ -d 'venv' ] ; then export PREFIX="venv/bin/" fi -export SOURCE_FILES="httpcore tests" +export SOURCE_FILES="httpcore scripts tests" set -x diff --git a/scripts/unasync.py b/scripts/unasync.py index 5a5627d7..618ff639 100644 --- a/scripts/unasync.py +++ b/scripts/unasync.py @@ -5,34 +5,37 @@ from pprint import pprint SUBS = [ - ('from .._backends.auto import AutoBackend', 'from .._backends.sync import SyncBackend'), - ('import trio as concurrency', 'from tests import concurrency'), - ('AsyncIterator', 'Iterator'), - ('Async([A-Z][A-Za-z0-9_]*)', r'\2'), - ('async def', 'def'), - ('async with', 'with'), - ('async for', 'for'), - ('await ', ''), - ('handle_async_request', 'handle_request'), - ('aclose', 'close'), - ('aiter_stream', 'iter_stream'), - ('aread', 'read'), - ('asynccontextmanager', 'contextmanager'), - ('__aenter__', '__enter__'), - ('__aexit__', '__exit__'), - ('__aiter__', '__iter__'), - ('@pytest.mark.anyio', ''), - ('@pytest.mark.trio', ''), - ('AutoBackend', 'SyncBackend'), + ( + "from .._backends.auto import AutoBackend", + "from .._backends.sync import SyncBackend", + ), + ("import trio as concurrency", "from tests import concurrency"), + ("AsyncIterator", "Iterator"), + ("Async([A-Z][A-Za-z0-9_]*)", r"\2"), + ("async def", "def"), + ("async with", "with"), + ("async for", "for"), + ("await ", ""), + ("handle_async_request", "handle_request"), + ("aclose", "close"), + ("aiter_stream", "iter_stream"), + ("aread", "read"), + ("asynccontextmanager", "contextmanager"), + ("__aenter__", "__enter__"), + ("__aexit__", "__exit__"), + ("__aiter__", "__iter__"), + ("@pytest.mark.anyio", "# unasync anyio"), + ("@pytest.mark.trio", "# unasync trio"), + ("AutoBackend", "SyncBackend"), ] COMPILED_SUBS = [ - (re.compile(r'(^|\b)' + regex + r'($|\b)'), repl) - for regex, repl in SUBS + (re.compile(r"(^|\b)" + regex + r"($|\b)"), repl) for regex, repl in SUBS ] USED_SUBS = set() -def unasync_line(line): + +def unasync_line(line: str) -> str: for index, (regex, repl) in enumerate(COMPILED_SUBS): old_line = line line = re.sub(regex, repl, line) @@ -41,7 +44,7 @@ def unasync_line(line): return line -def unasync_file(in_path, out_path): +def unasync_file(in_path: str, out_path: str) -> None: with open(in_path, "r") as in_file: with open(out_path, "w", newline="") as out_file: for line in in_file.readlines(): @@ -49,36 +52,36 @@ def unasync_file(in_path, out_path): out_file.write(line) -def unasync_file_check(in_path, out_path): +def unasync_file_check(in_path: str, out_path: str) -> None: with open(in_path, "r") as in_file: with open(out_path, "r") as out_file: for in_line, out_line in zip(in_file.readlines(), out_file.readlines()): expected = unasync_line(in_line) if out_line != expected: - print(f'unasync mismatch between {in_path!r} and {out_path!r}') - print(f'Async code: {in_line!r}') - print(f'Expected sync code: {expected!r}') - print(f'Actual sync code: {out_line!r}') + print(f"unasync mismatch between {in_path!r} and {out_path!r}") + print(f"Async code: {in_line!r}") + print(f"Expected sync code: {expected!r}") + print(f"Actual sync code: {out_line!r}") sys.exit(1) -def unasync_dir(in_dir, out_dir, check_only=False): +def unasync_dir(in_dir: str, out_dir: str, check_only: bool = False) -> None: for dirpath, dirnames, filenames in os.walk(in_dir): for filename in filenames: - if not filename.endswith('.py'): + if not filename.endswith(".py"): continue rel_dir = os.path.relpath(dirpath, in_dir) in_path = os.path.normpath(os.path.join(in_dir, rel_dir, filename)) out_path = os.path.normpath(os.path.join(out_dir, rel_dir, filename)) - print(in_path, '->', out_path) + print(in_path, "->", out_path) if check_only: unasync_file_check(in_path, out_path) else: unasync_file(in_path, out_path) -def main(): - check_only = '--check' in sys.argv +def main() -> None: + check_only = "--check" in sys.argv unasync_dir("httpcore/_async", "httpcore/_sync", check_only=check_only) unasync_dir("tests/_async", "tests/_sync", check_only=check_only) @@ -87,8 +90,8 @@ def main(): print("These patterns were not used:") pprint(unused_subs) - exit(1) - + exit(1) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/tests/_async/test_connection.py b/tests/_async/test_connection.py index b6ee0c7e..414c3edf 100644 --- a/tests/_async/test_connection.py +++ b/tests/_async/test_connection.py @@ -42,9 +42,9 @@ async def test_http_connection(): assert repr(conn) == "" async with conn.stream("GET", "https://example.com/") as response: - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) await response.aread() @@ -55,9 +55,9 @@ async def test_http_connection(): assert not conn.is_closed() assert conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -86,8 +86,8 @@ async def test_concurrent_requests_not_available_on_http11_connections(): await conn.request("GET", "https://example.com/") -@pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") @pytest.mark.anyio +@pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") async def test_write_error_with_response_sent(): """ If a server half-closes the connection while the client is sending @@ -103,7 +103,9 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: self.count = 0 async def write( - self, buffer: bytes, timeout: typing.Optional[float] = None + self, + buffer: bytes, + timeout: typing.Optional[float] = None, ) -> None: self.count += len(buffer) @@ -157,7 +159,9 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: self.count = 0 async def write( - self, buffer: bytes, timeout: typing.Optional[float] = None + self, + buffer: bytes, + timeout: typing.Optional[float] = None, ) -> None: self.count += len(buffer) @@ -212,7 +216,9 @@ async def test_http2_connection(): ) async with AsyncHTTPConnection( - origin=origin, network_backend=network_backend, http2=True + origin=origin, + network_backend=network_backend, + http2=True, ) as conn: response = await conn.request("GET", "https://example.com/") @@ -229,7 +235,8 @@ async def test_request_to_incorrect_origin(): origin = Origin(b"https", b"example.com", 443) network_backend = AsyncMockBackend([]) async with AsyncHTTPConnection( - origin=origin, network_backend=network_backend + origin=origin, + network_backend=network_backend, ) as conn: with pytest.raises(RuntimeError): await conn.request("GET", "https://other.com/") @@ -266,18 +273,24 @@ async def connect_tcp( class _NeedsRetryAsyncNetworkStream(AsyncNetworkStream): def __init__( - self, backend: "NeedsRetryBackend", stream: AsyncNetworkStream + self, + backend: "NeedsRetryBackend", + stream: AsyncNetworkStream, ) -> None: self._backend = backend self._stream = stream async def read( - self, max_bytes: int, timeout: typing.Optional[float] = None + self, + max_bytes: int, + timeout: typing.Optional[float] = None, ) -> bytes: return await self._stream.read(max_bytes, timeout) async def write( - self, buffer: bytes, timeout: typing.Optional[float] = None + self, + buffer: bytes, + timeout: typing.Optional[float] = None, ) -> None: await self._stream.write(buffer, timeout) diff --git a/tests/_async/test_connection_pool.py b/tests/_async/test_connection_pool.py index 2fc27204..0dc8b485 100644 --- a/tests/_async/test_connection_pool.py +++ b/tests/_async/test_connection_pool.py @@ -36,11 +36,12 @@ async def test_connection_pool_with_keepalive(): async with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) await response.aread() @@ -48,22 +49,25 @@ async def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - # Sending a second request to the same origin will reuse the existing IDLE connection. + # Sending a second request to the same origin will + # reuse the existing IDLE connection. async with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) await response.aread() @@ -71,23 +75,27 @@ async def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - # Sending a request to a different origin will not reuse the existing IDLE connection. + # Sending a request to a different origin will not + # reuse the existing IDLE connection. async with pool.stream("GET", "http://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "", - "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) await response.aread() @@ -95,12 +103,14 @@ async def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "", - "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) @@ -127,7 +137,8 @@ async def test_connection_pool_with_close(): ) as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] await response.aread() @@ -185,17 +196,20 @@ async def test_connection_pool_with_http2(): info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - # Sending a second request to the same origin will reuse the existing IDLE connection. + # Sending a second request to the same origin will + # reuse the existing IDLE connection. response = await pool.request("GET", "https://example.com/") assert response.status == 200 assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] @@ -239,7 +253,8 @@ async def test_connection_pool_with_http2_goaway(): info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] # Sending a second request to the same origin will require a new connection. @@ -250,7 +265,8 @@ async def test_connection_pool_with_http2_goaway(): info = [repr(c) for c in pool.connections] assert info == [ - "", + "", ] @@ -321,7 +337,8 @@ async def test_debug_request(caplog): ( "httpcore.connection", logging.DEBUG, - "connect_tcp.started host='example.com' port=80 local_address=None timeout=None socket_options=None", + "connect_tcp.started host='example.com' port=80 local_address=None" + " timeout=None socket_options=None", ), ( "httpcore.connection", @@ -349,7 +366,8 @@ async def test_debug_request(caplog): "httpcore.http11", logging.DEBUG, "receive_response_headers.complete return_value=" - "(b'HTTP/1.1', 200, b'OK', [(b'Content-Type', b'plain/text'), (b'Content-Length', b'13')])", + "(b'HTTP/1.1', 200, b'OK', [(b'Content-Type', b'plain/text')," + " (b'Content-Length', b'13')])", ), ( "httpcore.http11", @@ -381,7 +399,9 @@ async def trace(name, kwargs): # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): await pool.request( - "GET", "https://example.com/", extensions={"trace": trace} + "GET", + "https://example.com/", + extensions={"trace": trace}, ) info = [repr(c) for c in pool.connections] @@ -434,7 +454,9 @@ async def trace(name, kwargs): # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): await pool.request( - "GET", "https://example.com/", extensions={"trace": trace} + "GET", + "https://example.com/", + extensions={"trace": trace}, ) info = [repr(c) for c in pool.connections] @@ -470,7 +492,8 @@ async def test_connection_pool_with_immediate_expiry(): async with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] await response.aread() @@ -503,7 +526,8 @@ async def test_connection_pool_with_no_keepalive_connections_allowed(): async with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] await response.aread() @@ -550,11 +574,16 @@ async def fetch(pool, domain, info_list): # Each connection was to a different host, and only sent a single # request on that connection. assert item[0] in [ - "", - "", - "", - "", - "", + "", + "", + "", + "", + "", ] @@ -594,9 +623,9 @@ async def fetch(pool, domain, info_list): # single connection was established at any one time. assert len(item) == 1 # Only a single request was sent on each connection. - assert ( - item[0] - == "" + assert item[0] == ( + "" ) @@ -637,16 +666,21 @@ async def fetch(pool, domain, info_list): assert len(item) == 1 # The connection sent multiple requests. assert item[0] in [ - "", - "", - "", - "", - "", + "", + "", + "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) @@ -745,13 +779,17 @@ async def test_connection_pool_timeout_zero(): # Two consecutive requests with a pool timeout of zero. # Both succeed without raising a timeout. response = await pool.request( - "GET", "https://example.com/", extensions=extensions + "GET", + "https://example.com/", + extensions=extensions, ) assert response.status == 200 assert response.content == b"Hello, world!" response = await pool.request( - "GET", "https://example.com/", extensions=extensions + "GET", + "https://example.com/", + extensions=extensions, ) assert response.status == 200 assert response.content == b"Hello, world!" diff --git a/tests/_async/test_http11.py b/tests/_async/test_http11.py index 94f2febf..707073cf 100644 --- a/tests/_async/test_http11.py +++ b/tests/_async/test_http11.py @@ -26,10 +26,10 @@ async def test_http11_connection(): assert not conn.is_closed() assert conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" - ) + assert repr(conn) == ( + "" + ) # fmt: skip @pytest.mark.anyio @@ -56,9 +56,9 @@ async def test_http11_connection_unread_response(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -78,9 +78,9 @@ async def test_http11_connection_with_remote_protocol_error(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -107,9 +107,9 @@ async def test_http11_connection_with_incomplete_response(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -139,9 +139,9 @@ async def test_http11_connection_with_local_protocol_error(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) diff --git a/tests/_async/test_http2.py b/tests/_async/test_http2.py index b4ec6648..45658348 100644 --- a/tests/_async/test_http2.py +++ b/tests/_async/test_http2.py @@ -40,9 +40,8 @@ async def test_http2_connection(): assert ( conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1" ) - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) diff --git a/tests/_async/test_http_proxy.py b/tests/_async/test_http_proxy.py index b35fc289..5a4475d2 100644 --- a/tests/_async/test_http_proxy.py +++ b/tests/_async/test_http_proxy.py @@ -40,7 +40,8 @@ async def test_proxy_forwarding(): async with proxy.stream("GET", "http://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -48,13 +49,15 @@ async def test_proxy_forwarding(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a forwarding proxy can only handle HTTP requests to the same origin. + # A connection on a forwarding proxy can only + # handle HTTP requests to the same origin. assert proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) @@ -95,7 +98,8 @@ async def test_proxy_tunneling(): async with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -103,13 +107,15 @@ async def test_proxy_tunneling(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) @@ -187,7 +193,8 @@ async def test_proxy_tunneling_http2(): async with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -195,13 +202,15 @@ async def test_proxy_tunneling_http2(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) diff --git a/tests/_async/test_integration.py b/tests/_async/test_integration.py index 1970531d..afa5dd9e 100644 --- a/tests/_async/test_integration.py +++ b/tests/_async/test_integration.py @@ -1,6 +1,6 @@ import ssl -import pytest +import pytest # noqa: F401 import httpcore diff --git a/tests/_async/test_socks_proxy.py b/tests/_async/test_socks_proxy.py index 3f5dd1cc..c70c8c61 100644 --- a/tests/_async/test_socks_proxy.py +++ b/tests/_async/test_socks_proxy.py @@ -32,7 +32,8 @@ async def test_socks5_request(): async with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -40,13 +41,15 @@ async def test_socks5_request(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( httpcore.Origin(b"http", b"example.com", 80) ) @@ -93,7 +96,8 @@ async def test_authenticated_socks5_request(): async with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -101,7 +105,8 @@ async def test_authenticated_socks5_request(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() @@ -157,9 +162,9 @@ async def test_socks5_request_failed_to_provide_auth(): # Sending a request, which the proxy rejects with pytest.raises(httpcore.ProxyError) as exc_info: await proxy.request("GET", "https://example.com/") - assert ( - str(exc_info.value) - == "Requested NO AUTHENTICATION REQUIRED from proxy server, but got USERNAME/PASSWORD." + assert str(exc_info.value) == ( + "Requested NO AUTHENTICATION REQUIRED from proxy server," + " but got USERNAME/PASSWORD." ) assert not proxy.connections diff --git a/tests/_sync/test_connection.py b/tests/_sync/test_connection.py index 37c82e02..fbae0630 100644 --- a/tests/_sync/test_connection.py +++ b/tests/_sync/test_connection.py @@ -19,7 +19,7 @@ ) - +# unasync anyio def test_http_connection(): origin = Origin(b"https", b"example.com", 443) network_backend = MockBackend( @@ -42,9 +42,9 @@ def test_http_connection(): assert repr(conn) == "" with conn.stream("GET", "https://example.com/") as response: - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) response.read() @@ -55,13 +55,13 @@ def test_http_connection(): assert not conn.is_closed() assert conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) - +# unasync anyio def test_concurrent_requests_not_available_on_http11_connections(): """ Attempting to issue a request against an already active HTTP/1.1 connection @@ -86,8 +86,8 @@ def test_concurrent_requests_not_available_on_http11_connections(): conn.request("GET", "https://example.com/") +# unasync anyio @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") - def test_write_error_with_response_sent(): """ If a server half-closes the connection while the client is sending @@ -103,7 +103,9 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: self.count = 0 def write( - self, buffer: bytes, timeout: typing.Optional[float] = None + self, + buffer: bytes, + timeout: typing.Optional[float] = None, ) -> None: self.count += len(buffer) @@ -141,7 +143,7 @@ def connect_tcp( assert response.content == b"Request body exceeded 1,000,000 bytes" - +# unasync anyio @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") def test_write_error_without_response_sent(): """ @@ -157,7 +159,9 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: self.count = 0 def write( - self, buffer: bytes, timeout: typing.Optional[float] = None + self, + buffer: bytes, + timeout: typing.Optional[float] = None, ) -> None: self.count += len(buffer) @@ -187,7 +191,7 @@ def connect_tcp( assert str(exc_info.value) == "Server disconnected without sending a response." - +# unasync anyio @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") def test_http2_connection(): origin = Origin(b"https", b"example.com", 443) @@ -212,7 +216,9 @@ def test_http2_connection(): ) with HTTPConnection( - origin=origin, network_backend=network_backend, http2=True + origin=origin, + network_backend=network_backend, + http2=True, ) as conn: response = conn.request("GET", "https://example.com/") @@ -221,7 +227,7 @@ def test_http2_connection(): assert response.extensions["http_version"] == b"HTTP/2" - +# unasync anyio def test_request_to_incorrect_origin(): """ A connection can only send requests whichever origin it is connected to. @@ -229,7 +235,8 @@ def test_request_to_incorrect_origin(): origin = Origin(b"https", b"example.com", 443) network_backend = MockBackend([]) with HTTPConnection( - origin=origin, network_backend=network_backend + origin=origin, + network_backend=network_backend, ) as conn: with pytest.raises(RuntimeError): conn.request("GET", "https://other.com/") @@ -266,18 +273,24 @@ def connect_tcp( class _NeedsRetryAsyncNetworkStream(NetworkStream): def __init__( - self, backend: "NeedsRetryBackend", stream: NetworkStream + self, + backend: "NeedsRetryBackend", + stream: NetworkStream, ) -> None: self._backend = backend self._stream = stream def read( - self, max_bytes: int, timeout: typing.Optional[float] = None + self, + max_bytes: int, + timeout: typing.Optional[float] = None, ) -> bytes: return self._stream.read(max_bytes, timeout) def write( - self, buffer: bytes, timeout: typing.Optional[float] = None + self, + buffer: bytes, + timeout: typing.Optional[float] = None, ) -> None: self._stream.write(buffer, timeout) @@ -301,7 +314,7 @@ def get_extra_info(self, info: str) -> typing.Any: return self._stream.get_extra_info(info) - +# unasync anyio def test_connection_retries(): origin = Origin(b"https", b"example.com", 443) content = [ @@ -328,7 +341,7 @@ def test_connection_retries(): conn.request("GET", "https://example.com/") - +# unasync anyio def test_connection_retries_tls(): origin = Origin(b"https", b"example.com", 443) content = [ @@ -359,7 +372,7 @@ def test_connection_retries_tls(): conn.request("GET", "https://example.com/") - +# unasync anyio def test_uds_connections(): # We're not actually testing Unix Domain Sockets here, because we're just # using a mock backend, but at least we're covering the UDS codepath diff --git a/tests/_sync/test_connection_pool.py b/tests/_sync/test_connection_pool.py index ee303e5c..c115c4dd 100644 --- a/tests/_sync/test_connection_pool.py +++ b/tests/_sync/test_connection_pool.py @@ -9,7 +9,7 @@ import httpcore - +# unasync anyio def test_connection_pool_with_keepalive(): """ By default HTTP/1.1 requests should be returned to the connection pool. @@ -36,11 +36,12 @@ def test_connection_pool_with_keepalive(): with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) response.read() @@ -48,22 +49,25 @@ def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - # Sending a second request to the same origin will reuse the existing IDLE connection. + # Sending a second request to the same origin will + # reuse the existing IDLE connection. with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) response.read() @@ -71,23 +75,27 @@ def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - # Sending a request to a different origin will not reuse the existing IDLE connection. + # Sending a request to a different origin will not + # reuse the existing IDLE connection. with pool.stream("GET", "http://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "", - "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) response.read() @@ -95,16 +103,18 @@ def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "", - "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - +# unasync anyio def test_connection_pool_with_close(): """ HTTP/1.1 requests that include a 'Connection: Close' header should @@ -127,7 +137,8 @@ def test_connection_pool_with_close(): ) as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] response.read() @@ -137,7 +148,7 @@ def test_connection_pool_with_close(): assert info == [] - +# unasync anyio def test_connection_pool_with_http2(): """ Test a connection pool with HTTP/2 requests. @@ -185,21 +196,24 @@ def test_connection_pool_with_http2(): info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - # Sending a second request to the same origin will reuse the existing IDLE connection. + # Sending a second request to the same origin will + # reuse the existing IDLE connection. response = pool.request("GET", "https://example.com/") assert response.status == 200 assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - +# unasync anyio def test_connection_pool_with_http2_goaway(): """ Test a connection pool with HTTP/2 requests, that cleanly disconnects @@ -239,7 +253,8 @@ def test_connection_pool_with_http2_goaway(): info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] # Sending a second request to the same origin will require a new connection. @@ -250,11 +265,12 @@ def test_connection_pool_with_http2_goaway(): info = [repr(c) for c in pool.connections] assert info == [ - "", + "", ] - +# unasync anyio def test_trace_request(): """ The 'trace' request extension allows for a callback function to inspect the @@ -296,7 +312,7 @@ def trace(name, kwargs): ] - +# unasync anyio def test_debug_request(caplog): """ The 'trace' request extension allows for a callback function to inspect the @@ -321,7 +337,8 @@ def test_debug_request(caplog): ( "httpcore.connection", logging.DEBUG, - "connect_tcp.started host='example.com' port=80 local_address=None timeout=None socket_options=None", + "connect_tcp.started host='example.com' port=80 local_address=None" + " timeout=None socket_options=None", ), ( "httpcore.connection", @@ -349,7 +366,8 @@ def test_debug_request(caplog): "httpcore.http11", logging.DEBUG, "receive_response_headers.complete return_value=" - "(b'HTTP/1.1', 200, b'OK', [(b'Content-Type', b'plain/text'), (b'Content-Length', b'13')])", + "(b'HTTP/1.1', 200, b'OK', [(b'Content-Type', b'plain/text')," + " (b'Content-Length', b'13')])", ), ( "httpcore.http11", @@ -364,7 +382,7 @@ def test_debug_request(caplog): ] - +# unasync anyio def test_connection_pool_with_http_exception(): """ HTTP/1.1 requests that result in an exception during the connection should @@ -381,7 +399,9 @@ def trace(name, kwargs): # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): pool.request( - "GET", "https://example.com/", extensions={"trace": trace} + "GET", + "https://example.com/", + extensions={"trace": trace}, ) info = [repr(c) for c in pool.connections] @@ -403,7 +423,7 @@ def trace(name, kwargs): ] - +# unasync anyio def test_connection_pool_with_connect_exception(): """ HTTP/1.1 requests that result in an exception during connection should not @@ -434,7 +454,9 @@ def trace(name, kwargs): # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): pool.request( - "GET", "https://example.com/", extensions={"trace": trace} + "GET", + "https://example.com/", + extensions={"trace": trace}, ) info = [repr(c) for c in pool.connections] @@ -446,7 +468,7 @@ def trace(name, kwargs): ] - +# unasync anyio def test_connection_pool_with_immediate_expiry(): """ Connection pools with keepalive_expiry=0.0 should immediately expire @@ -470,7 +492,8 @@ def test_connection_pool_with_immediate_expiry(): with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] response.read() @@ -480,7 +503,7 @@ def test_connection_pool_with_immediate_expiry(): assert info == [] - +# unasync anyio def test_connection_pool_with_no_keepalive_connections_allowed(): """ When 'max_keepalive_connections=0' is used, IDLE connections should not @@ -503,7 +526,8 @@ def test_connection_pool_with_no_keepalive_connections_allowed(): with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] response.read() @@ -513,7 +537,7 @@ def test_connection_pool_with_no_keepalive_connections_allowed(): assert info == [] - +# unasync trio def test_connection_pool_concurrency(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -550,15 +574,20 @@ def fetch(pool, domain, info_list): # Each connection was to a different host, and only sent a single # request on that connection. assert item[0] in [ - "", - "", - "", - "", - "", + "", + "", + "", + "", + "", ] - +# unasync trio def test_connection_pool_concurrency_same_domain_closing(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -594,13 +623,13 @@ def fetch(pool, domain, info_list): # single connection was established at any one time. assert len(item) == 1 # Only a single request was sent on each connection. - assert ( - item[0] - == "" + assert item[0] == ( + "" ) - +# unasync trio def test_connection_pool_concurrency_same_domain_keepalive(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -637,20 +666,25 @@ def fetch(pool, domain, info_list): assert len(item) == 1 # The connection sent multiple requests. assert item[0] in [ - "", - "", - "", - "", - "", + "", + "", + "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - +# unasync anyio def test_unsupported_protocol(): with httpcore.ConnectionPool() as pool: with pytest.raises(httpcore.UnsupportedProtocol): @@ -660,7 +694,7 @@ def test_unsupported_protocol(): pool.request("GET", "://www.example.com/") - +# unasync anyio def test_connection_pool_closed_while_request_in_flight(): """ Closing a connection pool while a request/response is still in-flight @@ -687,7 +721,7 @@ def test_connection_pool_closed_while_request_in_flight(): response.read() - +# unasync anyio def test_connection_pool_timeout(): """ Ensure that exceeding max_connections can cause a request to timeout. @@ -714,7 +748,7 @@ def test_connection_pool_timeout(): pool.request("GET", "https://example.com/", extensions=extensions) - +# unasync anyio def test_connection_pool_timeout_zero(): """ A pool timeout of 0 shouldn't raise a PoolTimeout if there's @@ -745,13 +779,17 @@ def test_connection_pool_timeout_zero(): # Two consecutive requests with a pool timeout of zero. # Both succeed without raising a timeout. response = pool.request( - "GET", "https://example.com/", extensions=extensions + "GET", + "https://example.com/", + extensions=extensions, ) assert response.status == 200 assert response.content == b"Hello, world!" response = pool.request( - "GET", "https://example.com/", extensions=extensions + "GET", + "https://example.com/", + extensions=extensions, ) assert response.status == 200 assert response.content == b"Hello, world!" @@ -776,7 +814,7 @@ def test_connection_pool_timeout_zero(): assert response.content == b"Hello, world!" - +# unasync anyio def test_http11_upgrade_connection(): """ HTTP "101 Switching Protocols" indicates an upgraded connection. diff --git a/tests/_sync/test_http11.py b/tests/_sync/test_http11.py index f2fa28f4..edd237c8 100644 --- a/tests/_sync/test_http11.py +++ b/tests/_sync/test_http11.py @@ -3,7 +3,7 @@ import httpcore - +# unasync anyio def test_http11_connection(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -26,13 +26,13 @@ def test_http11_connection(): assert not conn.is_closed() assert conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" - ) - + assert repr(conn) == ( + "" + ) # fmt: skip +# unasync anyio def test_http11_connection_unread_response(): """ If the client releases the response without reading it to termination, @@ -56,13 +56,13 @@ def test_http11_connection_unread_response(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) - +# unasync anyio def test_http11_connection_with_remote_protocol_error(): """ If a remote protocol error occurs, then no response will be returned, @@ -78,13 +78,13 @@ def test_http11_connection_with_remote_protocol_error(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) - +# unasync anyio def test_http11_connection_with_incomplete_response(): """ We should be gracefully handling the case where the connection ends prematurely. @@ -107,13 +107,13 @@ def test_http11_connection_with_incomplete_response(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) - +# unasync anyio def test_http11_connection_with_local_protocol_error(): """ If a local protocol error occurs, then no response will be returned, @@ -139,13 +139,13 @@ def test_http11_connection_with_local_protocol_error(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) - +# unasync anyio def test_http11_connection_handles_one_active_request(): """ Attempting to send a request while one is already in-flight will raise @@ -167,7 +167,7 @@ def test_http11_connection_handles_one_active_request(): conn.request("GET", "https://example.com/") - +# unasync anyio def test_http11_connection_attempt_close(): """ A connection can only be closed when it is idle. @@ -189,7 +189,7 @@ def test_http11_connection_attempt_close(): assert response.content == b"Hello, world!" - +# unasync anyio def test_http11_request_to_incorrect_origin(): """ A connection can only send requests to whichever origin it is connected to. @@ -201,7 +201,7 @@ def test_http11_request_to_incorrect_origin(): conn.request("GET", "https://other.com/") - +# unasync anyio def test_http11_expect_continue(): """ HTTP "100 Continue" is an interim response. @@ -234,7 +234,7 @@ def test_http11_expect_continue(): assert response.content == b"Hello, world!" - +# unasync anyio def test_http11_upgrade_connection(): """ HTTP "101 Switching Protocols" indicates an upgraded connection. @@ -269,7 +269,7 @@ def test_http11_upgrade_connection(): assert content == b"..." - +# unasync anyio def test_http11_upgrade_with_trailing_data(): """ HTTP "101 Switching Protocols" indicates an upgraded connection. @@ -320,7 +320,7 @@ def test_http11_upgrade_with_trailing_data(): network_stream.close() - +# unasync anyio def test_http11_early_hints(): """ HTTP "103 Early Hints" is an interim response. @@ -356,7 +356,7 @@ def test_http11_early_hints(): assert response.content == b"Hello, world! ..." - +# unasync anyio def test_http11_header_sub_100kb(): """ A connection should be able to handle a http header size up to 100kB. diff --git a/tests/_sync/test_http2.py b/tests/_sync/test_http2.py index 695359bd..b7ad5ae4 100644 --- a/tests/_sync/test_http2.py +++ b/tests/_sync/test_http2.py @@ -5,7 +5,7 @@ import httpcore - +# unasync anyio def test_http2_connection(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -40,13 +40,12 @@ def test_http2_connection(): assert ( conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1" ) - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) - +# unasync anyio def test_http2_connection_closed(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -82,7 +81,7 @@ def test_http2_connection_closed(): assert not conn.is_available() - +# unasync anyio def test_http2_connection_post_request(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -114,7 +113,7 @@ def test_http2_connection_post_request(): assert response.content == b"Hello, world!" - +# unasync anyio def test_http2_connection_with_remote_protocol_error(): """ If a remote protocol error occurs, then no response will be returned, @@ -127,7 +126,7 @@ def test_http2_connection_with_remote_protocol_error(): conn.request("GET", "https://example.com/") - +# unasync anyio def test_http2_connection_with_rst_stream(): """ If a stream reset occurs, then no response will be returned, @@ -173,7 +172,7 @@ def test_http2_connection_with_rst_stream(): assert response.status == 200 - +# unasync anyio def test_http2_connection_with_goaway(): """ If a GoAway frame occurs, then no response will be returned, @@ -223,7 +222,7 @@ def test_http2_connection_with_goaway(): conn.request("GET", "https://example.com/") - +# unasync anyio def test_http2_connection_with_flow_control(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -283,7 +282,7 @@ def test_http2_connection_with_flow_control(): assert response.content == b"100,000 bytes received" - +# unasync anyio def test_http2_connection_attempt_close(): """ A connection can only be closed when it is idle. @@ -318,7 +317,7 @@ def test_http2_connection_attempt_close(): conn.request("GET", "https://example.com/") - +# unasync anyio def test_http2_request_to_incorrect_origin(): """ A connection can only send requests to whichever origin it is connected to. @@ -330,7 +329,7 @@ def test_http2_request_to_incorrect_origin(): conn.request("GET", "https://other.com/") - +# unasync anyio def test_http2_remote_max_streams_update(): """ If the remote server updates the maximum concurrent streams value, we should diff --git a/tests/_sync/test_http_proxy.py b/tests/_sync/test_http_proxy.py index 2d66578e..38609deb 100644 --- a/tests/_sync/test_http_proxy.py +++ b/tests/_sync/test_http_proxy.py @@ -16,7 +16,7 @@ ) - +# unasync anyio def test_proxy_forwarding(): """ Send an HTTP request via a proxy. @@ -40,7 +40,8 @@ def test_proxy_forwarding(): with proxy.stream("GET", "http://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -48,13 +49,15 @@ def test_proxy_forwarding(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a forwarding proxy can only handle HTTP requests to the same origin. + # A connection on a forwarding proxy can only + # handle HTTP requests to the same origin. assert proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) @@ -69,7 +72,7 @@ def test_proxy_forwarding(): ) - +# unasync anyio def test_proxy_tunneling(): """ Send an HTTPS request via a proxy. @@ -95,7 +98,8 @@ def test_proxy_tunneling(): with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -103,13 +107,15 @@ def test_proxy_tunneling(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) @@ -151,7 +157,7 @@ def connect_tcp( return HTTP1ThenHTTP2Stream(list(self._buffer)) - +# unasync anyio def test_proxy_tunneling_http2(): """ Send an HTTP/2 request via a proxy. @@ -187,7 +193,8 @@ def test_proxy_tunneling_http2(): with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -195,13 +202,15 @@ def test_proxy_tunneling_http2(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) @@ -216,7 +225,7 @@ def test_proxy_tunneling_http2(): ) - +# unasync anyio def test_proxy_tunneling_with_403(): """ Send an HTTPS request via a proxy. @@ -237,7 +246,7 @@ def test_proxy_tunneling_with_403(): assert not proxy.connections - +# unasync anyio def test_proxy_tunneling_with_auth(): """ Send an authenticated HTTPS request via a proxy. diff --git a/tests/_sync/test_integration.py b/tests/_sync/test_integration.py index e3327e69..44502743 100644 --- a/tests/_sync/test_integration.py +++ b/tests/_sync/test_integration.py @@ -1,18 +1,18 @@ import ssl -import pytest +import pytest # noqa: F401 import httpcore - +# unasync anyio def test_request(httpbin): with httpcore.ConnectionPool() as pool: response = pool.request("GET", httpbin.url) assert response.status == 200 - +# unasync anyio def test_ssl_request(httpbin_secure): ssl_context = ssl.create_default_context() ssl_context.check_hostname = False @@ -22,7 +22,7 @@ def test_ssl_request(httpbin_secure): assert response.status == 200 - +# unasync anyio def test_extra_info(httpbin_secure): ssl_context = ssl.create_default_context() ssl_context.check_hostname = False diff --git a/tests/_sync/test_socks_proxy.py b/tests/_sync/test_socks_proxy.py index 2d39bb97..56d376e2 100644 --- a/tests/_sync/test_socks_proxy.py +++ b/tests/_sync/test_socks_proxy.py @@ -3,7 +3,7 @@ import httpcore - +# unasync anyio def test_socks5_request(): """ Send an HTTP request via a SOCKS proxy. @@ -32,7 +32,8 @@ def test_socks5_request(): with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -40,13 +41,15 @@ def test_socks5_request(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( httpcore.Origin(b"http", b"example.com", 80) ) @@ -61,7 +64,7 @@ def test_socks5_request(): ) - +# unasync anyio def test_authenticated_socks5_request(): """ Send an HTTP request via a SOCKS proxy. @@ -93,7 +96,8 @@ def test_authenticated_socks5_request(): with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -101,14 +105,15 @@ def test_authenticated_socks5_request(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - +# unasync anyio def test_socks5_request_connect_failed(): """ Attempt to send an HTTP request via a SOCKS proxy, resulting in a connect failure. @@ -137,7 +142,7 @@ def test_socks5_request_connect_failed(): assert not proxy.connections - +# unasync anyio def test_socks5_request_failed_to_provide_auth(): """ Attempt to send an HTTP request via an authenticated SOCKS proxy, @@ -157,15 +162,15 @@ def test_socks5_request_failed_to_provide_auth(): # Sending a request, which the proxy rejects with pytest.raises(httpcore.ProxyError) as exc_info: proxy.request("GET", "https://example.com/") - assert ( - str(exc_info.value) - == "Requested NO AUTHENTICATION REQUIRED from proxy server, but got USERNAME/PASSWORD." + assert str(exc_info.value) == ( + "Requested NO AUTHENTICATION REQUIRED from proxy server," + " but got USERNAME/PASSWORD." ) assert not proxy.connections - +# unasync anyio def test_socks5_request_incorrect_auth(): """ Attempt to send an HTTP request via an authenticated SOCKS proxy,