diff --git a/tests/_async/test_connection.py b/tests/_async/test_connection.py index b5093c28..414c3edf 100644 --- a/tests/_async/test_connection.py +++ b/tests/_async/test_connection.py @@ -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 192cd769..0dc8b485 100644 --- a/tests/_async/test_connection_pool.py +++ b/tests/_async/test_connection_pool.py @@ -399,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] @@ -452,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] @@ -775,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 db34011a..90b213bf 100644 --- a/tests/_async/test_http11.py +++ b/tests/_async/test_http11.py @@ -29,7 +29,7 @@ async def test_http11_connection(): assert repr(conn) == ( "" - ) + ) # fmt: skip @pytest.mark.anyio diff --git a/tests/_async/test_http2.py b/tests/_async/test_http2.py index fe1c1516..45658348 100644 --- a/tests/_async/test_http2.py +++ b/tests/_async/test_http2.py @@ -41,8 +41,7 @@ async def test_http2_connection(): conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1" ) assert repr(conn) == ( - "" + "" ) diff --git a/tests/_sync/test_connection.py b/tests/_sync/test_connection.py index c84a882b..fbae0630 100644 --- a/tests/_sync/test_connection.py +++ b/tests/_sync/test_connection.py @@ -19,6 +19,7 @@ ) +# unasync anyio def test_http_connection(): origin = Origin(b"https", b"example.com", 443) network_backend = MockBackend( @@ -60,6 +61,7 @@ def test_http_connection(): ) +# unasync anyio def test_concurrent_requests_not_available_on_http11_connections(): """ Attempting to issue a request against an already active HTTP/1.1 connection @@ -84,6 +86,7 @@ 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(): """ @@ -99,7 +102,11 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: super().__init__(buffer, http2) self.count = 0 - def write(self, buffer: bytes, timeout: typing.Optional[float] = None) -> None: + def write( + self, + buffer: bytes, + timeout: typing.Optional[float] = None, + ) -> None: self.count += len(buffer) if self.count > 1_000_000: @@ -136,6 +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(): """ @@ -150,7 +158,11 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: super().__init__(buffer, http2) self.count = 0 - def write(self, buffer: bytes, timeout: typing.Optional[float] = None) -> None: + def write( + self, + buffer: bytes, + timeout: typing.Optional[float] = None, + ) -> None: self.count += len(buffer) if self.count > 1_000_000: @@ -179,6 +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) @@ -203,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/") @@ -212,13 +227,17 @@ 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. """ origin = Origin(b"https", b"example.com", 443) network_backend = MockBackend([]) - with HTTPConnection(origin=origin, network_backend=network_backend) as conn: + with HTTPConnection( + origin=origin, + network_backend=network_backend, + ) as conn: with pytest.raises(RuntimeError): conn.request("GET", "https://other.com/") @@ -253,14 +272,26 @@ def connect_tcp( return self._NeedsRetryAsyncNetworkStream(self, stream) class _NeedsRetryAsyncNetworkStream(NetworkStream): - def __init__(self, backend: "NeedsRetryBackend", stream: NetworkStream) -> None: + def __init__( + self, + backend: "NeedsRetryBackend", + stream: NetworkStream, + ) -> None: self._backend = backend self._stream = stream - def read(self, max_bytes: int, timeout: typing.Optional[float] = None) -> bytes: + def read( + 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) -> None: + def write( + self, + buffer: bytes, + timeout: typing.Optional[float] = None, + ) -> None: self._stream.write(buffer, timeout) def close(self) -> None: @@ -283,6 +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 = [ @@ -309,6 +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 = [ @@ -339,6 +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 1a296ac1..c115c4dd 100644 --- a/tests/_sync/test_connection_pool.py +++ b/tests/_sync/test_connection_pool.py @@ -9,6 +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. @@ -113,6 +114,7 @@ def test_connection_pool_with_keepalive(): ) +# unasync anyio def test_connection_pool_with_close(): """ HTTP/1.1 requests that include a 'Connection: Close' header should @@ -146,6 +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. @@ -210,6 +213,7 @@ def test_connection_pool_with_http2(): ] +# unasync anyio def test_connection_pool_with_http2_goaway(): """ Test a connection pool with HTTP/2 requests, that cleanly disconnects @@ -266,6 +270,7 @@ def test_connection_pool_with_http2_goaway(): ] +# unasync anyio def test_trace_request(): """ The 'trace' request extension allows for a callback function to inspect the @@ -307,6 +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 @@ -376,6 +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 @@ -391,7 +398,11 @@ def trace(name, kwargs): with httpcore.ConnectionPool(network_backend=network_backend) as pool: # 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}) + pool.request( + "GET", + "https://example.com/", + extensions={"trace": trace}, + ) info = [repr(c) for c in pool.connections] assert info == [] @@ -412,6 +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 @@ -441,7 +453,11 @@ def trace(name, kwargs): with httpcore.ConnectionPool(network_backend=network_backend) as pool: # 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}) + pool.request( + "GET", + "https://example.com/", + extensions={"trace": trace}, + ) info = [repr(c) for c in pool.connections] assert info == [] @@ -452,6 +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 @@ -486,6 +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 @@ -519,6 +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 @@ -568,6 +587,7 @@ def fetch(pool, domain, info_list): ] +# unasync trio def test_connection_pool_concurrency_same_domain_closing(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -609,6 +629,7 @@ def fetch(pool, domain, info_list): ) +# unasync trio def test_connection_pool_concurrency_same_domain_keepalive(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -663,6 +684,7 @@ def fetch(pool, domain, info_list): ) +# unasync anyio def test_unsupported_protocol(): with httpcore.ConnectionPool() as pool: with pytest.raises(httpcore.UnsupportedProtocol): @@ -672,6 +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 @@ -698,6 +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. @@ -724,6 +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 @@ -753,11 +778,19 @@ def test_connection_pool_timeout_zero(): ) as pool: # Two consecutive requests with a pool timeout of zero. # Both succeed without raising a timeout. - response = pool.request("GET", "https://example.com/", extensions=extensions) + response = pool.request( + "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) + response = pool.request( + "GET", + "https://example.com/", + extensions=extensions, + ) assert response.status == 200 assert response.content == b"Hello, world!" @@ -781,6 +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 c6486b80..de4bebf9 100644 --- a/tests/_sync/test_http11.py +++ b/tests/_sync/test_http11.py @@ -3,6 +3,7 @@ import httpcore +# unasync anyio def test_http11_connection(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -26,10 +27,12 @@ def test_http11_connection(): assert conn.is_available() assert not conn.has_expired() assert repr(conn) == ( - "" - ) + "" + ) # fmt: skip +# unasync anyio def test_http11_connection_unread_response(): """ If the client releases the response without reading it to termination, @@ -59,6 +62,7 @@ def test_http11_connection_unread_response(): ) +# unasync anyio def test_http11_connection_with_remote_protocol_error(): """ If a remote protocol error occurs, then no response will be returned, @@ -80,6 +84,7 @@ def test_http11_connection_with_remote_protocol_error(): ) +# unasync anyio def test_http11_connection_with_incomplete_response(): """ We should be gracefully handling the case where the connection ends prematurely. @@ -108,6 +113,7 @@ def test_http11_connection_with_incomplete_response(): ) +# unasync anyio def test_http11_connection_with_local_protocol_error(): """ If a local protocol error occurs, then no response will be returned, @@ -139,6 +145,7 @@ def test_http11_connection_with_local_protocol_error(): ) +# unasync anyio def test_http11_connection_handles_one_active_request(): """ Attempting to send a request while one is already in-flight will raise @@ -160,6 +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. @@ -181,6 +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. @@ -192,6 +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. @@ -224,6 +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. @@ -258,6 +269,7 @@ def test_http11_upgrade_connection(): assert content == b"..." +# unasync anyio def test_http11_early_hints(): """ HTTP "103 Early Hints" is an interim response. @@ -293,6 +305,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 576b5ff6..b7ad5ae4 100644 --- a/tests/_sync/test_http2.py +++ b/tests/_sync/test_http2.py @@ -5,6 +5,7 @@ import httpcore +# unasync anyio def test_http2_connection(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -40,10 +41,11 @@ def test_http2_connection(): conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1" ) assert repr(conn) == ( - "" + "" ) +# unasync anyio def test_http2_connection_closed(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -79,6 +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( @@ -110,6 +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, @@ -122,6 +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, @@ -167,6 +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, @@ -216,6 +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( @@ -275,6 +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. @@ -309,6 +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. @@ -320,6 +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 0db03ee9..38609deb 100644 --- a/tests/_sync/test_http_proxy.py +++ b/tests/_sync/test_http_proxy.py @@ -16,6 +16,7 @@ ) +# unasync anyio def test_proxy_forwarding(): """ Send an HTTP request via a proxy. @@ -71,6 +72,7 @@ def test_proxy_forwarding(): ) +# unasync anyio def test_proxy_tunneling(): """ Send an HTTPS request via a proxy. @@ -155,6 +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. @@ -222,6 +225,7 @@ def test_proxy_tunneling_http2(): ) +# unasync anyio def test_proxy_tunneling_with_403(): """ Send an HTTPS request via a proxy. @@ -242,6 +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 ca5a2d9a..44502743 100644 --- a/tests/_sync/test_integration.py +++ b/tests/_sync/test_integration.py @@ -5,12 +5,14 @@ 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 @@ -20,6 +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 a0652130..56d376e2 100644 --- a/tests/_sync/test_socks_proxy.py +++ b/tests/_sync/test_socks_proxy.py @@ -3,6 +3,7 @@ import httpcore +# unasync anyio def test_socks5_request(): """ Send an HTTP request via a SOCKS proxy. @@ -63,6 +64,7 @@ def test_socks5_request(): ) +# unasync anyio def test_authenticated_socks5_request(): """ Send an HTTP request via a SOCKS proxy. @@ -111,6 +113,7 @@ def test_authenticated_socks5_request(): 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. @@ -139,6 +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, @@ -166,6 +170,7 @@ def test_socks5_request_failed_to_provide_auth(): assert not proxy.connections +# unasync anyio def test_socks5_request_incorrect_auth(): """ Attempt to send an HTTP request via an authenticated SOCKS proxy, diff --git a/unasync.py b/unasync.py index b6a54b96..1ed8ece4 100755 --- a/unasync.py +++ b/unasync.py @@ -24,8 +24,8 @@ ("__aenter__", "__enter__"), ("__aexit__", "__exit__"), ("__aiter__", "__iter__"), - ("@pytest.mark.anyio", ""), - ("@pytest.mark.trio", ""), + ("@pytest.mark.anyio", "# unasync anyio"), + ("@pytest.mark.trio", "# unasync trio"), ("AutoBackend", "SyncBackend"), ] COMPILED_SUBS = [