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

aiohttp.test_utils.TestClient doesn't work with async with #2525

Closed
barrachri opened this issue Nov 17, 2017 · 4 comments
Closed

aiohttp.test_utils.TestClient doesn't work with async with #2525

barrachri opened this issue Nov 17, 2017 · 4 comments
Labels

Comments

@barrachri
Copy link

Long story short

Testing websocket using a TestClient does not work as expected.

Expected behaviour

async with cli.ws_connect('http://localhost/ws') as ws:

should work, where cli is aiohttp.test_utils.TestClient

Actual behaviour

>       async with cli.ws_connect('/ws') as ws:
E       AttributeError: __aexit__

Steps to reproduce

Use async with a TestClient.ws_connect

Your environment

Python 3.6.3
aiohttp 2.3.2

@barrachri
Copy link
Author

barrachri commented Nov 17, 2017

I'd like to send a PR but I am a little bit lost inside the code.

TestClient.ws_connect returns a ClientSession(loop=loop, cookie_jar=cookie_jar, **kwargs) instance

ws_connect in ClientSession returns _WSRequestContextManager( self._ws_connect

self._ws_connect returns a ws_response_class

that is a ClientWebSocketResponse instance

and then wrapped inside a _WSRequestContextManager that seems to have a aexit:

class _WSRequestContextManager(_BaseRequestContextManager):
    @asyncio.coroutine
    def __aexit__(self, exc_type, exc, tb):
        yield from self._resp.close()

Am I missing something?

@asvetlov
Copy link
Member

I suggest the following trick:

  1. Rename TestClient.ws_connect coroutine to _ws_connect.
  2. Create TestClient.ws_connect non-coroutine function which returns a wrapped call to self._ws_connect() -- just like ClientSession does.

@asvetlov
Copy link
Member

Supporting both await and async with in the same async function is a quite tricky.

rutsky referenced this issue in rutsky/aiohttp Aug 1, 2018
* WIP fix#2525

* Update test_utils.py
@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants