Skip to content

Commit

Permalink
Merge pull request #48 from yunstanford/yx-replace-aiohttp-with-httpx
Browse files Browse the repository at this point in the history
Replace aiohttp with httpx
  • Loading branch information
yunstanford authored Feb 27, 2021
2 parents b039329 + e398b67 commit d31dd30
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 431 deletions.
629 changes: 253 additions & 376 deletions poetry.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest-sanic"
version = "1.6.2"
version = "1.7.0"
description = "a pytest plugin for Sanic"
authors = ["Yun Xu <[email protected]>"]
license = "MIT"
Expand All @@ -13,14 +13,15 @@ classifiers = [
]

[tool.poetry.dependencies]
python = "^3.6"
python = ">=3.6.1"
pytest = ">=5.2"
async_generator = "^1.10"
aiohttp = "^3.6.2"
httpx = ">=0.15.4"
websockets = ">=8.1,<9.0"

[tool.poetry.dev-dependencies]
pytest = ">=5.3.5"
sanic = "^19.12.2"
sanic = "^20.12.2"
pytest-cov = "^2.8.1"

[tool.poetry.plugins."pytest11"]
Expand Down
15 changes: 5 additions & 10 deletions pytest_sanic/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import socket
import asyncio
import warnings
import httpx
import websockets

from aiohttp import ClientSession, CookieJar
from sanic.server import serve, HttpProtocol
from inspect import isawaitable
from sanic.app import Sanic
Expand Down Expand Up @@ -187,9 +188,7 @@ def __init__(self, app, loop=None,
self._app, loop=loop,
protocol=self._protocol, ssl=self._ssl,
scheme=self._scheme)
cookie_jar = CookieJar(unsafe=True)
self._session = ClientSession(cookie_jar=cookie_jar,
**kwargs)
self._session = httpx.AsyncClient(**kwargs)
# Let's collect responses objects and websocket objects,
# and clean up when test is done.
self._responses = []
Expand Down Expand Up @@ -233,7 +232,7 @@ async def close(self):
resp.close()
for ws in self._websockets:
await ws.close()
await self._session.close()
await self._session.aclose()
await self._server.close()
self._closed = True

Expand Down Expand Up @@ -269,13 +268,9 @@ async def head(self, uri, *args, **kwargs):
async def ws_connect(self, uri, *args, **kwargs):
"""
Create a websocket connection.
a thin wrapper around aiohttp.ClientSession.ws_connect.
"""
url = self._server.make_url(uri)
ws_conn = await self._session.ws_connect(
url, *args, **kwargs
)
ws_conn = await websockets.connect(url, *args, **kwargs)
# Save it, clean up later.
self._websockets.append(ws_conn)
return ws_conn
Expand Down
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
README_PATH = os.path.join(base, "README.rst")

install_requires = [
'pytest',
'aiohttp',
'async_generator',
'pytest>=5.2',
'httpx>=0.15.4',
'async_generator>=1.10',
'websockets>=8.1,<9.0',
]

tests_require = []

setup(name='pytest-sanic',
version='1.6.1',
version='1.7.0',
description='a pytest plugin for Sanic',
long_description=open(README_PATH).read(),
author='Yun Xu',
Expand All @@ -32,7 +33,6 @@
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
Expand Down
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import sanic
import asyncio
import pytest

from sanic.app import Sanic
from sanic.websocket import WebSocketProtocol
from sanic import response


collect_ignore = []
Sanic.test_mode = True

if sanic.__version__ <= '0.6.0':
collect_ignore.append("test_client_websocket.py")
Expand Down Expand Up @@ -70,4 +72,10 @@ def sanic_server(loop, app, test_server):

@pytest.fixture
def test_cli(loop, app, sanic_client):
return loop.run_until_complete(sanic_client(app, protocol=WebSocketProtocol))
return loop.run_until_complete(sanic_client(app))


@pytest.fixture
def test_cli_ws(loop, app, sanic_client):
return loop.run_until_complete(sanic_client(app, scheme='ws', protocol=WebSocketProtocol))

53 changes: 26 additions & 27 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest

from sanic.app import Sanic
from sanic.websocket import WebSocketProtocol
from sanic import response
from aiohttp.web import Application


async def test_fixture_sanic_client_get_properties(test_cli):
Expand All @@ -21,58 +20,57 @@ async def test_fixture_sanic_client_make_url(test_cli):

async def test_fixture_sanic_client_get(test_cli):
resp = await test_cli.get('/test_get')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json == {"GET": True}


async def test_fixture_sanic_client_post(test_cli):
resp = await test_cli.post('/test_post')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json == {"POST": True}


async def test_fixture_sanic_client_put(test_cli):
resp = await test_cli.put('/test_put')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json == {"PUT": True}


async def test_fixture_sanic_client_delete(test_cli):
resp = await test_cli.delete('/test_delete')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json == {"DELETE": True}


async def test_fixture_sanic_client_patch(test_cli):
resp = await test_cli.patch('/test_patch')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json == {"PATCH": True}


async def test_fixture_sanic_client_options(test_cli):
resp = await test_cli.options('/test_options')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json == {"OPTIONS": True}


async def test_fixture_sanic_client_head(test_cli):
resp = await test_cli.head('/test_head')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
# HEAD should not have body
assert resp_json is None
assert resp.content == b""


async def test_fixture_sanic_client_close(test_cli):
resp = await test_cli.get('/test_get')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json == {"GET": True}
await test_cli.close()
assert test_cli._closed == True
Expand All @@ -81,31 +79,32 @@ async def test_fixture_sanic_client_close(test_cli):
async def test_fixture_sanic_client_passing_headers(test_cli):
headers={"authorization": "Basic bG9naW46cGFzcw=="}
resp = await test_cli.get('/test_passing_headers', headers=headers)
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json["headers"]["authorization"] == headers["authorization"]


async def test_fixture_sanic_client_context_manager(app, sanic_client):
async with await sanic_client(app) as test_cli:
resp = await test_cli.get('/test_get')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json == {"GET": True}


async def test_fixture_test_client_context_manager(app, test_client):
async with await test_client(app) as test_cli:
resp = await test_cli.get('/test_get')
assert resp.status == 200
resp_json = await resp.json()
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json == {"GET": True}


async def test_fixture_sanic_client_raise_exception_for_non_sanic_app(sanic_client):
aiohttp_web = Application()
class SimpleApplication:
pass
with pytest.raises(TypeError):
await sanic_client(aiohttp_web)
await sanic_client(SimpleApplication())


async def test_fixture_sanic_client_app_is_running(test_cli):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_client_websocket.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This is a special test. it will fail for sanic <= 0.5.4
async def test_fixture_sanic_client_ws(test_cli):
ws_conn = await test_cli.ws_connect('/test_ws')
async def test_fixture_sanic_client_ws(test_cli_ws):
ws_conn = await test_cli_ws.ws_connect('/test_ws')
data = 'hello world!'
await ws_conn.send_str(data)
msg = await ws_conn.receive()
assert msg.data == data
await ws_conn.send(data)
msg = await ws_conn.recv()
assert msg == data
await ws_conn.close()
8 changes: 5 additions & 3 deletions tests/test_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from aiohttp.web import Application
import pytest


Expand All @@ -19,6 +18,9 @@ async def test_fixture_test_server_close(sanic_server):


async def test_fixture_test_server_raise_exception_for_non_sanic_app(test_server):
aiohttp_web = Application()

class SimpleApplication:
pass

with pytest.raises(TypeError):
await test_server(aiohttp_web)
await test_server(SimpleApplication())

0 comments on commit d31dd30

Please sign in to comment.