getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.17k stars 355 forks source link

raise_for_status() does not return the response #723

Closed tuky closed 3 months ago

tuky commented 5 months ago

Describe the bug

Look at https://3.python-requests.org/user/quickstart/ to see, that this method actually return the response in case of no error for easy method chaining:

image

The mocked response however always return None breaking the code during testing.

Additional context

No response

Version of responses

0.25.3

Steps to Reproduce

response.raise_for_status().json()

Expected Result

the mocked response

Actual Result

None

dj-wasabi commented 3 months ago

@tuky Where you able to get a work around for the raise_for_status()? I am currently having the problem that my tests keeps breaking, so I am almost at the point of removing the r.raise_for_status() from my code and do some other magic.

tuky commented 3 months ago

It's actually not an issue. The linked docs are not the correct ones. These are the right ones: https://requests.readthedocs.io/en/latest/api/#requests.Response.raise_for_status. And indeed, raise_for_status does not return the successful response.

dj-wasabi commented 3 months ago

Hi @tuky

(Sorry for this) But I have this code:

class AdminApi:
    ...
    def login(self, **kwargs):
        r = self.request._get(url=self.ai_api_url + "/login", **kwargs)
        r.raise_for_status()
        return r

But when running the pytest, it fails:

@responses.activate
def test_fews_login_faiure():
    """Test the logout."""
    responses.add(responses.GET, 'http://localhost:8080/api/v1/logi', body="REQUESTEDIDFROMAUTH", status=200)

    myRequest = my.http.request(debug=True)
    myApp = fews.api.AdminApi(request=myRequest, ai_api_url="http://localhost:8080/api/v1")
    myOutput = myApp.login(username="username", password="password")

    assert myOutput.text == 'blabla'

I get the error executing the pytest command:

>           r.raise_for_status()
E           AttributeError: 'dict' object has no attribute 'raise_for_status'

So I am not sure how I would use responses to test my code?

beliaev-maksim commented 3 months ago

it feels like a black magic is happening under the hood of self.request._get()

dj-wasabi commented 3 months ago

it feels like a black magic is happening under the hood of self.request._get()

Hmm, interesting. Will check that, thank you @beliaev-maksim !