diff --git a/CHANGES/8742.bugfix.rst b/CHANGES/8742.bugfix.rst new file mode 100644 index 00000000000..850f0390399 --- /dev/null +++ b/CHANGES/8742.bugfix.rst @@ -0,0 +1 @@ +Fixed :meth:`aiohttp.ClientResponse.json()` not setting ``status`` when :exc:`aiohttp.ContentTypeError` is raised -- by :user:`bdraco`. diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index 2c10da4ff81..bea76d84c39 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -1199,6 +1199,7 @@ async def json( raise ContentTypeError( self.request_info, self.history, + status=self.status, message=( "Attempt to decode JSON with " "unexpected mimetype: %s" % ctype ), diff --git a/tests/test_client_response.py b/tests/test_client_response.py index 166089cc84a..628e3d71b92 100644 --- a/tests/test_client_response.py +++ b/tests/test_client_response.py @@ -659,11 +659,13 @@ async def test_json_invalid_content_type(loop, session) -> None: ) response._headers = {"Content-Type": "data/octet-stream"} response._body = b"" + response.status = 500 with pytest.raises(aiohttp.ContentTypeError) as info: await response.json() assert info.value.request_info == response.request_info + assert info.value.status == 500 async def test_json_no_content(loop, session) -> None: