0

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!

Philip Koch
  • 197
  • 1
  • 11

1 Answers1

0

I found the problem. I'm not sure why this happens, but the qs-parameter is not filled when using mock:// as schema for the requests. It works with http:// so I changed my mocked url to http://.

Here's an minimal example:

# works with http-schame
def test_requests_mocker():
    with requests_mock.Mocker() as m:
        url = "http://www.example.com"
        m.get(url=f"{url}/path", text="abc")
        resp = requests.get(url=f"{url}/path", params={"limit": 10})
        assert m.last_request.qs["limit"] == ["10"]

# mock-schema doesn't work -- KeyError: 'limit'
def test_requests_mocker_mock_schema():
    with requests_mock.Mocker() as m:
        url = "mock://www.example.com"
        m.get(url=f"{url}/path", text="abc")
        resp = requests.get(url=f"{url}/path", params={"limit": 10})
        assert m.last_request.qs["limit"] == ["10"]
Philip Koch
  • 197
  • 1
  • 11