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

Response with query string, but match_querystring=False should raise a warning #211

Closed
rbu opened this issue Jul 27, 2018 · 5 comments
Closed

Comments

@rbu
Copy link
Contributor

rbu commented Jul 27, 2018

Problem

I've debugged an unrelated part of the an application for hours before noticing that mocked responses were matched unexpectedly, due to me not seeing the default match_querystring=False. A naive test case might do something like this:

$ python
Python 3.6.6 (default, Jul 19 2018, 14:25:17) 
[GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import responses
>>> import requests
>>> 
>>> responses.start()
>>> 
>>> responses.add(responses.GET, "https://example.com/api?endpoint=1",
...     json={'endpoint': 1}
... )
>>> responses.add(responses.GET, "https://example.com/api?endpoint=2",
...     json={'endpoint': 2}
... )
>>> 
>>> requests.get('https://example.com/api?endpoint=2').content
b'{"endpoint": 1}'
>>> requests.get('https://example.com/api?endpoint=1').content
b'{"endpoint": 2}'
>>> 

Proposal

While I'd prefer the default for match_querystring to be changed, I'd propose responses throws a RuntimeWarning when a response is added that contains a query string without setting match_querystring explicitly.

@rbu
Copy link
Contributor Author

rbu commented Jul 27, 2018

Another example of someone stumbling over this is #203

@dcramer
Copy link
Member

dcramer commented Jul 27, 2018

Not opposed to that. I'd also be fine just changing match_querystring=True (implicit only) when you pass a URL with a querystring.

@rbu
Copy link
Contributor Author

rbu commented Jul 27, 2018

@dcramer I like the idea of a giving match_querystring a new default value that depends on the url. Is this something you'd be working on? If not, I'd look into submitting a PR for this next week.

@dcramer
Copy link
Member

dcramer commented Jul 27, 2018

I probably won’t have time, but if you throw up a PR I’m happy to review/merge

rbu added a commit to rbu/responses that referenced this issue Aug 3, 2018
Previously, adding a response with a query string without also setting
match_querystring=True would provide confusing results, see getsentry#211.

This commit changes the argument's default value to consider the URL and
automatically enable match_querystring if a query string is detected in
the response.
@rbu
Copy link
Contributor Author

rbu commented Aug 3, 2018

@dcramer I have added a pull request for the automatic behavior you proposed in #213.
I'd very much appreciate your review.

dcramer pushed a commit that referenced this issue Aug 3, 2018
Previously, adding a response with a query string without also setting
match_querystring=True would provide confusing results, see #211.

This commit changes the argument's default value to consider the URL and
automatically enable match_querystring if a query string is detected in
the response.
@dcramer dcramer closed this as completed Aug 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants