Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClientResponse.raise_for_status() raises an AssertionError for empty reason #3532

Closed
rhwlo opened this issue Jan 13, 2019 · 1 comment · Fixed by #3533

Comments

@rhwlo
Copy link
Contributor

commented Jan 13, 2019

Long story short

ClientResponse.raise_for_status() raises an AssertionError for a >= 400 status code if its reason is an empty string.

Expected behaviour

I expect ClientResponse.raise_for_status() to raise a ClientResponseError if it’s invoked on a ClientResponse with a status >= 400, and to either:

  • raise a ClientError (or some subclass of ClientError) if it’s called on a ClientResponse with a reason = None, regardless of the HTTP status code, if this can only happen when raise_for_status() has been called before start() has been called; or
  • convert a ClientResponse.reason of None into "" if None is a valid value for ClientResponse.reason

Actual behaviour

  • ClientResponse.raise_for_status() raises an AssertionError if it’s invoked on a ClientResponse when a status >= 400 and bool(reason) is False, which includes reason == ""
  • ClientResponse.raise_for_status() raises a ClientResponseError if it’s invoked on a ClientResponse with a status >= 400 if the reason is not falsey
  • ClientResponse.raise_for_status() raises no error if it’s invoked on a ClientResponse with a status < 400 and a reason = None

Steps to reproduce

A colleague and I uncovered this using aioresponses, which currently doesn’t set a reason on the ClientResponse at all – see pnuckowski/aioresponses#114.

You can reproduce this with the example client and server in this gist: https://gist.github.com/rhwlo/5ff3cb6bae8deb6abb4a1e527ad9583e

Your environment

I’m using the latest aiohttp from the repository, but this problem shows up only in v3.5.0 and newer.

@aio-libs-bot

This comment has been minimized.

Copy link
Collaborator

commented Jan 13, 2019

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #1177 (Static handler raises AssertionError), #156 (ClientResponse.content.read() raises ConnectionResetError), #1670 (ClientResponse ergonomics), #3248 (history tuple on ClientResponseError empty with raise_for_status() or ClientSession(raise_for_status=True)), and #1633 (Testing with pytest-aiohttp test_client raises an AssertionError.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.