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

PreparedRequest instances are modified by responses #738

Open
Colin-b opened this issue Sep 9, 2024 · 4 comments
Open

PreparedRequest instances are modified by responses #738

Colin-b opened this issue Sep 9, 2024 · 4 comments

Comments

@Colin-b
Copy link

Colin-b commented Sep 9, 2024

Describe the bug

As you can see in

request.params = self._parse_request_params(request.path_url) # type: ignore[attr-defined]
responses, is modifying PreparedRequest instances to incorporate additional attributes.
This is fine in the context of responses, this is not however when this data is returned to the clients.

Could you modify responses so that the behavior from a client perspective is the one of requests (ie: no additional attributes in objects) ?

Thanks again

Additional context

In the context of writing tests against HTTP behavior that cannot be reproduced with a real live scenario (such as an HTTP failure response from the server), I am trusting my test suite to be able to handle those scenarios and if I see that a PreparedRequest contains an attribute, I assume it will exists in production code as well the day such issue will occur (could be years after deployment time in such scenarios).

Version of responses

0.25.3

Steps to Reproduce

import requests
import responses

def test_params_in_prepared_request():
    responses.get("https://the_test.url", status=403)
    response = requests.get("https://the_test.url?param=value")
    try:
        response.raise_for_status()
    except requests.RequestException as e:
        print(e.request.params)  # Without responses, an AttributeError will be raise ('PreparedRequest' object has no attribute 'params')

Expected Result

I expect the same failure as in production in my test run

Actual Result

The AttributeError is not raised when accessing params on a PreparedRequest

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Sep 9, 2024
@markstory
Copy link
Member

Could you modify responses so that the behavior from a client perspective is the one of requests (ie: no additional attributes in objects) ?

So where would you put the additional state that responses needs and do it in a backwards compatible way?

@patrickhodel
Copy link

The way these params are modified should be considered a bug:

if len(values) == 1:
values = values[0] # type: ignore[assignment]

This means it is not possible to mock an api that returns a JSON list with one item.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Nov 11, 2024
@beliaev-maksim
Copy link
Collaborator

@patrickhodel you started with an issue that you access an attribute that does not exist. In general, any linting tool should have caught it on your side, since requests do not have this attribute

your second comment describes something else, completely unrelated.
Please provide minimal viable example that shows that responses modifies some parameters in the incompatible way.

This particular piece of code exists for more than 4 years and never caused any issue

@patrickhodel
Copy link

@beliaev-maksim Thanks for your quick feedback. Upon further debugging and trying to create a minimal viable example I discovered that some other code in our environment does the same kind of conversion to the response as in the snippet I posted. Sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

5 participants