Lifespan and code cleanup

This commit is contained in:
Adam Hopkins
2019-06-04 10:58:00 +03:00
parent aebe2b5809
commit 3685b4de85
16 changed files with 924 additions and 179 deletions

View File

@@ -57,7 +57,7 @@ def test_asyncio_server_start_serving(app):
def test_app_loop_not_running(app):
with pytest.raises(SanicException) as excinfo:
_ = app.loop
app.loop
assert str(excinfo.value) == (
"Loop can only be retrieved after the app has started "

View File

@@ -1,5 +1,5 @@
from sanic.testing import SanicASGITestClient
def asgi_client_instantiation(app):
def test_asgi_client_instantiation(app):
assert isinstance(app.asgi_client, SanicASGITestClient)

View File

@@ -226,6 +226,7 @@ def test_config_access_log_passing_in_run(app):
assert app.config.ACCESS_LOG == True
@pytest.mark.asyncio
async def test_config_access_log_passing_in_create_server(app):
assert app.config.ACCESS_LOG == True

View File

@@ -27,6 +27,24 @@ def test_cookies(app):
assert response_cookies["right_back"].value == "at you"
@pytest.mark.asyncio
async def test_cookies_asgi(app):
@app.route("/")
def handler(request):
response = text("Cookies are: {}".format(request.cookies["test"]))
response.cookies["right_back"] = "at you"
return response
request, response = await app.asgi_client.get(
"/", cookies={"test": "working!"}
)
response_cookies = SimpleCookie()
response_cookies.load(response.headers.get("set-cookie", {}))
assert response.text == "Cookies are: working!"
assert response_cookies["right_back"].value == "at you"
@pytest.mark.parametrize("httponly,expected", [(False, False), (True, True)])
def test_false_cookies_encoded(app, httponly, expected):
@app.route("/")

View File

@@ -24,7 +24,9 @@ old_conn = None
class ReusableSanicConnectionPool(httpcore.ConnectionPool):
async def acquire_connection(self, origin):
global old_conn
connection = self.active_connections.pop_by_origin(origin, http2_only=True)
connection = self.active_connections.pop_by_origin(
origin, http2_only=True
)
if connection is None:
connection = self.keepalive_connections.pop_by_origin(origin)
@@ -187,11 +189,7 @@ class ReuseableSanicTestClient(SanicTestClient):
self._session = ResusableSanicSession()
try:
response = await getattr(self._session, method.lower())(
url,
verify=False,
timeout=request_keepalive,
*args,
**kwargs,
url, verify=False, timeout=request_keepalive, *args, **kwargs
)
except NameError:
raise Exception(response.status_code)

View File

@@ -110,21 +110,19 @@ def test_redirect_with_header_injection(redirect_app):
@pytest.mark.parametrize("test_str", ["sanic-test", "sanictest", "sanic test"])
async def test_redirect_with_params(app, test_client, test_str):
def test_redirect_with_params(app, test_str):
use_in_uri = quote(test_str)
@app.route("/api/v1/test/<test>/")
async def init_handler(request, test):
assert test == test_str
return redirect("/api/v2/test/{}/".format(quote(test)))
return redirect("/api/v2/test/{}/".format(use_in_uri))
@app.route("/api/v2/test/<test>/")
async def target_handler(request, test):
assert test == test_str
return text("OK")
test_cli = await test_client(app)
response = await test_cli.get("/api/v1/test/{}/".format(quote(test_str)))
_, response = app.test_client.get("/api/v1/test/{}/".format(use_in_uri))
assert response.status == 200
txt = await response.text()
assert txt == "OK"
assert response.content == b"OK"

View File

@@ -1,10 +1,13 @@
import asyncio
import contextlib
import pytest
from sanic.response import stream, text
async def test_request_cancel_when_connection_lost(loop, app, test_client):
@pytest.mark.asyncio
async def test_request_cancel_when_connection_lost(app):
app.still_serving_cancelled_request = False
@app.get("/")
@@ -14,10 +17,9 @@ async def test_request_cancel_when_connection_lost(loop, app, test_client):
app.still_serving_cancelled_request = True
return text("OK")
test_cli = await test_client(app)
# schedule client call
task = loop.create_task(test_cli.get("/"))
loop = asyncio.get_event_loop()
task = loop.create_task(app.asgi_client.get("/"))
loop.call_later(0.01, task)
await asyncio.sleep(0.5)
@@ -33,7 +35,8 @@ async def test_request_cancel_when_connection_lost(loop, app, test_client):
assert app.still_serving_cancelled_request is False
async def test_stream_request_cancel_when_conn_lost(loop, app, test_client):
@pytest.mark.asyncio
async def test_stream_request_cancel_when_conn_lost(app):
app.still_serving_cancelled_request = False
@app.post("/post/<id>", stream=True)
@@ -53,10 +56,9 @@ async def test_stream_request_cancel_when_conn_lost(loop, app, test_client):
return stream(streaming)
test_cli = await test_client(app)
# schedule client call
task = loop.create_task(test_cli.post("/post/1"))
loop = asyncio.get_event_loop()
task = loop.create_task(app.asgi_client.post("/post/1"))
loop.call_later(0.01, task)
await asyncio.sleep(0.5)

View File

@@ -111,7 +111,6 @@ def test_request_stream_app(app):
result += body.decode("utf-8")
return text(result)
assert app.is_request_stream is True
request, response = app.test_client.get("/get")

View File

@@ -13,15 +13,12 @@ class DelayableSanicConnectionPool(httpcore.ConnectionPool):
self._request_delay = request_delay
super().__init__(*args, **kwargs)
async def send(
self,
request,
stream=False,
ssl=None,
timeout=None,
):
async def send(self, request, stream=False, ssl=None, timeout=None):
connection = await self.acquire_connection(request.url.origin)
if connection.h11_connection is None and connection.h2_connection is None:
if (
connection.h11_connection is None
and connection.h2_connection is None
):
await connection.connect(ssl=ssl, timeout=timeout)
if self._request_delay:
await asyncio.sleep(self._request_delay)

File diff suppressed because it is too large Load Diff

View File

@@ -292,7 +292,7 @@ def test_stream_response_writes_correct_content_to_transport_when_chunked(
async def mock_drain():
pass
def mock_push_data(data):
async def mock_push_data(data):
response.protocol.transport.write(data)
response.protocol.push_data = mock_push_data
@@ -330,7 +330,7 @@ def test_stream_response_writes_correct_content_to_transport_when_not_chunked(
async def mock_drain():
pass
def mock_push_data(data):
async def mock_push_data(data):
response.protocol.transport.write(data)
response.protocol.push_data = mock_push_data

View File

@@ -76,6 +76,7 @@ def test_all_listeners(app):
assert app.name + listener_name == output.pop()
@pytest.mark.asyncio
async def test_trigger_before_events_create_server(app):
class MySanicDb:
pass