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

Add reusable client tests and refactor #74

Merged
merged 1 commit into from
May 3, 2024
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
7 changes: 4 additions & 3 deletions sanic_testing/reusable.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ def stop(self):
self._run(
self.app._server_event("shutdown", "before", loop=self._loop)
)
if self._session:
self._run(self._session.aclose())
self._session = None

if self._server:
self._server.close()
self._run(self._server.wait_closed())
self._server = None

if self._session:
self._run(self._session.aclose())
self._session = None
self._run(self.app._server_event("shutdown", "after", loop=self._loop))

def _sanic_endpoint_test(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_reusable_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from sanic import Sanic, response

from sanic_testing.reusable import ReusableClient


@pytest.fixture
def reusable_app():
sanic_app = Sanic(__name__)

@sanic_app.get("/")
def basic(request):
return response.text("foo")

return sanic_app


@pytest.mark.asyncio
def test_basic_asgi_client(reusable_app):
client = ReusableClient(reusable_app)
with client:
request, response = client.get("/")

assert request.method.lower() == "get"
assert response.body == b"foo"
assert response.status == 200
Loading