From 85cad99662b9b40f9fc3e114edbbc41e798969e9 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Sat, 8 Jul 2017 14:55:34 +0800 Subject: [PATCH] tests for both tornado and aiohttp --- .travis.yml | 6 +++++- nvchecker/source/__init__.py | 11 +++++++++-- tests/test_proxy.py | 38 +++++++++++++++++++++++------------- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index 61a4e33..081415f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ python: - "nightly" # ptr requires Python >= 3.3 but pypy3 reports 3.2 # - "pypy3" -install: pip install -U aiohttp pytest pytest-asyncio pytest-xdist flaky +install: pip install -U $DEPS pytest pytest-asyncio pytest-xdist flaky script: pytest env: global: @@ -14,3 +14,7 @@ env: - secure: "JNuxbHbO+Qj88r0So+FKp8GBVmobGlBNi0hkZIyOH4cBXtuiM1Jo6FtRYInfTUH5TcgfMQml1a8p9g8n1fbRcTsxPt3kkT0ZleW1fJNudOHJFOmDooM4gC2/A+6aMl3xdnLCQ9cXxqsXjIUBie3GhqC4ufInU7VshxOn7KZADbI3zDuLuw9gdsBQf/OADY4oO3y1URxdnWjssP8pwfDFRSEkuLKNDtsYrhkmp3jRAq5DMtMXTEyHly9CJHow7yMyoBHa6Q/J7+C57pI4JsO8c0nJWy/wQUnqw9EeLE/9gAHY1sHlEpjZtJrV45kRd+KC6x4FtoFjvngxymK2A0zmecBI3DRTWBAZedPPVatAD9nlDmwAacBtwvuZJkt6fMUBWMY1I1NEiwdYxceBiqrnvU48FfNOylXE6KuarCQZik/VWk8olIQjXIukMu8EQ58pnEuLZB7wbwNzMLheomuVMEK1nfLOltKaytztl/7cKlsx6SmxY5rQI/x7QInd+rq9OxDDwCo+jEofPKvAcCbUJj6SqfB7QAUxJwwD/ER4/Bji9KSz3BoCu+x7h/ILcskNqLlg4LDCcpxqMOyxePk7A30sSop1E5YLWo0lmS9s88mEz89tzCWSDVIzwQrdMghNBe6JFMzOoKDRDhEkMrs3MAK+FUJkbteGhHrdC86EidU=" # gitlab - secure: "ZmD5E59cLbGylhId+uYsuaM7HCcuP7E3DVZUHtSKQ7ZtiDIPG2EFCl+WlcPBS2JhdyEJ1v7PbxSCq6/jkSK2EGVcWaHMDIyZRu8TFY+l8mqghHi18l0jeegE7FSqkW2JMWmB3y6jsv7EV6YffrSuvHiNgHyZhhIRkbV/B4N9KvJwNYoyVxGYGoJRe5yuvE+2Xkwc9y0ddxh/p+nILU+Vt0i3Z+yVfg4jul7zN1KhK8I8ax4wpAq+0V1PpWbF6/4UK5Xc/1UMEyWE0f8aEzn4kdC6UetOKacWycq7ag3W1vWKVYJvXyRKjGWmef+Al7z8KbwBkU6KR0Hb2OZWKF3SsCvv8bQEjbqcIeGKQT9J2LTgqTxgFtCMmKWXM3BxLASac/WEdQyyZmQq3XHI/OyJ/1hsSLCgvpexIueITo68LkOigrRRiobSER6KK1CaA1AQFWnPnEwrC3QCtzYUIHPT70nTy1Dx0PiOeQrG/stUoPQ9V0FCBf2tKYg2tUDlJbk7avt8rsmLht1uGx8I75qgg3Di/03N19wEBf6V50H9T23wYXRJ/q2mqPiBCBWIlHwE8NbLZgRfBvCFyUBRvggNAyvZaEOmWRl3U9JEipcqrAQtddzDP1dUbr6SYJChR6yfMkWXXsUvy3FxrOOILeaBT2j0goSaye8aLncUqArgWSw=" + matrix: + - DEPS=aiohttp + - DEPS=tornado pycurl + - DEPS=tornado diff --git a/nvchecker/source/__init__.py b/nvchecker/source/__init__.py index a823ee6..024d9b0 100644 --- a/nvchecker/source/__init__.py +++ b/nvchecker/source/__init__.py @@ -3,10 +3,17 @@ try: import tornado, pycurl + # connection reuse, http/2 which = 'tornado' except ImportError: - import aiohttp - which = 'aiohttp' + try: + import aiohttp + which = 'aiohttp' + # connection reuse + except ImportError: + import tornado + which = 'tornado' + # fallback m = __import__('%s_httpclient' % which, globals(), locals(), level=1) __all__ = m.__all__ diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 0870fbc..c157ec8 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -1,25 +1,35 @@ # MIT licensed # Copyright (c) 2017 Felix Yan , et al. -import aiohttp +import sys + +try: + import aiohttp +except ImportError: + aiohttp = None + import pytest -pytestmark = pytest.mark.asyncio +pytestmark = [ + pytest.mark.asyncio, + pytest.mark.skipif(not aiohttp, reason="requires aiohttp"), +] async def test_proxy(get_version, monkeypatch): - from nvchecker.source import session + sys.modules['nvchecker.source'] = sys.modules['nvchecker.source.aiohttp_httpclient'] + from nvchecker.source import session - async def fake_request(*args, proxy, **kwargs): - class fake_response(): - async def read(): - return proxy.encode("ascii") + async def fake_request(*args, proxy, **kwargs): + class fake_response(): + async def read(): + return proxy.encode("ascii") - def release(): - pass + def release(): + pass - return fake_response + return fake_response - monkeypatch.setattr(session, "nv_config", {"proxy": "255.255.255.255:65535"}, raising=False) - monkeypatch.setattr(aiohttp.ClientSession, "_request", fake_request) + monkeypatch.setattr(session, "nv_config", {"proxy": "255.255.255.255:65535"}, raising=False) + monkeypatch.setattr(aiohttp.ClientSession, "_request", fake_request) - assert await get_version("example", {"regex": "(.+)", "url": "deadbeef"}) == "255.255.255.255:65535" - assert await get_version("example", {"regex": "(.+)", "url": "deadbeef", "proxy": "0.0.0.0:0"}) == "0.0.0.0:0" + assert await get_version("example", {"regex": "(.+)", "url": "deadbeef"}) == "255.255.255.255:65535" + assert await get_version("example", {"regex": "(.+)", "url": "deadbeef", "proxy": "0.0.0.0:0"}) == "0.0.0.0:0"