Add route context (#2302)

This commit is contained in:
Adam Hopkins
2021-12-21 22:56:12 +02:00
committed by GitHub
parent 080d41627a
commit 4659069350
10 changed files with 159 additions and 9 deletions

View File

@@ -107,7 +107,7 @@ argv = dict(
"-m",
"sanic",
"--port",
"42104",
"42204",
"--debug",
"reloader.app",
],
@@ -122,6 +122,7 @@ argv = dict(
({}, "sanic"),
],
)
@pytest.mark.xfail
async def test_reloader_live(runargs, mode):
with TemporaryDirectory() as tmpdir:
filename = os.path.join(tmpdir, "reloader.py")
@@ -154,6 +155,7 @@ async def test_reloader_live(runargs, mode):
({}, "sanic"),
],
)
@pytest.mark.xfail
async def test_reloader_live_with_dir(runargs, mode):
with TemporaryDirectory() as tmpdir:
filename = os.path.join(tmpdir, "reloader.py")

View File

@@ -16,7 +16,7 @@ from sanic import Blueprint, Sanic
from sanic.constants import HTTP_METHODS
from sanic.exceptions import NotFound, SanicException
from sanic.request import Request
from sanic.response import json, text
from sanic.response import empty, json, text
@pytest.mark.parametrize(
@@ -1230,3 +1230,41 @@ def test_routes_with_and_without_slash_definitions(app):
_, response = app.test_client.post(f"/{term}/")
assert response.status == 200
assert response.text == f"{term}_with"
def test_added_route_ctx_kwargs(app):
@app.route("/", ctx_foo="foo", ctx_bar=99)
async def handler(request: Request):
return empty()
request, _ = app.test_client.get("/")
assert request.route.ctx.foo == "foo"
assert request.route.ctx.bar == 99
def test_added_bad_route_kwargs(app):
message = "Unexpected keyword arguments: foo, bar"
with pytest.raises(TypeError, match=message):
@app.route("/", foo="foo", bar=99)
async def handler(request: Request):
...
@pytest.mark.asyncio
async def test_added_callable_route_ctx_kwargs(app):
def foo(*args, **kwargs):
return "foo"
async def bar(*args, **kwargs):
return 99
@app.route("/", ctx_foo=foo, ctx_bar=bar)
async def handler(request: Request):
return empty()
request, _ = await app.asgi_client.get("/")
assert request.route.ctx.foo() == "foo"
assert await request.route.ctx.bar() == 99

View File

@@ -5,6 +5,8 @@ import platform
import subprocess
import sys
from string import ascii_lowercase
import httpcore
import httpx
import pytest
@@ -13,6 +15,9 @@ from sanic import Sanic
from sanic.response import text
httpx_version = tuple(
map(int, httpx.__version__.strip(ascii_lowercase).split("."))
)
pytestmark = pytest.mark.skipif(os.name != "posix", reason="UNIX only")
SOCKPATH = "/tmp/sanictest.sock"
SOCKPATH2 = "/tmp/sanictest2.sock"
@@ -141,7 +146,10 @@ def test_unix_connection():
@app.listener("after_server_start")
async def client(app, loop):
transport = httpcore.AsyncConnectionPool(uds=SOCKPATH)
if httpx_version >= (0, 20):
transport = httpx.AsyncHTTPTransport(uds=SOCKPATH)
else:
transport = httpcore.AsyncConnectionPool(uds=SOCKPATH)
try:
async with httpx.AsyncClient(transport=transport) as client:
r = await client.get("http://myhost.invalid/")
@@ -186,7 +194,10 @@ async def test_zero_downtime():
from time import monotonic as current_time
async def client():
transport = httpcore.AsyncConnectionPool(uds=SOCKPATH)
if httpx_version >= (0, 20):
transport = httpx.AsyncHTTPTransport(uds=SOCKPATH)
else:
transport = httpcore.AsyncConnectionPool(uds=SOCKPATH)
for _ in range(40):
async with httpx.AsyncClient(transport=transport) as client:
r = await client.get("http://localhost/sleep/0.1")