jamielennox / requests-mock

Mocked responses for the requests library
https://requests-mock.readthedocs.io
Apache License 2.0
450 stars 71 forks source link

Response reason should be set based on status_code #261

Open soxofaan opened 4 months ago

soxofaan commented 4 months ago

(Instead of piggybacking on the old #118, It thought it was better to start a new ticket)

PR #126 tried to address #118, but I think that fix never worked, I still get this with latest requests-mock 1.12.1:

url = "https://example.com/notfound"
with requests_mock.Mocker() as m:
    m.get(url, status_code=404)
    requests.get(url).raise_for_status()

HTTPError: 404 Client Error: None for url: https://example.com/notfound

Note the "None" instead of "Not Found"

soxofaan commented 4 months ago

126 basically added this:

https://github.com/jamielennox/requests-mock/blob/9742d02a8cad17276dbeba7b300b6d27ae1b6fb1/requests_mock/response.py#L188

But in reality that fallback value is never used because the "reason" is actually set (e.g. to None unless user specified something else), as create_response is called roughly like this:

context = _Context(...
    self._params.get('reason'),
)
...
return create_response(...
    reason=context.reason,

I guess a better fix is to also add a fallback value to that self._params.get('reason') call