lundberg / respx

Mock HTTPX with awesome request patterns and response side effects 🦋
https://lundberg.github.io/respx
BSD 3-Clause "New" or "Revised" License
581 stars 38 forks source link

Change to match in 0.20.2 breaks naive use of path #242

Closed commentator8 closed 11 months ago

commentator8 commented 11 months ago

Hi,

i used to write something like this:

url = 'https://www.instance_url.com/oauth_token.do'
my_route = respx.route(method="POST", path=url)
response = await httpx.post(url)

and due to the change in 0.20.2 (it worked in 0.20.1) where you now filter based on a white list that doesn't include :, the above will break (due to the https:) with respx.models.AllMockedAssertionError: RESPX: <Request('POST', 'https://www.instance_url.com/oauth_token.do')> not mocked!

The below however will work:

url = '/oauth_token.do'
host = 'www.instance_url.com'
my_route = respx.route(method="POST", host=host, path=url)
httpx.post('https://' + host + url)

I am not sure if the way i was writing was correct, but in any case it was a naive take and I imagine others could get caught in this as well. The docs show the below as the quickstart for the non route version (e.g. respx.get("https://foo.bar/").mock(return_value=Response(204))) and for the route give a host=a, path=b example.

lundberg commented 11 months ago

I am not sure if the way i was writing was correct

Not exactly .. the path pattern (kwarg) is for the path bit of the url that you want to match on.

To match full url with .route() you should use the url pattern, e.g.


url = 'https://www.instance_url.com/oauth_token.do'
my_route = respx.route(method="POST", url=url)
response = await httpx.post(url)```