]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Fix aiohttp patch to work with aiohttp >= 3.3
authorStefan Tjarks <stefan.tjarks@eventbase.com>
Thu, 28 Jun 2018 16:12:26 +0000 (09:12 -0700)
committerLuiz Menezes <luiz.menezesf@gmail.com>
Wed, 25 Jul 2018 16:24:29 +0000 (13:24 -0300)
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
tox.ini
vcr/stubs/aiohttp_stubs/__init__.py

index 6cc28c3c3c375c30d730ae32f55b1fb0f24cce68..4951e69bc72e2acc9b379c1f62ab7ef8123ab3ef 100644 (file)
@@ -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 b72d43b2f19e8cba7264e9477d7c8f73e8610d3d..3eeb686d8b62b998c0bed54c9f77334768b47485 100644 (file)
--- 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
index f57cb63968b34178ef4d4945891b0d7aa37f2ae1..78bb6583bc0c6a60ee0c933a43e75f7475934098 100644 (file)
@@ -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)