From e559be758ad6d22fed9ef662aac848e527329e89 Mon Sep 17 00:00:00 2001 From: Stefan Tjarks Date: Thu, 28 Jun 2018 09:12:26 -0700 Subject: [PATCH] Fix aiohttp patch to work with aiohttp >= 3.3 Aiohttp expects an awaitable instance to be returned from `ClientSession._request` though `asyncio.coroutine` decorated function do not implement `__await__`. By changing the syntax and dropping Python 3.4 support we fix this issue. --- tests/integration/test_aiohttp.py | 12 ++++++++++++ tox.ini | 1 + vcr/stubs/aiohttp_stubs/__init__.py | 7 +++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_aiohttp.py b/tests/integration/test_aiohttp.py index 6cc28c3..4951e69 100644 --- a/tests/integration/test_aiohttp.py +++ b/tests/integration/test_aiohttp.py @@ -154,3 +154,15 @@ def test_params_on_url(tmpdir, scheme): assert request.url == url assert cassette_response_json == response_json assert cassette.play_count == 1 + + +async def test_aiohttp_client_does_not_break_with_patching_request(aiohttp_client, tmpdir): + async def hello(request): + return aiohttp.web.Response(text='Hello, world') + + app = aiohttp.web.Application() + app.router.add_get('/', hello) + client = await aiohttp_client(app) + + with vcr.use_cassette(str(tmpdir.join('get.yaml'))): + await client.get('/') diff --git a/tox.ini b/tox.ini index b72d43b..3eeb686 100644 --- a/tox.ini +++ b/tox.ini @@ -27,6 +27,7 @@ deps = boto3: boto3 aiohttp: aiohttp aiohttp: pytest-asyncio + aiohttp: pytest-aiohttp [flake8] max_line_length = 110 diff --git a/vcr/stubs/aiohttp_stubs/__init__.py b/vcr/stubs/aiohttp_stubs/__init__.py index f57cb63..78bb658 100644 --- a/vcr/stubs/aiohttp_stubs/__init__.py +++ b/vcr/stubs/aiohttp_stubs/__init__.py @@ -45,8 +45,7 @@ class MockClientResponse(ClientResponse): def vcr_request(cassette, real_request): @functools.wraps(real_request) - @asyncio.coroutine - def new_request(self, method, url, **kwargs): + async def new_request(self, method, url, **kwargs): headers = kwargs.get('headers') headers = self._prepare_headers(headers) data = kwargs.get('data') @@ -82,7 +81,7 @@ def vcr_request(cassette, real_request): response.close() return response - response = yield from real_request(self, method, url, **kwargs) # NOQA: E999 + response = await real_request(self, method, url, **kwargs) # NOQA: E999 vcr_response = { 'status': { @@ -90,7 +89,7 @@ def vcr_request(cassette, real_request): 'message': response.reason, }, 'headers': dict(response.headers), - 'body': {'string': (yield from response.read())}, # NOQA: E999 + 'body': {'string': (await response.read())}, # NOQA: E999 'url': response.url, } cassette.append(vcr_request, vcr_response) -- 2.43.0