diff --git a/CHANGES/3532.bugfix b/CHANGES/3532.bugfix new file mode 100644 index 00000000000..6ee7b3b3d18 --- /dev/null +++ b/CHANGES/3532.bugfix @@ -0,0 +1,2 @@ +Raise a ClientResponseError instead of an AssertionError for a blank +HTTP Reason Phrase. \ No newline at end of file diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 1188ca1ccf1..5b7d89b81d5 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -120,6 +120,7 @@ Joel Watts Jon Nabozny Joongi Kim Josep Cugat +Joshu Coats Julia Tsemusheva Julien Duponchelle Jungkook Park diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index 846ce6f090c..8785605c708 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -932,7 +932,8 @@ def release(self) -> Any: def raise_for_status(self) -> None: if 400 <= self.status: - assert self.reason # always not None for started response + # reason should always be not None for a started response + assert self.reason is not None self.release() raise ClientResponseError( self.request_info, diff --git a/tests/test_client_response.py b/tests/test_client_response.py index cf203d844b2..f29f9e4d084 100644 --- a/tests/test_client_response.py +++ b/tests/test_client_response.py @@ -669,6 +669,24 @@ def test_raise_for_status_4xx() -> None: assert response.closed +def test_raise_for_status_4xx_without_reason() -> None: + response = ClientResponse('get', URL('http://def-cl-resp.org'), + request_info=mock.Mock(), + writer=mock.Mock(), + continue100=None, + timer=TimerNoop(), + traces=[], + loop=mock.Mock(), + session=mock.Mock()) + response.status = 404 + response.reason = '' + with pytest.raises(aiohttp.ClientResponseError) as cm: + response.raise_for_status() + assert str(cm.value.status) == '404' + assert str(cm.value.message) == '' + assert response.closed + + def test_resp_host() -> None: response = ClientResponse('get', URL('http://del-cl-resp.org'), request_info=mock.Mock(),