-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from lamenezes/aiohttp-support
Aiohttp support
- Loading branch information
Showing
6 changed files
with
207 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import asyncio | ||
|
||
|
||
@asyncio.coroutine | ||
def aiohttp_request(session, method, url, as_text, **kwargs): | ||
response = yield from session.request(method, url, **kwargs) # NOQA: E999 | ||
return response, (yield from response.text()) if as_text else (yield from response.json()) # NOQA: E999 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import pytest | ||
aiohttp = pytest.importorskip("aiohttp") | ||
|
||
import asyncio # NOQA | ||
import sys # NOQA | ||
|
||
import aiohttp # NOQA | ||
import pytest # NOQA | ||
import vcr # NOQA | ||
|
||
from .aiohttp_utils import aiohttp_request # NOQA | ||
|
||
|
||
def get(url, as_text=True, **kwargs): | ||
loop = asyncio.get_event_loop() | ||
with aiohttp.ClientSession() as session: | ||
task = loop.create_task(aiohttp_request(session, 'GET', url, as_text, **kwargs)) | ||
return loop.run_until_complete(task) | ||
|
||
|
||
def post(url, as_text=True, **kwargs): | ||
loop = asyncio.get_event_loop() | ||
with aiohttp.ClientSession() as session: | ||
task = loop.create_task(aiohttp_request(session, 'POST', url, as_text, **kwargs)) | ||
return loop.run_until_complete(task) | ||
|
||
|
||
@pytest.fixture(params=["https", "http"]) | ||
def scheme(request): | ||
'''Fixture that returns both http and https.''' | ||
return request.param | ||
|
||
|
||
def test_status(tmpdir, scheme): | ||
url = scheme + '://httpbin.org' | ||
with vcr.use_cassette(str(tmpdir.join('status.yaml'))): | ||
response, _ = get(url) | ||
|
||
with vcr.use_cassette(str(tmpdir.join('status.yaml'))) as cassette: | ||
cassette_response, _ = get(url) | ||
assert cassette_response.status == response.status | ||
assert cassette.play_count == 1 | ||
|
||
|
||
def test_headers(tmpdir, scheme): | ||
url = scheme + '://httpbin.org' | ||
with vcr.use_cassette(str(tmpdir.join('headers.yaml'))): | ||
response, _ = get(url) | ||
|
||
with vcr.use_cassette(str(tmpdir.join('headers.yaml'))) as cassette: | ||
cassette_response, _ = get(url) | ||
assert cassette_response.headers == response.headers | ||
assert cassette.play_count == 1 | ||
|
||
|
||
def test_text(tmpdir, scheme): | ||
url = scheme + '://httpbin.org' | ||
with vcr.use_cassette(str(tmpdir.join('text.yaml'))): | ||
_, response_text = get(url) | ||
|
||
with vcr.use_cassette(str(tmpdir.join('text.yaml'))) as cassette: | ||
_, cassette_response_text = get(url) | ||
assert cassette_response_text == response_text | ||
assert cassette.play_count == 1 | ||
|
||
|
||
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) | ||
|
||
with vcr.use_cassette(str(tmpdir.join('json.yaml'))) as cassette: | ||
_, cassette_response_json = get(url, as_text=False) | ||
assert cassette_response_json == response_json | ||
assert cassette.play_count == 1 | ||
|
||
|
||
def test_post(tmpdir, scheme): | ||
data = {'key1': 'value1', 'key2': 'value2'} | ||
url = scheme + '://httpbin.org/post' | ||
with vcr.use_cassette(str(tmpdir.join('post.yaml'))): | ||
_, response_json = post(url, data=data) | ||
|
||
with vcr.use_cassette(str(tmpdir.join('post.yaml'))) as cassette: | ||
_, cassette_response_json = post(url, data=data) | ||
assert cassette_response_json == response_json | ||
assert cassette.play_count == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'''Stubs for aiohttp HTTP clients''' | ||
from __future__ import absolute_import | ||
|
||
import asyncio | ||
import functools | ||
import json | ||
|
||
from aiohttp import ClientResponse | ||
|
||
from vcr.request import Request | ||
|
||
|
||
class MockClientResponse(ClientResponse): | ||
# TODO: get encoding from header | ||
@asyncio.coroutine | ||
def json(self, *, encoding='utf-8', loads=json.loads): # NOQA: E999 | ||
return loads(self.content.decode(encoding)) | ||
|
||
@asyncio.coroutine | ||
def text(self, encoding='utf-8'): | ||
return self.content.decode(encoding) | ||
|
||
@asyncio.coroutine | ||
def release(self): | ||
pass | ||
|
||
|
||
def vcr_request(cassette, real_request): | ||
|
||
@functools.wraps(real_request) | ||
@asyncio.coroutine | ||
def new_request(self, method, url, **kwargs): | ||
headers = kwargs.get('headers') | ||
headers = self._prepare_headers(headers) | ||
data = kwargs.get('data') | ||
|
||
vcr_request = Request(method, url, data, headers) | ||
|
||
if cassette.can_play_response_for(vcr_request): | ||
vcr_response = cassette.play_response(vcr_request) | ||
|
||
response = MockClientResponse(method, vcr_response.get('url')) | ||
response.status = vcr_response['status']['code'] | ||
response.content = vcr_response['body']['string'] | ||
response.reason = vcr_response['status']['message'] | ||
response.headers = vcr_response['headers'] | ||
|
||
response.close() | ||
return response | ||
|
||
if cassette.write_protected and cassette.filter_request(vcr_request): | ||
response = MockClientResponse(method, url) | ||
response.status = 599 | ||
msg = ("No match for the request {!r} was found. Can't overwrite " | ||
"existing cassette {!r} in your current record mode {!r}.") | ||
msg = msg.format(vcr_request, cassette._path, cassette.record_mode) | ||
response.content = msg.encode() | ||
response.close() | ||
return response | ||
|
||
response = yield from real_request(self, method, url, **kwargs) # NOQA: E999 | ||
|
||
vcr_response = { | ||
'status': { | ||
'code': response.status, | ||
'message': response.reason, | ||
}, | ||
'headers': dict(response.headers), | ||
'body': {'string': (yield from response.text())}, # NOQA: E999 | ||
'url': response.url, | ||
} | ||
cassette.append(vcr_request, vcr_response) | ||
|
||
return response | ||
|
||
return new_request |