Skip to content

Commit e650dc4

Browse files
refactor(tests): parametrize byte-type check and simplify assertions
Collapse the two duplicate test methods into one parametrized test, move the isinstance assertion outside the Stream context manager, drop the custom failure message, and use :class: RST roles in the changelog entry — all per code-review feedback.
1 parent c3f30a1 commit e650dc4

2 files changed

Lines changed: 7 additions & 19 deletions

File tree

CHANGES/12404.bugfix.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Fixed ``BodyPartReader.read()`` and ``BodyPartReader.read(decode=True)`` returning
2-
``bytearray`` instead of ``bytes``, violating the documented API contract.
2+
:class:`bytearray` instead of :class:`bytes`, violating the documented API contract.
33
-- by :user:`CrepuscularIRIS`.

tests/test_multipart.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,28 +185,16 @@ async def test_read(self) -> None:
185185
assert b"Hello, world!" == result
186186
assert obj.at_eof()
187187

188-
async def test_read_returns_bytes_not_bytearray(self) -> None:
189-
# Regression test for https://github.com/aio-libs/aiohttp/issues/12404
190-
# read() must return bytes (not bytearray) to honour the documented API contract.
191-
with Stream(b"Hello, world!\r\n--:") as stream:
192-
d = CIMultiDictProxy[str](CIMultiDict())
193-
obj = aiohttp.BodyPartReader(BOUNDARY, d, stream)
194-
result = await obj.read()
195-
assert isinstance(
196-
result, bytes
197-
), f"Expected bytes, got {type(result).__name__}"
198-
199-
async def test_read_decode_returns_bytes_not_bytearray(self) -> None:
188+
@pytest.mark.parametrize("kwargs", [{}, {"decode": True}])
189+
async def test_read_returns_bytes_not_bytearray(
190+
self, kwargs: dict[str, bool]
191+
) -> None:
200192
# Regression test for https://github.com/aio-libs/aiohttp/issues/12404
201-
# read(decode=True) must return bytes (not bytearray) even when no
202-
# Content-Transfer-Encoding / Content-Encoding header is present.
203193
with Stream(b"Hello, world!\r\n--:") as stream:
204194
d = CIMultiDictProxy[str](CIMultiDict())
205195
obj = aiohttp.BodyPartReader(BOUNDARY, d, stream)
206-
result = await obj.read(decode=True)
207-
assert isinstance(
208-
result, bytes
209-
), f"Expected bytes, got {type(result).__name__}"
196+
result = await obj.read(**kwargs)
197+
assert isinstance(result, bytes)
210198

211199
async def test_read_chunk_at_eof(self) -> None:
212200
with Stream(b"--:") as stream:

0 commit comments

Comments
 (0)