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

Query string on the request object wrapper is erroneously empty #221

Open
vlsd opened this issue Jan 17, 2023 · 4 comments
Open

Query string on the request object wrapper is erroneously empty #221

vlsd opened this issue Jan 17, 2023 · 4 comments

Comments

@vlsd
Copy link

vlsd commented Jan 17, 2023

I'm trying to test out some pretty simple pagination, so I'm making a request using the following syntax:

response = requests.get(endpoint, params={'limit': 10})

and catching it using something like:

mocker.get(endpoint, [{'json': ['bar', 'foo'], 'status': 200}, {'json': [], 'status' 200)])

The request gets intercepted, but the mocker.last_request.qs attribute is empty. I was hoping it would hold {'limit': 10}. Is this a bug, or is there another way to get the query string from this request?

@jamielennox
Copy link
Owner

It seems like a bug. last_request should be the actual request and so should be interpretting the underlying object.

Can you write it as a test case?

@jamielennox
Copy link
Owner

So i can't reproduce the value not being present. Compared to your test case the qs is a list, because you can specify the same qs multiple times, and it's always a string but the value is definitely present.

import requests
import requests_mock


with requests_mock.mock() as m:
    url = 'http://www.example.com'
    l = 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']

If i change your test case to ['value'] it works as well.

@philkoch
Copy link

I came across the same issue, and it seems like the used schema is the culprit. I used the mock:// schema, which is mentioned in the documentation but when this schema is used the m.last_request.qs dict is always empty. Maybe params are handled differently based on the used schema?

# request_mock.mock() and http-schema works
def test_requests_mock():
    with requests_mock.mock() 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"]

# request_mock.Mocker() and http-schema works 
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"]

# requests_mock.Mocker() and mock-schema doesn't work
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"]

Thanks for your response @jamielennox, this helped me find out what the issue was for me.

@jamielennox
Copy link
Owner

That's interesting, it means requests is parsing qs differently based on the scheme of the URL? It's definitely not something we're handling in requests-mock.

mock:// is in the documentation as when i started the project i expected people to want to use it as an Adapter more and this was required. It really should get rewritten so that this use case is a more advanced one, but you know how it is finding time to do big documentation rewrites....

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

3 participants