kevin1024/vcrpy

aiohttp and vcrpy

Closed this issue · 3 comments

Hello! Now i'm rewriting one of my applications to be async, in tests i use vcrpy, but i cant figure out how to get it work with aiohttp, code example:

import aiohttp
import asyncio
import vcr

async def run(urls):
    async with aiohttp.ClientSession(raise_for_status=True) as session:
        tasks = [
            fetch(dict(url=url, method='GET'), session) for url in urls
        ]

        for future in asyncio.as_completed(tasks):
            response, url_object, code = await future
            print(len(response), url_object, code)


async def fetch(request, session):
    async with session.request(**request) as response:
            result = await response.read()
            return result, request, response.status
test_vcr = vcr.VCR()

loop = asyncio.get_event_loop()
with test_vcr.use_cassette('test3.yaml'):
    loop.run_until_complete(run(['https://google.com']))

If you run code above once - everything will be ok - i can see that the cassete successfully created. But if i run same code again with existing cassete - i receive the following traceback:

Traceback (most recent call last):
  File "/Users/smosker/pych/request_t.py", line 24, in <module>
    loop.run_until_complete(run(['https://google.com']))
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 467, in run_until_complete
    return future.result()
  File "/Users/smosker/pych/request_t.py", line 12, in run
    response, url_object, code = await future
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py", line 458, in _wait_for_one
    return f.result()  # May raise f.exception().
  File "/Users/smosker/pych/request_t.py", line 18, in fetch
    result = await response.read()
  File "/usr/local/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 746, in read
    self._content = yield from self.content.read()
AttributeError: 'bytes' object has no attribute 'read'

im using python 3.6 and vcrpy==1.11.1
Any ideas why this happens? Is vcrpy really support aiohttp?

@Smosker I have this exact problem, how did you solve it?

as far as i remember i use request.json() or request.text() instead of .read()

actually there appears to be a PR that fixes my problem #320