]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Update aiohttp_stub to work with binary content
authorAllisson Azevedo <allisson@gmail.com>
Thu, 22 Jun 2017 18:12:58 +0000 (15:12 -0300)
committerAllisson Azevedo <allisson@gmail.com>
Thu, 22 Jun 2017 18:12:58 +0000 (15:12 -0300)
tests/integration/aiohttp_utils.py
tests/integration/test_aiohttp.py
vcr/stubs/aiohttp_stubs/__init__.py

index 5b77fae0c69fbfca696956d858c0a428adc82141..991877b1f2d2e3fbced8a727b190ac383046e5c4 100644 (file)
@@ -3,11 +3,13 @@ import aiohttp
 
 
 @asyncio.coroutine
-def aiohttp_request(loop, method, url, as_text, **kwargs):
+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
-        if as_text:
+        if output == 'text':
             content = yield from response.text()  # NOQA: E999
-        else:
+        elif output == 'json':
             content = yield from response.json()  # NOQA: E999
+        elif output == 'raw':
+            content = yield from response.read()  # NOQA: E999
         return response, content
index 280be3a99520ca5447ae61ab335267867946e60c..bccd4e67e964019cb9dd2eeed04028c15de41deb 100644 (file)
@@ -22,19 +22,19 @@ def run_in_loop(fn):
         return loop.run_until_complete(task)
 
 
-def request(method, url, as_text=True, **kwargs):
+def request(method, url, output='text', **kwargs):
     def run(loop):
-        return aiohttp_request(loop, method, url, as_text, **kwargs)
+        return aiohttp_request(loop, method, url, output=output, **kwargs)
 
     return run_in_loop(run)
 
 
-def get(url, as_text=True, **kwargs):
-    return request('GET', url, as_text, **kwargs)
+def get(url, output='text', **kwargs):
+    return request('GET', url, output=output, **kwargs)
 
 
-def post(url, as_text=True, **kwargs):
-    return request('POST', url, as_text, **kwargs)
+def post(url, output='text', **kwargs):
+    return request('POST', url, output='text', **kwargs)
 
 
 @pytest.fixture(params=["https", "http"])
@@ -79,14 +79,25 @@ def test_text(tmpdir, scheme):
 def test_json(tmpdir, scheme):
     url = scheme + '://httpbin.org/get'
     with vcr.use_cassette(str(tmpdir.join('json.yaml'))):
-        _, response_json = get(url, as_text=False)
+        _, response_json = get(url, output='json')
 
     with vcr.use_cassette(str(tmpdir.join('json.yaml'))) as cassette:
-        _, cassette_response_json = get(url, as_text=False)
+        _, cassette_response_json = get(url, output='json')
         assert cassette_response_json == response_json
         assert cassette.play_count == 1
 
 
+def test_binary(tmpdir, scheme):
+    url = scheme + '://httpbin.org/image/png'
+    with vcr.use_cassette(str(tmpdir.join('binary.yaml'))):
+        _, response_binary = get(url, output='raw')
+
+    with vcr.use_cassette(str(tmpdir.join('binary.yaml'))) as cassette:
+        _, cassette_response_binary = get(url, output='raw')
+        assert cassette_response_binary == response_binary
+        assert cassette.play_count == 1
+
+
 def test_post(tmpdir, scheme):
     data = {'key1': 'value1', 'key2': 'value2'}
     url = scheme + '://httpbin.org/post'
@@ -103,10 +114,10 @@ def test_params(tmpdir, scheme):
     url = scheme + '://httpbin.org/get'
     params = {'a': 1, 'b': False, 'c': 'c'}
     with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
-        _, response_json = get(url, as_text=False, params=params)
+        _, response_json = get(url, output='json', params=params)
 
     with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
-        _, cassette_response_json = get(url, as_text=False, params=params)
+        _, cassette_response_json = get(url, output='json', params=params)
         assert cassette_response_json == response_json
         assert cassette.play_count == 1
 
@@ -115,15 +126,15 @@ def test_params_same_url_distinct_params(tmpdir, scheme):
     url = scheme + '://httpbin.org/get'
     params = {'a': 1, 'b': False, 'c': 'c'}
     with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
-        _, response_json = get(url, as_text=False, params=params)
+        _, response_json = get(url, output='json', params=params)
 
     with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
-        _, cassette_response_json = get(url, as_text=False, params=params)
+        _, cassette_response_json = get(url, output='json', params=params)
         assert cassette_response_json == response_json
         assert cassette.play_count == 1
 
     other_params = {'other': 'params'}
     with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
-        response, cassette_response_text = get(url, as_text=True, params=other_params)
+        response, cassette_response_text = get(url, output='text', params=other_params)
         assert 'No match for the request' in cassette_response_text
         assert response.status == 599
index 28cf8c77aa75ed954a9a4669776409104ff2717a..79ce06c1ff9b6d30a0f6a7e043ba2a678fd45e51 100644 (file)
@@ -21,6 +21,10 @@ class MockClientResponse(ClientResponse):
     def text(self, encoding='utf-8'):
         return self.content.decode(encoding)
 
+    @asyncio.coroutine
+    def read(self):
+        return self.content
+
     @asyncio.coroutine
     def release(self):
         pass
@@ -71,7 +75,7 @@ def vcr_request(cassette, real_request):
                 'message': response.reason,
             },
             'headers': dict(response.headers),
-            'body': {'string': (yield from response.text())},  # NOQA: E999
+            'body': {'string': (yield from response.read())},  # NOQA: E999
             'url': response.url,
         }
         cassette.append(vcr_request, vcr_response)