1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

Fix aiohttp stub to support version >= 3.1.0

This commit is contained in:
Luiz Menezes
2018-05-01 18:20:37 -03:00
parent f890709a20
commit 26be756f47
4 changed files with 21 additions and 23 deletions

View File

@@ -1,15 +1,13 @@
import asyncio
import aiohttp
@asyncio.coroutine
def aiohttp_request(loop, method, url, output='text', **kwargs):
async def aiohttp_request(loop, method, url, output='text', **kwargs):
with aiohttp.ClientSession(loop=loop) as session:
response = yield from session.request(method, url, **kwargs) # NOQA: E999
response = await session.request(method, url, **kwargs) # NOQA: E999
if output == 'text':
content = yield from response.text() # NOQA: E999
content = await response.text() # NOQA: E999
elif output == 'json':
content = yield from response.json() # NOQA: E999
content = await response.json() # NOQA: E999
elif output == 'raw':
content = yield from response.read() # NOQA: E999
content = await response.read() # NOQA: E999
return response, content

View File

@@ -1,13 +0,0 @@
import aiohttp
import pytest
import vcr
@vcr.use_cassette()
@pytest.mark.asyncio
async def test_http(): # noqa: E999
async with aiohttp.ClientSession() as session:
url = 'https://httpbin.org/get'
params = {'ham': 'spam'}
resp = await session.get(url, params=params) # noqa: E999
assert (await resp.json())['args'] == {'ham': 'spam'} # noqa: E999

View File

@@ -7,12 +7,11 @@ import contextlib # noqa: E402
import pytest # noqa: E402
import vcr # noqa: E402
from .aiohttp_utils import aiohttp_request # noqa: E402
try:
from .async_def import test_http # noqa: F401
from .aiohttp_utils import aiohttp_request # noqa: E402
except SyntaxError:
pass
pytest.skip('python<3.5', allow_module_level=True)
def run_in_loop(fn):

View File

@@ -12,6 +12,20 @@ from vcr.request import Request
class MockClientResponse(ClientResponse):
def __init__(self, method, url):
super().__init__(
method=method,
url=url,
writer=None,
continue100=None,
timer=None,
request_info=None,
auto_decompress=None,
traces=None,
loop=asyncio.get_event_loop(),
session=None,
)
# TODO: get encoding from header
@asyncio.coroutine
def json(self, *, encoding='utf-8', loads=json.loads): # NOQA: E999