Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix aiohttp stub to support version >= 3.1.0 #353

Merged
merged 16 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.pyc
.tox
.cache
.pytest_cache/
build/
dist/
*.egg/
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ env:
matrix:
allow_failures:
- env: TOX_SUFFIX="boto3"
- env: TOX_SUFFIX="aiohttp"
python: "pypy3.5-5.9.0"
exclude:
# Only run flakes on a single Python 2.x and a single 3.x
- env: TOX_SUFFIX="flakes"
Expand Down
28 changes: 18 additions & 10 deletions tests/integration/aiohttp_utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
# flake8: noqa
import asyncio

import aiohttp


@asyncio.coroutine
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 output == 'text':
content = yield from response.text() # NOQA: E999
elif output == 'json':
content = yield from response.json() # NOQA: E999
elif output == 'raw':
content = yield from response.read() # NOQA: E999
return response, content
def aiohttp_request(loop, method, url, output='text', encoding='utf-8', **kwargs):
session = aiohttp.ClientSession(loop=loop)
response_ctx = session.request(method, url, **kwargs)

response = yield from response_ctx.__aenter__()
if output == 'text':
content = yield from response.text()
elif output == 'json':
content = yield from response.json(encoding=encoding)
elif output == 'raw':
content = yield from response.read()

response_ctx._resp.close()
yield from session.close()

return response, content
13 changes: 0 additions & 13 deletions tests/integration/async_def.py

This file was deleted.

31 changes: 15 additions & 16 deletions tests/integration/test_aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import contextlib

import pytest
asyncio = pytest.importorskip("asyncio")
aiohttp = pytest.importorskip("aiohttp")

import asyncio # noqa: E402
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
except SyntaxError:
pass


def run_in_loop(fn):
with contextlib.closing(asyncio.new_event_loop()) as loop:
Expand Down Expand Up @@ -78,11 +71,13 @@ def test_text(tmpdir, scheme):

def test_json(tmpdir, scheme):
url = scheme + '://httpbin.org/get'
headers = {'Content-Type': 'application/json'}

with vcr.use_cassette(str(tmpdir.join('json.yaml'))):
_, response_json = get(url, output='json')
_, response_json = get(url, output='json', headers=headers)

with vcr.use_cassette(str(tmpdir.join('json.yaml'))) as cassette:
_, cassette_response_json = get(url, output='json')
_, cassette_response_json = get(url, output='json', headers=headers)
assert cassette_response_json == response_json
assert cassette.play_count == 1

Expand Down Expand Up @@ -112,24 +107,28 @@ def test_post(tmpdir, scheme):

def test_params(tmpdir, scheme):
url = scheme + '://httpbin.org/get'
headers = {'Content-Type': 'application/json'}
params = {'a': 1, 'b': False, 'c': 'c'}

with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
_, response_json = get(url, output='json', params=params)
_, response_json = get(url, output='json', params=params, headers=headers)

with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
_, cassette_response_json = get(url, output='json', params=params)
_, cassette_response_json = get(url, output='json', params=params, headers=headers)
assert cassette_response_json == response_json
assert cassette.play_count == 1


def test_params_same_url_distinct_params(tmpdir, scheme):
url = scheme + '://httpbin.org/get'
headers = {'Content-Type': 'application/json'}
params = {'a': 1, 'b': False, 'c': 'c'}

with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
_, response_json = get(url, output='json', params=params)
_, response_json = get(url, output='json', params=params, headers=headers)

with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
_, cassette_response_json = get(url, output='json', params=params)
_, cassette_response_json = get(url, output='json', params=params, headers=headers)
assert cassette_response_json == response_json
assert cassette.play_count == 1

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def test_post_chunked_binary(tmpdir, httpbin):
assert req1 == req2


@pytest.mark.xfail('sys.version_info >= (3, 6)', strict=True, raises=ConnectionError)
@pytest.mark.xfail((3, 5) < sys.version_info < (3, 6) and
@pytest.mark.xskip('sys.version_info >= (3, 6)', strict=True, raises=ConnectionError)
@pytest.mark.xskip((3, 5) < sys.version_info < (3, 6) and
platform.python_implementation() == 'CPython',
reason='Fails on CPython 3.5')
def test_post_chunked_binary_secure(tmpdir, httpbin_secure):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ deps =
{py27,py35,py36,pypy}-tornado4: pytest-tornado
{py27,py35,py36}-tornado4: pycurl
boto3: boto3
aiohttp: aiohttp<3
aiohttp: aiohttp
aiohttp: pytest-asyncio

[flake8]
Expand Down
14 changes: 14 additions & 0 deletions vcr/stubs/aiohttp_stubs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@


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
Expand Down