I have a function which makes a GET-request with a dict of params. In my unit-tests I want to make sure that the parameters are set correctly. However when I try to use requests-mock
to mock the request, I can only check for the URL without the parameters. It seems like the params are not picked up by requests-mock.
Example Code:
import requests as req
def make_request():
url = "mock://some.url/media"
headers = {"Authorization": "Bearer ..."}
params = {"since": 0, "until": 1}
req.get(url, params=params, headers=self.headers, timeout=30)
Example Test:
import requests_mock
def my_test():
auth_headers = {"Authorization": "Bearer ..."}
query_params = "?since=0&until=1"
expected_url = f"mock://some.url/media{query_params}" # no mock address
with requests_mock.Mocker() as mocker:
mocker.get(
expected_url,
headers=auth_headers,
text="media_found",
)
response = make_request()
assert response.text == "media_found"
# assert mocker.last_request.params == query_params
This is what would've made sense to me. However, as long as I use the query_params
in expected_url
I get a No Mock Address
error.
When I'm using the URL without the query_params
the test runs fine, but doesn't check that the query params are set correctly. I found the query-string-matcher in the documentation, but didn't get it to work. The No Mock Address
error kept coming up.
I also tried debugging the request_history
-object provided by requests mock, but there was no params/query field set in there.
_url_parts: ParseResult(scheme='mock', netloc='some.url', path='/media', params='', query='', fragment='')
_url_parts_: ParseResult(scheme='mock', netloc='some.url', path='/media', params='', query='', fragment='')
This was surprising, because when doing the actual request outside of the code, the params are added correctly.
Can someone point me in the right direction on how I can check the GET-query-parameters with requests-mock
without creating a spy on the req.get()
function and checking the parameters the function was called with?
Thanks!