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

Mocked requests are more permissive with args, causing tests to pass when they should fail #39

Open
furmaniak opened this issue Dec 1, 2015 · 1 comment

Comments

@furmaniak
Copy link

I ran into this issue today, where everything worked in tests but failed when not mocked.

>>> import requests
>>> requests.post('http://example.com', 2.0)
TypeError: 'float' object is not iterable

Compare to

>>> import requests
>>> import httmock
>>> with httmock.HTTMock(lambda url, req: ''): requests.post('http://example.com', 2.0)
<Response [200]>

This is because 2.0 becomes the value of data, which must be iterable in requests, but can be anything in _fake_send, Putting in a type check for data would at least fix this (perhaps most common variant) even if it's not perfect

@ljluestc
Copy link

import requests

def custom_post(url, data=None, **kwargs):
    # Check if data is provided and if it's iterable (e.g., a list or dictionary)
    if data is not None and not isinstance(data, (list, dict)):
        raise TypeError("data must be an iterable (e.g., list or dictionary)")

    return requests.post(url, data=data, **kwargs)

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

2 participants