Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle HTTP/1.1 half-closed connections gracefully. #641

Merged
merged 13 commits into from
Jul 7, 2023
Prev Previous commit
Next Next commit
Fix 'test_write_error_but_response_sent' test case
  • Loading branch information
tomchristie committed Dec 13, 2022
commit 4185ebbf83641a4d500877466354b9cbbe44d1ac
14 changes: 12 additions & 2 deletions tests/_async/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WriteError,
)
from httpcore.backends.base import AsyncNetworkStream
from httpcore.backends.mock import AsyncMockBackend
from httpcore.backends.mock import AsyncMockBackend, AsyncMockStream


@pytest.mark.anyio
Expand Down Expand Up @@ -89,7 +89,7 @@ async def test_write_error_but_response_sent():
will raise a `ConnectionNotAvailable` exception.
"""

class ErrorOnRequestTooLarge(AsyncMockBackend):
class ErrorOnRequestTooLargeStream(AsyncMockStream):
def __init__(self, buffer: List[bytes], http2: bool = False) -> None:
super().__init__(buffer, http2)
self.count = 0
Expand All @@ -100,6 +100,16 @@ async def write(self, buffer: bytes, timeout: Optional[float] = None) -> None:
if self.count > 1_000_000:
raise WriteError()

class ErrorOnRequestTooLarge(AsyncMockBackend):
async def connect_tcp(
self,
host: str,
port: int,
timeout: Optional[float] = None,
local_address: Optional[str] = None,
) -> AsyncMockStream:
return ErrorOnRequestTooLargeStream(list(self._buffer), http2=self._http2)

origin = Origin(b"https", b"example.com", 443)
network_backend = ErrorOnRequestTooLarge(
[
Expand Down
14 changes: 12 additions & 2 deletions tests/_sync/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WriteError,
)
from httpcore.backends.base import NetworkStream
from httpcore.backends.mock import MockBackend
from httpcore.backends.mock import MockBackend, MockStream



Expand Down Expand Up @@ -89,7 +89,7 @@ def test_write_error_but_response_sent():
will raise a `ConnectionNotAvailable` exception.
"""

class ErrorOnRequestTooLarge(MockBackend):
class ErrorOnRequestTooLargeStream(MockStream):
def __init__(self, buffer: List[bytes], http2: bool = False) -> None:
super().__init__(buffer, http2)
self.count = 0
Expand All @@ -100,6 +100,16 @@ def write(self, buffer: bytes, timeout: Optional[float] = None) -> None:
if self.count > 1_000_000:
raise WriteError()

class ErrorOnRequestTooLarge(MockBackend):
def connect_tcp(
self,
host: str,
port: int,
timeout: Optional[float] = None,
local_address: Optional[str] = None,
) -> MockStream:
return ErrorOnRequestTooLargeStream(list(self._buffer), http2=self._http2)

origin = Origin(b"https", b"example.com", 443)
network_backend = ErrorOnRequestTooLarge(
[
Expand Down