]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Drop support to asyncio.coroutine (py34 async/await syntax)
authorLuiz Menezes <luiz.menezesf@gmail.com>
Tue, 18 Sep 2018 16:59:40 +0000 (13:59 -0300)
committerLuiz Menezes <luiz.menezesf@gmail.com>
Tue, 18 Sep 2018 17:14:25 +0000 (14:14 -0300)
tests/integration/aiohttp_utils.py
tests/integration/test_aiohttp.py
vcr/_handle_coroutine.py
vcr/stubs/aiohttp_stubs/__init__.py

index b0b2dd65a88a17d4b0e1f50f2616e623a1475b92..6f6f15e991acea33bb0c184d1e1fa7c6a15faffc 100644 (file)
@@ -2,23 +2,32 @@
 import asyncio
 
 import aiohttp
+from aiohttp.test_utils import TestClient
 
 
-@asyncio.coroutine
-def aiohttp_request(loop, method, url, output='text', encoding='utf-8', content_type=None, **kwargs):
+async def aiohttp_request(loop, method, url, output='text', encoding='utf-8', content_type=None, **kwargs):
     session = aiohttp.ClientSession(loop=loop)
     response_ctx = session.request(method, url, **kwargs)
 
-    response = yield from response_ctx.__aenter__()
+    response = await response_ctx.__aenter__()
     if output == 'text':
-        content = yield from response.text()
+        content = await response.text()
     elif output == 'json':
         content_type = content_type or 'application/json'
-        content = yield from response.json(encoding=encoding, content_type=content_type)
+        content = await response.json(encoding=encoding, content_type=content_type)
     elif output == 'raw':
-        content = yield from response.read()
+        content = await response.read()
 
     response_ctx._resp.close()
-    yield from session.close()
+    await session.close()
 
     return response, content
+
+
+def aiohttp_app():
+    async def hello(request):
+        return aiohttp.web.Response(text='hello')
+
+    app = aiohttp.web.Application()
+    app.router.add_get('/', hello)
+    return app
index 4951e69bc72e2acc9b379c1f62ab7ef8123ab3ef..74aaccc347026489abc0e6ef9dc7cd8e812ecb3a 100644 (file)
@@ -5,7 +5,7 @@ asyncio = pytest.importorskip("asyncio")
 aiohttp = pytest.importorskip("aiohttp")
 
 import vcr  # noqa: E402
-from .aiohttp_utils import aiohttp_request  # noqa: E402
+from .aiohttp_utils import aiohttp_app, aiohttp_request  # noqa: E402
 
 
 def run_in_loop(fn):
@@ -156,13 +156,24 @@ def test_params_on_url(tmpdir, scheme):
         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)
+def test_aiohttp_test_client(aiohttp_client, tmpdir):
+    loop = asyncio.get_event_loop()
+    app = aiohttp_app()
+    url = '/'
+    client = loop.run_until_complete(aiohttp_client(app))
 
     with vcr.use_cassette(str(tmpdir.join('get.yaml'))):
-        await client.get('/')
+        response = loop.run_until_complete(client.get(url))
+
+    assert response.status == 200
+    response_text = loop.run_until_complete(response.text())
+    assert response_text == 'hello'
+
+    with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
+        response = loop.run_until_complete(client.get(url))
+
+    request = cassette.requests[0]
+    assert request.url == str(client.make_url(url))
+    response_text = loop.run_until_complete(response.text())
+    assert response_text == 'hello'
+    assert cassette.play_count == 1
index 0b20be6ff4b312de5e25fb674f243cf9a7466e60..610305fa99bba876b9319143c237068fd40729ca 100644 (file)
@@ -1,7 +1,3 @@
-import asyncio
-
-
-@asyncio.coroutine
-def handle_coroutine(vcr, fn):
+async def handle_coroutine(vcr, fn):  # noqa: E999
     with vcr as cassette:
-        return (yield from fn(cassette))  # noqa: E999
+        return (await fn(cassette))  # noqa: E999
index 78bb6583bc0c6a60ee0c933a43e75f7475934098..38f337e5f369cbcd9d7402336c6094a6121354a0 100644 (file)
@@ -25,21 +25,16 @@ class MockClientResponse(ClientResponse):
             session=None,
         )
 
-    # TODO: get encoding from header
-    @asyncio.coroutine
-    def json(self, *, encoding='utf-8', loads=json.loads, **kwargs):  # NOQA: E999
+    async def json(self, *, encoding='utf-8', loads=json.loads, **kwargs):  # NOQA: E999
         return loads(self._body.decode(encoding))
 
-    @asyncio.coroutine
-    def text(self, encoding='utf-8'):
+    async def text(self, encoding='utf-8'):
         return self._body.decode(encoding)
 
-    @asyncio.coroutine
-    def read(self):
+    async def read(self):
         return self._body
 
-    @asyncio.coroutine
-    def release(self):
+    async def release(self):
         pass