From 9130938b8ef24bceed1cfe930229f678f70b5ec9 Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 18 Oct 2026 10:56:07 +0800 Subject: [PATCH] fix(python): preserve streamed body read errors --- impit-python/src/response.rs | 4 ++-- impit-python/test/async_client_test.py | 15 +++++++++++++++ impit-python/test/basic_client_test.py | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/impit-python/src/response.rs b/impit-python/src/response.rs index c23c6163..49e833dc 100644 --- a/impit-python/src/response.rs +++ b/impit-python/src/response.rs @@ -411,7 +411,7 @@ impl ImpitPyResponse { let content = response .bytes() .await - .map_err(|_| ImpitPyError(impit::errors::ImpitError::NetworkError))?; + .map_err(|error| ImpitPyError(ImpitError::from(error, None)))?; Ok(Python::attach(|py| { let mut slf_ref = slf.borrow_mut(py); @@ -528,7 +528,7 @@ impl ImpitPyResponse { response .bytes() .await - .map_err(|_| ImpitPyError(impit::errors::ImpitError::NetworkError)) + .map_err(|error| ImpitPyError(ImpitError::from(error, None))) }) })?; diff --git a/impit-python/test/async_client_test.py b/impit-python/test/async_client_test.py index 459fd8df..eb7c98df 100644 --- a/impit-python/test/async_client_test.py +++ b/impit-python/test/async_client_test.py @@ -795,6 +795,21 @@ async def test_iter_bytes_without_consumed(self, browser: Browser) -> None: with pytest.raises(StreamClosed): _ = response.content + async def test_read_truncated_body_raises_remote_protocol_error(self, browser: Browser) -> None: + port_holder = [0] + thread = threading.Thread(target=truncating_server, args=(port_holder,)) + thread.start() + await asyncio.sleep(0.1) + + impit = AsyncClient(browser=browser) + + async with impit.stream('GET', f'http://localhost:{port_holder[0]}/', timeout=5) as response: + assert response.status_code == 200 + with pytest.raises(RemoteProtocolError): + await response.aread() + + thread.join() + async def test_truncated_stream_raises(self, browser: Browser) -> None: port_holder = [0] thread = threading.Thread(target=truncating_server, args=(port_holder,)) diff --git a/impit-python/test/basic_client_test.py b/impit-python/test/basic_client_test.py index 795981ec..d8b95bbe 100644 --- a/impit-python/test/basic_client_test.py +++ b/impit-python/test/basic_client_test.py @@ -721,6 +721,21 @@ def test_truncated_stream_raises(self, browser: Browser) -> None: thread.join() + def test_read_truncated_body_raises_remote_protocol_error(self, browser: Browser) -> None: + port_holder = [0] + thread = threading.Thread(target=truncating_server, args=(port_holder,)) + thread.start() + time.sleep(0.1) + + impit = Client(browser=browser) + + with impit.stream('GET', f'http://localhost:{port_holder[0]}/', timeout=5) as response: + assert response.status_code == 200 + with pytest.raises(RemoteProtocolError): + response.read() + + thread.join() + def make_slow_server(port_holder: list[int], delay: float = 2.0) -> None: """Start a server in a daemon thread that waits `delay` seconds before responding."""