-
Notifications
You must be signed in to change notification settings - Fork 42
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
How about mocking sockets with trio ? #74
Comments
Please add more information, maybe a few lines you are using for testing that case. I am not familiar with |
I think so, it's probably more following the modern python3 gradual typing mindset... I am using https://github.com/theelous3/asks (asynchronous http client lib) to test it (I'm trying to mock sockets in there) and I made this quick script for checking: import pytest
import json
import asks
from mocket import Mocketizer
from mocket.mockhttp import Entry
asks.init('trio')
@pytest.mark.trio
async def test_connection_httpbin():
url_to_mock = 'http://httpbin.org/get'
Entry.single_register(
Entry.GET,
url_to_mock,
body=json.dumps({"args": {}}),
headers={'content-type': 'application/json'}
)
with Mocketizer():
session = asks.Session()
resp = await session.get(url_to_mock,)
assert "args" in resp.json() and resp["args"] == {}
if __name__ == '__main__':
pytest.main(['-s', __file__]) It crashes, probably because :
More investigation is needed here, but I guess my question becomes, couldn't 'MockerSocket' be a usual 'socket' to pass this kind of checks ? |
Thanks for the hint. Actually, imho -of course-, there is no reason to check the type when dealing with |
Just to add an information, https://github.com/mindflayer/python-mocket/blob/master/tests/tests35/test_http_aiohttp.py |
Oh thank I hadn't notice that, it's a good reference. |
I made a PR #75 adapting the aiohttp tests to asks with curio and trio. They can be used as reference when discussing what could or should be done. |
Thank you very much for your contribution. I am not 100% sure I should do something to make |
Hey, main author of Trio here. I wrote some more notes here: python-trio/trio#170 (comment) Trio does check that the sockets you pass in are
However, trio does have explicit support for socket mocking at a slightly higher level: if you have a class that implements the trio socket API (which is basically the regular socket API except the blocking functions are marked as async), then you can tell trio to use that instead of regular sockets. I wonder if there would be any way to implement a class like this, while reusing mocket's existing code for handling http requests etc.? |
Using mocket with trio, I get :
Is there any known way for this to work ?
The text was updated successfully, but these errors were encountered: