Skip to content

Commit f590bfd

Browse files
rhwloasvetlov
authored andcommitted
Permit empty reason strings in ClientResponse.raise_for_status() (#3533)
1 parent 0e60406 commit f590bfd

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

CHANGES/3532.bugfix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise a ClientResponseError instead of an AssertionError for a blank
2+
HTTP Reason Phrase.

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Joel Watts
120120
Jon Nabozny
121121
Joongi Kim
122122
Josep Cugat
123+
Joshu Coats
123124
Julia Tsemusheva
124125
Julien Duponchelle
125126
Jungkook Park

aiohttp/client_reqrep.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,8 @@ def release(self) -> Any:
932932

933933
def raise_for_status(self) -> None:
934934
if 400 <= self.status:
935-
assert self.reason # always not None for started response
935+
# reason should always be not None for a started response
936+
assert self.reason is not None
936937
self.release()
937938
raise ClientResponseError(
938939
self.request_info,

tests/test_client_response.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,24 @@ def test_raise_for_status_4xx() -> None:
669669
assert response.closed
670670

671671

672+
def test_raise_for_status_4xx_without_reason() -> None:
673+
response = ClientResponse('get', URL('http://def-cl-resp.org'),
674+
request_info=mock.Mock(),
675+
writer=mock.Mock(),
676+
continue100=None,
677+
timer=TimerNoop(),
678+
traces=[],
679+
loop=mock.Mock(),
680+
session=mock.Mock())
681+
response.status = 404
682+
response.reason = ''
683+
with pytest.raises(aiohttp.ClientResponseError) as cm:
684+
response.raise_for_status()
685+
assert str(cm.value.status) == '404'
686+
assert str(cm.value.message) == ''
687+
assert response.closed
688+
689+
672690
def test_resp_host() -> None:
673691
response = ClientResponse('get', URL('http://del-cl-resp.org'),
674692
request_info=mock.Mock(),

0 commit comments

Comments
 (0)