Skip to content

Consider removing assert about HTTP Reason-Phrase being always present #3906

Closed
@WouldYouKindly

Description

Hi!

aiohttp has the following code:

def raise_for_status(self) -> None:
    if 400 <= self.status:
        assert self.reason  # always not None for started response
        self.release()
        raise ClientResponseError(
            self.request_info,
            self.history,
            status=self.status,
            message=self.reason,
            headers=self.headers)

This assertion fails if the server omits Reason-Phrase, i.e. if response looks like HTTP/1.1 404 (no Not Found).

HTTP spec is not clear about whether reason must be provided, but e.g. this answer suggests that it may not be: https://stackoverflow.com/questions/17517086/can-an-http-response-omit-the-reason-phrase

Maybe aiohttp should be more permissive in this case. For me, the problem is that hoverfly drops the reason phrase, which leads to aiohttp raising AssertionError. Reason can be set to an empty string if it is not found in response.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions