From dba7ad1ff2ba8552ba5910d35749d1717472b3d6 Mon Sep 17 00:00:00 2001 From: Joongi Kim Date: Tue, 13 Feb 2018 01:34:42 +0900 Subject: [PATCH 1/4] Upgrade to aiohttp v3 series and drop support for v1 * aiohttp v3 uses native async/await syntax and drops support for Python 3.5.2 or lower. * I think it would be reasonable enough to support latest two major versions of aiohttp: v2 and v3 series. --- aioresponses/core.py | 9 ++++----- setup.cfg | 4 ++-- tox.ini | 13 +++++-------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/aioresponses/core.py b/aioresponses/core.py index a017300..540554c 100644 --- a/aioresponses/core.py +++ b/aioresponses/core.py @@ -3,7 +3,7 @@ import asyncio import json from typing import Dict, Tuple -from unittest.mock import patch +from unittest.mock import Mock, patch from urllib.parse import urlparse, parse_qsl, urlencode from aiohttp import ( @@ -61,7 +61,7 @@ def build_response(self) -> 'ClientResponse': self.resp.headers.update(self.headers) self.resp.raw_headers = self._build_raw_headers(self.resp.headers) self.resp.status = self.status - self.resp.content = StreamReader() + self.resp.content = StreamReader(Mock(_reading_paused=False)) self.resp.content.feed_data(self.body) self.resp.content.feed_eof() @@ -188,8 +188,7 @@ def match(self, method: str, url: str) -> 'ClientResponse': del self._responses[i] return resp - @asyncio.coroutine - def _request_mock(self, orig_self: client.ClientSession, + async def _request_mock(self, orig_self: client.ClientSession, method: str, url: str, *args: Tuple, **kwargs: Dict) -> 'ClientResponse': """Return mocked response object or raise connection error.""" @@ -198,7 +197,7 @@ def _request_mock(self, orig_self: client.ClientSession, for prefix in self._passthrough: if str(url).startswith(prefix): - return (yield from self.patcher.temp_original( + return (await self.patcher.temp_original( orig_self, method, url, *args, **kwargs )) diff --git a/setup.cfg b/setup.cfg index fa25d8d..15990c1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,8 +13,8 @@ classifier = License :: OSI Approved :: MIT License Natural Language :: English Programming Language :: Python :: 3 - Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 [files] packages = @@ -29,4 +29,4 @@ source-dir = docs/source universal = 1 [flake8] -exclude = docs \ No newline at end of file +exclude = docs diff --git a/tox.ini b/tox.ini index 72532fd..f35183a 100644 --- a/tox.ini +++ b/tox.ini @@ -2,8 +2,8 @@ envlist = flake8, coverage, - py34-aiohttp{10,11,12,13,20,21,22,23} - py35-aiohttp{10,11,12,13,20,21,22,23} + py35-aiohttp{20,21,22,23,30} + py36-aiohttp{20,21,22,23,30} skipsdist=True [testenv:flake8] @@ -17,20 +17,17 @@ setenv = PYTHONPATH = {toxinidir}:{toxinidir}/aioresponses deps = - aiohttp10: aiohttp>=1.0,<1.1 - aiohttp11: aiohttp>=1.1,<1.2 - aiohttp12: aiohttp>=1.2,<1.3 - aiohttp13: aiohttp>=1.3,<2.0 aiohttp20: aiohttp>=2.0,<2.1 aiohttp21: aiohttp>=2.1,<2.2 aiohttp22: aiohttp>=2.2,<2.3 aiohttp23: aiohttp>=2.3,<2.4 + aiohttp30: aiohttp>=3.0,<3.1 aiohttpmaster: https://github.com/KeepSafe/aiohttp/archive/master.tar.gz -r{toxinidir}/requirements-dev.txt basepython = - py34: python3.4 py35: python3.5 + py36: python3.6 commands = python setup.py test @@ -41,4 +38,4 @@ commands = coverage run --source=aioresponses setup.py test [testenv:cov_html] basepython = python deps = aiohttp -commands = nose2 --coverage=aioresponses --coverage-report=html \ No newline at end of file +commands = nose2 --coverage=aioresponses --coverage-report=html From 938424f4a516ffce3702ca520601ee54bfe72faf Mon Sep 17 00:00:00 2001 From: Joongi Kim Date: Tue, 13 Feb 2018 01:41:14 +0900 Subject: [PATCH 2/4] Update travis config to use the updated list of tox envs. --- .travis.yml | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/.travis.yml b/.travis.yml index d9f2c16..3903b5e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,29 +5,6 @@ language: python matrix: include: - - python: 3.4 - env: TOXENV=py34-aiohttp10 - - python: 3.4 - env: TOXENV=py34-aiohttp12 - - python: 3.4 - env: TOXENV=py34-aiohttp13 - - python: 3.4 - env: TOXENV=py34-aiohttp20 - - python: 3.4 - env: TOXENV=py34-aiohttp21 - - python: 3.4 - env: TOXENV=py34-aiohttp22 - - python: 3.4 - env: TOXENV=py34-aiohttp23 - - - python: 3.5 - env: TOXENV=py35-aiohttp10 - - python: 3.5 - env: TOXENV=py35-aiohttp11 - - python: 3.5 - env: TOXENV=py35-aiohttp12 - - python: 3.5 - env: TOXENV=py35-aiohttp13 - python: 3.5 env: TOXENV=py35-aiohttp20 - python: 3.5 @@ -36,12 +13,26 @@ matrix: env: TOXENV=py35-aiohttp22 - python: 3.5 env: TOXENV=py35-aiohttp23 -# - python: 3.5 -# env: TOXENV=py35-aiohttpmaster - - python: 3.5 + env: TOXENV=py35-aiohttp30 + + - python: 3.6 + env: TOXENV=py36-aiohttp20 + - python: 3.6 + env: TOXENV=py36-aiohttp21 + - python: 3.6 + env: TOXENV=py36-aiohttp22 + - python: 3.6 + env: TOXENV=py36-aiohttp23 + - python: 3.6 + env: TOXENV=py36-aiohttp30 +# - python: 3.6 +# env: TOXENV=py36-aiohttpmaster +# + + - python: 3.6 env: TOXENV=flake8 - - python: 3.5 + - python: 3.6 env: TOXENV=coverage # command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors From c583131a11ce91056110b45ebdd25fdd88c701c6 Mon Sep 17 00:00:00 2001 From: Joongi Kim Date: Tue, 13 Feb 2018 01:47:26 +0900 Subject: [PATCH 3/4] Fix flake8 warning --- aioresponses/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aioresponses/core.py b/aioresponses/core.py index 540554c..ef3ce10 100644 --- a/aioresponses/core.py +++ b/aioresponses/core.py @@ -189,8 +189,8 @@ def match(self, method: str, url: str) -> 'ClientResponse': return resp async def _request_mock(self, orig_self: client.ClientSession, - method: str, url: str, *args: Tuple, - **kwargs: Dict) -> 'ClientResponse': + method: str, url: str, *args: Tuple, + **kwargs: Dict) -> 'ClientResponse': """Return mocked response object or raise connection error.""" url = merge_url_params(url, kwargs.get('params')) From ce9d779461aba32186c3c6d01aa001455ce2e4ec Mon Sep 17 00:00:00 2001 From: Joongi Kim Date: Tue, 13 Feb 2018 01:55:58 +0900 Subject: [PATCH 4/4] Use proper protocol object instead of mocking (thanks to @decaz's #87) --- aioresponses/compat.py | 13 ++++++++++++- aioresponses/core.py | 8 ++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/aioresponses/compat.py b/aioresponses/compat.py index e7f0501..a7a3bcd 100644 --- a/aioresponses/compat.py +++ b/aioresponses/compat.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from aiohttp import __version__ as aiohttp_version +from aiohttp import __version__ as aiohttp_version, StreamReader from typing import Optional from urllib.parse import urlsplit, urlencode, SplitResult, urlunsplit @@ -15,6 +15,17 @@ class URL(str): yarl_available = False +if int(aiohttp_version.split('.')[0]) >= 3: + from aiohttp.client_proto import ResponseHandler + + def stream_reader(): + protocol = ResponseHandler() + return StreamReader(protocol) +else: + def stream_reader(): + return StreamReader() + + __all__ = ['URL', 'merge_url_params'] diff --git a/aioresponses/core.py b/aioresponses/core.py index ef3ce10..6cf0d53 100644 --- a/aioresponses/core.py +++ b/aioresponses/core.py @@ -3,17 +3,17 @@ import asyncio import json from typing import Dict, Tuple -from unittest.mock import Mock, patch +from unittest.mock import patch from urllib.parse import urlparse, parse_qsl, urlencode from aiohttp import ( - hdrs, ClientResponse, ClientConnectionError, StreamReader, client + hdrs, ClientResponse, ClientConnectionError, client ) from collections import namedtuple from functools import wraps from multidict import CIMultiDict -from .compat import URL, merge_url_params +from .compat import URL, merge_url_params, stream_reader class UrlResponse(object): @@ -61,7 +61,7 @@ def build_response(self) -> 'ClientResponse': self.resp.headers.update(self.headers) self.resp.raw_headers = self._build_raw_headers(self.resp.headers) self.resp.status = self.status - self.resp.content = StreamReader(Mock(_reading_paused=False)) + self.resp.content = stream_reader() self.resp.content.feed_data(self.body) self.resp.content.feed_eof()