getsentry / responses

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

Missing `request` attribute on the response #617

Closed vlsd closed 1 year ago

vlsd commented 1 year ago

Describe the bug

In the readme file, it says

You can use the matchers.query_param_matcher function to match against the params request parameter. Just use the same dictionary as you will use in params argument in request.

It even give an example of code where it runs an assert on resp.request.params. However, this doesn't seem to work in practice, as no matter what I try my response objects do not have a request attribute attached, and no clear way of accessing the query parameters.

I cannot tell if the documentation out of date, ahead of its time, or there's been am unexpected regression in the code.

Additional context

No response

Version of responses

0.22.0

Steps to Reproduce

import responses
import requests

@responses.activate
def test():
    resp = responses.get(url="https://www.test.com", json=[])
    requests.get("https://www.test.com", params={"foo": "bar"})

    print(resp.request.params)

if __name__ == "__main__":
    test()

Expected Result

The following should be printed

{"foo": "bar"}

Actual Result

*** AttributeError: 'Response' object has no attribute 'request'
beliaev-maksim commented 1 year ago

your example does not meet the criteria of minimal reproducible

vlsd commented 1 year ago

your example does not meet the criteria of minimal reproducible

Sorry, I typed it off the cuff. I updated it to be a proper python script.

beliaev-maksim commented 1 year ago

If you compare your example and one in readme, you may find a small difference

response should be returned from request

Also, if you are only interested in request parameters, you even don't need to explicitly compare them. Just use the matcher and it will do it for you.

vlsd commented 1 year ago

Oh, I see now, they just have similar variable names and I got confused. So this was never a feature. Thanks for clarifying!

It's still confusing as to why those assert statements were included in the example, they seem to simply show that the requests library works as expected.

Also, if you are only interested in request parameters, you even don't need to explicitly compare them. Just use the matcher and it will do it for you.

What I'm interested in is obtaining the request parameters when a different piece of code makes the request. In other words, I want to test that a certain endpoint was accessed and what parameters it was accessed with.

beliaev-maksim commented 1 year ago

@vlsd

to test that a certain endpoint was accessed and what parameters it was accessed with.

if you want to run a unittest against this scenario, then you need to know in advance what are the parameters that you are looking for and use the matcher. If no request with such parameters was fired, then it will raise an exception.

they seem to simply show that the requests library works as expected.

it is not. Since the response that you get, is the response generated by responses library and not by requests