Merge pull request #2012 from sanic-org/testing-manage-2011

Testing manage 2011
This commit is contained in:
Ashley Sommer 2021-01-29 08:05:29 +10:00 committed by GitHub
commit afb527b37c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -86,6 +86,7 @@ class Sanic:
self.websocket_tasks: Set[Future] = set()
self.named_request_middleware: Dict[str, MiddlewareType] = {}
self.named_response_middleware: Dict[str, MiddlewareType] = {}
self._test_manager = None
self._test_client = None
self._asgi_client = None
# Register alternative method names
@ -1032,18 +1033,22 @@ class Sanic:
# -------------------------------------------------------------------- #
@property
def test_client(self):
def test_client(self): # noqa
if self._test_client:
return self._test_client
elif self._test_manager:
return self._test_manager.test_client
from sanic_testing.testing import SanicTestClient # type: ignore
self._test_client = SanicTestClient(self)
return self._test_client
@property
def asgi_client(self):
def asgi_client(self): # noqa
if self._asgi_client:
return self._asgi_client
elif self._test_manager:
return self._test_manager.test_client
from sanic_testing.testing import SanicASGITestClient # type: ignore
self._asgi_client = SanicASGITestClient(self)

View File

@ -1,5 +1,6 @@
import inspect
import os
from pathlib import Path
from time import gmtime, strftime
@ -93,8 +94,8 @@ def test_static_file_pathlib(app, static_file_directory, file_name):
[b"test.file", b"decode me.txt", b"python.png"],
)
def test_static_file_bytes(app, static_file_directory, file_name):
bsep = os.path.sep.encode('utf-8')
file_path = static_file_directory.encode('utf-8') + bsep + file_name
bsep = os.path.sep.encode("utf-8")
file_path = static_file_directory.encode("utf-8") + bsep + file_name
app.static("/testing.file", file_path)
request, response = app.test_client.get("/testing.file")
assert response.status == 200