From 24aff29918defece1d43a6fa7ee02af298901f09 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Tue, 15 Jan 2019 09:00:20 +0200 Subject: [PATCH] [3.5] Permit empty reason strings in ClientResponse.raise_for_status() (#3533) (#3541) (cherry picked from commit f590bfd) Co-authored-by: Joshu Coats --- CHANGES/3532.bugfix | 2 ++ CONTRIBUTORS.txt | 1 + aiohttp/client_reqrep.py | 3 ++- tests/test_client_response.py | 18 ++++++++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 CHANGES/3532.bugfix 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 27b7256935d..abe99c26c09 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 c61998de8b7..c5861fa955f 100644 --- a/tests/test_client_response.py +++ b/tests/test_client_response.py @@ -668,6 +668,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(),