diff --git a/CHANGES/5343.bugfix b/CHANGES/5343.bugfix new file mode 100644 index 00000000000..4e33071ea94 --- /dev/null +++ b/CHANGES/5343.bugfix @@ -0,0 +1 @@ +Fixed StreamResponse.prepared to return True after EOF is sent -- by :user:`arthurdarcet`. diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index a0ee89d6706..238c31277bc 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -121,7 +121,7 @@ def __init__( @property def prepared(self) -> bool: - return self._payload_writer is not None + return self._eof_sent or self._payload_writer is not None @property def task(self) -> "Optional[asyncio.Task[None]]": diff --git a/tests/test_web_response.py b/tests/test_web_response.py index f90eb4b1153..d1226f27763 100644 --- a/tests/test_web_response.py +++ b/tests/test_web_response.py @@ -734,11 +734,8 @@ async def test___repr___after_eof() -> None: resp = StreamResponse() await resp.prepare(make_request("GET", "/")) - assert resp.prepared - await resp.write(b"data") await resp.write_eof() - assert not resp.prepared resp_repr = repr(resp) assert resp_repr == "" @@ -1110,14 +1107,22 @@ def test_content_type_with_set_body() -> None: assert resp.content_type == "application/octet-stream" -def test_started_when_not_started() -> None: +def test_prepared_when_not_started() -> None: resp = StreamResponse() assert not resp.prepared -async def test_started_when_started() -> None: +async def test_prepared_when_started() -> None: + resp = StreamResponse() + await resp.prepare(make_request("GET", "/")) + assert resp.prepared + + +async def test_prepared_after_eof() -> None: resp = StreamResponse() await resp.prepare(make_request("GET", "/")) + await resp.write(b"data") + await resp.write_eof() assert resp.prepared