mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 09:35:34 +00:00
Merge pull request #437 from steinnes/minimal-aiohttp-streamreader-support
Minimal aiohttp streamreader support
This commit is contained in:
@@ -17,6 +17,8 @@ async def aiohttp_request(loop, method, url, output='text', encoding='utf-8', co
|
|||||||
content = await 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 = await response.read()
|
content = await response.read()
|
||||||
|
elif output == 'stream':
|
||||||
|
content = await response.content.read()
|
||||||
|
|
||||||
response_ctx._resp.close()
|
response_ctx._resp.close()
|
||||||
await session.close()
|
await session.close()
|
||||||
|
|||||||
@@ -93,6 +93,18 @@ def test_binary(tmpdir, scheme):
|
|||||||
assert cassette.play_count == 1
|
assert cassette.play_count == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_stream(tmpdir, scheme):
|
||||||
|
url = scheme + '://httpbin.org/get'
|
||||||
|
|
||||||
|
with vcr.use_cassette(str(tmpdir.join('stream.yaml'))):
|
||||||
|
resp, body = get(url, output='raw') # Do not use stream here, as the stream is exhausted by vcr
|
||||||
|
|
||||||
|
with vcr.use_cassette(str(tmpdir.join('stream.yaml'))) as cassette:
|
||||||
|
cassette_resp, cassette_body = get(url, output='stream')
|
||||||
|
assert cassette_body == body
|
||||||
|
assert cassette.play_count == 1
|
||||||
|
|
||||||
|
|
||||||
def test_post(tmpdir, scheme):
|
def test_post(tmpdir, scheme):
|
||||||
data = {'key1': 'value1', 'key2': 'value2'}
|
data = {'key1': 'value1', 'key2': 'value2'}
|
||||||
url = scheme + '://httpbin.org/post'
|
url = scheme + '://httpbin.org/post'
|
||||||
|
|||||||
@@ -5,12 +5,16 @@ import asyncio
|
|||||||
import functools
|
import functools
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from aiohttp import ClientResponse
|
from aiohttp import ClientResponse, streams
|
||||||
from yarl import URL
|
from yarl import URL
|
||||||
|
|
||||||
from vcr.request import Request
|
from vcr.request import Request
|
||||||
|
|
||||||
|
|
||||||
|
class MockStream(asyncio.StreamReader, streams.AsyncStreamReaderMixin):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class MockClientResponse(ClientResponse):
|
class MockClientResponse(ClientResponse):
|
||||||
def __init__(self, method, url):
|
def __init__(self, method, url):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
@@ -37,6 +41,13 @@ class MockClientResponse(ClientResponse):
|
|||||||
def release(self):
|
def release(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def content(self):
|
||||||
|
s = MockStream()
|
||||||
|
s.feed_data(self._body)
|
||||||
|
s.feed_eof()
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
def vcr_request(cassette, real_request):
|
def vcr_request(cassette, real_request):
|
||||||
@functools.wraps(real_request)
|
@functools.wraps(real_request)
|
||||||
|
|||||||
Reference in New Issue
Block a user