mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 09:35:34 +00:00
Merge pull request #390 from kevin1024/fix-aiohttp-client
Fix vcr to support aiohttp client requests
This commit is contained in:
@@ -2,23 +2,32 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from aiohttp.test_utils import TestClient
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def aiohttp_request(loop, method, url, output='text', encoding='utf-8', content_type=None, **kwargs):
|
||||||
def aiohttp_request(loop, method, url, output='text', encoding='utf-8', content_type=None, **kwargs):
|
|
||||||
session = aiohttp.ClientSession(loop=loop)
|
session = aiohttp.ClientSession(loop=loop)
|
||||||
response_ctx = session.request(method, url, **kwargs)
|
response_ctx = session.request(method, url, **kwargs)
|
||||||
|
|
||||||
response = yield from response_ctx.__aenter__()
|
response = await response_ctx.__aenter__()
|
||||||
if output == 'text':
|
if output == 'text':
|
||||||
content = yield from response.text()
|
content = await response.text()
|
||||||
elif output == 'json':
|
elif output == 'json':
|
||||||
content_type = content_type or 'application/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':
|
elif output == 'raw':
|
||||||
content = yield from response.read()
|
content = await response.read()
|
||||||
|
|
||||||
response_ctx._resp.close()
|
response_ctx._resp.close()
|
||||||
yield from session.close()
|
await session.close()
|
||||||
|
|
||||||
return response, content
|
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
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ asyncio = pytest.importorskip("asyncio")
|
|||||||
aiohttp = pytest.importorskip("aiohttp")
|
aiohttp = pytest.importorskip("aiohttp")
|
||||||
|
|
||||||
import vcr # noqa: E402
|
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):
|
def run_in_loop(fn):
|
||||||
@@ -154,3 +154,26 @@ def test_params_on_url(tmpdir, scheme):
|
|||||||
assert request.url == url
|
assert request.url == url
|
||||||
assert cassette_response_json == response_json
|
assert cassette_response_json == response_json
|
||||||
assert cassette.play_count == 1
|
assert cassette.play_count == 1
|
||||||
|
|
||||||
|
|
||||||
|
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'))):
|
||||||
|
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
|
||||||
|
|||||||
1
tox.ini
1
tox.ini
@@ -27,6 +27,7 @@ deps =
|
|||||||
boto3: boto3
|
boto3: boto3
|
||||||
aiohttp: aiohttp
|
aiohttp: aiohttp
|
||||||
aiohttp: pytest-asyncio
|
aiohttp: pytest-asyncio
|
||||||
|
aiohttp: pytest-aiohttp
|
||||||
|
|
||||||
[flake8]
|
[flake8]
|
||||||
max_line_length = 110
|
max_line_length = 110
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
import asyncio
|
async def handle_coroutine(vcr, fn): # noqa: E999
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
|
||||||
def handle_coroutine(vcr, fn):
|
|
||||||
with vcr as cassette:
|
with vcr as cassette:
|
||||||
return (yield from fn(cassette)) # noqa: E999
|
return (await fn(cassette)) # noqa: E999
|
||||||
|
|||||||
@@ -25,28 +25,22 @@ class MockClientResponse(ClientResponse):
|
|||||||
session=None,
|
session=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO: get encoding from header
|
async def json(self, *, encoding='utf-8', loads=json.loads, **kwargs): # NOQA: E999
|
||||||
@asyncio.coroutine
|
|
||||||
def json(self, *, encoding='utf-8', loads=json.loads, **kwargs): # NOQA: E999
|
|
||||||
return loads(self._body.decode(encoding))
|
return loads(self._body.decode(encoding))
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def text(self, encoding='utf-8'):
|
||||||
def text(self, encoding='utf-8'):
|
|
||||||
return self._body.decode(encoding)
|
return self._body.decode(encoding)
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def read(self):
|
||||||
def read(self):
|
|
||||||
return self._body
|
return self._body
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def release(self):
|
||||||
def release(self):
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def vcr_request(cassette, real_request):
|
def vcr_request(cassette, real_request):
|
||||||
@functools.wraps(real_request)
|
@functools.wraps(real_request)
|
||||||
@asyncio.coroutine
|
async def new_request(self, method, url, **kwargs):
|
||||||
def new_request(self, method, url, **kwargs):
|
|
||||||
headers = kwargs.get('headers')
|
headers = kwargs.get('headers')
|
||||||
headers = self._prepare_headers(headers)
|
headers = self._prepare_headers(headers)
|
||||||
data = kwargs.get('data')
|
data = kwargs.get('data')
|
||||||
@@ -82,7 +76,7 @@ def vcr_request(cassette, real_request):
|
|||||||
response.close()
|
response.close()
|
||||||
return response
|
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 = {
|
vcr_response = {
|
||||||
'status': {
|
'status': {
|
||||||
@@ -90,7 +84,7 @@ def vcr_request(cassette, real_request):
|
|||||||
'message': response.reason,
|
'message': response.reason,
|
||||||
},
|
},
|
||||||
'headers': dict(response.headers),
|
'headers': dict(response.headers),
|
||||||
'body': {'string': (yield from response.read())}, # NOQA: E999
|
'body': {'string': (await response.read())}, # NOQA: E999
|
||||||
'url': response.url,
|
'url': response.url,
|
||||||
}
|
}
|
||||||
cassette.append(vcr_request, vcr_response)
|
cassette.append(vcr_request, vcr_response)
|
||||||
|
|||||||
Reference in New Issue
Block a user