Skip to content

Commit

Permalink
Permit empty reason strings in ClientResponse.raise_for_status() (#3533)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhwlo authored and asvetlov committed Jan 14, 2019
1 parent 0e60406 commit f590bfd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES/3532.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise a ClientResponseError instead of an AssertionError for a blank
HTTP Reason Phrase.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Joel Watts
Jon Nabozny
Joongi Kim
Josep Cugat
Joshu Coats
Julia Tsemusheva
Julien Duponchelle
Jungkook Park
Expand Down
3 changes: 2 additions & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit f590bfd

Please sign in to comment.