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

Passing Parameters In URLs #225

Open
tsaavik opened this issue Apr 16, 2023 · 4 comments
Open

Passing Parameters In URLs #225

tsaavik opened this issue Apr 16, 2023 · 4 comments

Comments

@tsaavik
Copy link

tsaavik commented Apr 16, 2023

Requests allows you to pass the url parameters in as a dictionary of strings (From their docs)
image

Would be really nice to have this in requests-mock so that tests can be 1:1

@jamielennox
Copy link
Owner

What isn't supported at the moment for this to work? Because of where requests-mock sits in the chain there's generally nothing we need to do to support configuration like this, requests/urllib will turn it into a http request for us. I then normally just give some helpers to make matching easier.

In this case qs, as params changes based on a post/get etc, but:

import requests
import requests_mock


with requests_mock.Mocker() as m:
    url = 'https://httpbin.org/get'

    m.get(
        url=url,
        status_code=200,
        text='hello',
    )

    payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
    r = requests.get(url, params=payload)

    print("requests url", r.url)
    print("req-mock url", m.last_request.url)
    print("")
    print("requests payload", payload)
    print("req-mock payload", m.last_request.qs)
requests url https://httpbin.org/get?key1=value1&key2=value2&key2=value3
req-mock url https://httpbin.org/get?key1=value1&key2=value2&key2=value3

requests payload {'key1': 'value1', 'key2': ['value2', 'value3']}
req-mock payload {'key1': ['value1'], 'key2': ['value2', 'value3']}

you can see the qs is slightly different to the payload. At the point where i decode the qs i'm decoding the actual url so i i can't know if there was a single item array or not so for consistency it's always a string : list[string] response.

@tsaavik
Copy link
Author

tsaavik commented Jun 8, 2023

Thanks for the reply! I should have been more specific about my request, sorry about that.
I was trying to return different mocked results from the same url based on the param payload.
For example:

@pytest.fixture
def test_requests(requests_mock):
    url = "https://httpbin.org/get"
    
    #Mock all content
    payload = {"type": "all"}
    requests_mock.get(url, params=payload, text="ALL content")

    #Mock no content
    payload = {"type": "none"}
    requests_mock.get(url, params=payload, text="NO content")

I apologize if I'm totally wrong here and just doing something dumb. I'm new to Python :)

@jamielennox
Copy link
Owner

ah, i see. the functionality is there i just don't use the params key, as you say i normally try for consistency with requests so maybe i should have.

https://requests-mock.readthedocs.io/en/stable/matching.html#query-strings

@michaeligreenberg
Copy link

One conceptually nice thing about a params parameter is that it could urlencode the query parameters so that one doesn't have to when constructing the URL to match.

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