lundberg / respx

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

Quoted params fail #209

Closed dpManuqb closed 2 years ago

dpManuqb commented 2 years ago

When I try an url with query quoted parameters, respx does not match the mocked url and raises "RESPX ... not mocked!"

from httpx import AsyncClient, Response
from urllib.parse import quote
import respx
import asyncio

@respx.mock
async def main():
    quoted_param = quote("""{"name": "something"}""")
    mock = respx.get(
        host="test", path="/path", params={"filter": quoted_param}).mock(
            return_value=Response(json={"nice": "job"}, status_code=200))

    print((await AsyncClient().get(url=f'http://test/path?filter={quoted_param}')).json())

if(__name__ == "__main__"):
    asyncio.run(main())
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1751, in get
    return await self.request(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1527, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1614, in send
    response = await self._send_handling_auth(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1642, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1679, in _send_handling_redirects
    response = await self._send_single_request(request)
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1716, in _send_single_request
    response = await transport.handle_async_request(request)
  File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
    resp = await self._pool.handle_async_request(req)
  File "/usr/local/lib/python3.9/site-packages/respx/mocks.py", line 188, in amock
    response = await cls._send_async_request(
  File "/usr/local/lib/python3.9/site-packages/respx/mocks.py", line 222, in _send_async_request
    httpx_response = await cls.async_handler(httpx_request)
  File "/usr/local/lib/python3.9/site-packages/respx/mocks.py", line 134, in async_handler
    raise assertion_error
  File "/usr/local/lib/python3.9/site-packages/respx/mocks.py", line 127, in async_handler
    httpx_response = await router.async_handler(httpx_request)
  File "/usr/local/lib/python3.9/site-packages/respx/router.py", line 319, in async_handler
    resolved = await self.aresolve(request)
  File "/usr/local/lib/python3.9/site-packages/respx/router.py", line 306, in aresolve
    break
  File "/usr/local/Cellar/python@3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py", line 126, in __exit__
    next(self.gen)
  File "/usr/local/lib/python3.9/site-packages/respx/router.py", line 251, in resolver
    raise AllMockedAssertionError(f"RESPX: {request!r} not mocked!")
respx.models.AllMockedAssertionError: RESPX: <Request('GET', 'http://test/path?filter=%7B%22name%22%3A%20%22something%22%7D')> not mocked!
dpManuqb commented 2 years ago

Solved, shouldn't quote if you add params in params parameter, it already quotes it

Change quoted_param = quote("""{"name": "something"}""") to quoted_param = """{"name": "something"}""" and it works

lundberg commented 2 years ago

Was writing the same answer atm 😂 ... i.e. the params kwarg should not be quoted.