Clean up use cases:
This commit is contained in:
parent
96cc49e31e
commit
3f1e9ff528
|
@ -826,6 +826,9 @@ class Sanic(BaseSanic):
|
|||
await result
|
||||
|
||||
async def _run_request_middleware(self, request, request_name=None):
|
||||
print(self.request_middleware)
|
||||
print(self.named_request_middleware)
|
||||
print(request_name)
|
||||
# The if improves speed. I don't know why
|
||||
named_middleware = self.named_request_middleware.get(
|
||||
request_name, deque()
|
||||
|
|
|
@ -122,7 +122,8 @@ class Blueprint(BaseSanic):
|
|||
)
|
||||
|
||||
route = app._apply_route(apply_route)
|
||||
routes.append(route)
|
||||
operation = routes.extend if isinstance(route, list) else routes.append
|
||||
operation(route)
|
||||
|
||||
# Static Files
|
||||
for future in self._future_statics:
|
||||
|
|
|
@ -52,6 +52,8 @@ class RouteMixin:
|
|||
of type :class:`FutureRoute`
|
||||
"""
|
||||
|
||||
# TODO:
|
||||
# - run when applying future, not here
|
||||
if websocket:
|
||||
self.enable_websocket()
|
||||
|
||||
|
@ -83,6 +85,8 @@ class RouteMixin:
|
|||
# variable will be a tuple of (existing routes, handler fn)
|
||||
_, handler = handler
|
||||
|
||||
# TODO:
|
||||
# - move websocket handler out and attach it when applying
|
||||
if websocket:
|
||||
websocket_handler = partial(
|
||||
self._websocket_handler,
|
||||
|
@ -100,6 +104,11 @@ class RouteMixin:
|
|||
# -
|
||||
name = self._generate_name(handler, name)
|
||||
|
||||
if isinstance(host, str):
|
||||
host = frozenset([host])
|
||||
elif host and not isinstance(host, frozenset):
|
||||
host = frozenset(host)
|
||||
|
||||
route = FutureRoute(
|
||||
handler,
|
||||
uri,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from functools import lru_cache
|
||||
from typing import Iterable, Optional, Union
|
||||
from typing import FrozenSet, Iterable, List, Optional, Union
|
||||
|
||||
from sanic_routing import BaseRouter
|
||||
from sanic_routing.exceptions import NoMethod
|
||||
|
@ -37,13 +37,14 @@ class Router(BaseRouter):
|
|||
route, handler, params = self.resolve(
|
||||
path=request.path,
|
||||
method=request.method,
|
||||
extra={"host": request.headers.get("host")}
|
||||
)
|
||||
except RoutingNotFound as e:
|
||||
raise NotFound("Requested URL {} not found".format(e.path))
|
||||
except NoMethod as e:
|
||||
raise MethodNotSupported(
|
||||
"Method {} not allowed for URL {}".format(
|
||||
request.method, request.url
|
||||
request.method, request.path
|
||||
),
|
||||
method=request.method,
|
||||
allowed_methods=e.allowed_methods,
|
||||
|
@ -68,13 +69,13 @@ class Router(BaseRouter):
|
|||
uri: str,
|
||||
methods: Iterable[str],
|
||||
handler,
|
||||
host: Optional[str] = None,
|
||||
host: Optional[Union[str, FrozenSet[str]]] = None,
|
||||
strict_slashes: bool = False,
|
||||
stream: bool = False,
|
||||
ignore_body: bool = False,
|
||||
version: Union[str, float, int] = None,
|
||||
name: Optional[str] = None,
|
||||
) -> Route:
|
||||
) -> Union[Route, List[Route]]:
|
||||
"""
|
||||
Add a handler to the router
|
||||
|
||||
|
@ -111,17 +112,34 @@ class Router(BaseRouter):
|
|||
version = str(version).strip("/").lstrip("v")
|
||||
uri = "/".join([f"/v{version}", uri.lstrip("/")])
|
||||
|
||||
route = super().add(
|
||||
params = dict(
|
||||
path=uri,
|
||||
handler=handler,
|
||||
methods=methods,
|
||||
name=name,
|
||||
strict=strict_slashes,
|
||||
)
|
||||
route.ctx.ignore_body = ignore_body
|
||||
route.ctx.stream = stream
|
||||
|
||||
return route
|
||||
if isinstance(host, str):
|
||||
hosts = [host]
|
||||
else:
|
||||
hosts = host or [None]
|
||||
|
||||
routes = []
|
||||
|
||||
for host in hosts:
|
||||
if host:
|
||||
params.update({"requirements": {"host": host}})
|
||||
|
||||
route = super().add(**params)
|
||||
route.ctx.ignore_body = ignore_body
|
||||
route.ctx.stream = stream
|
||||
|
||||
routes.append(route)
|
||||
|
||||
if len(routes) == 1:
|
||||
return routes[0]
|
||||
return routes
|
||||
|
||||
def is_stream_handler(self, request) -> bool:
|
||||
"""
|
||||
|
|
|
@ -92,6 +92,9 @@ class CompositionView:
|
|||
self.handlers = {}
|
||||
self.name = self.__class__.__name__
|
||||
|
||||
def __name__(self):
|
||||
return self.name
|
||||
|
||||
def add(self, methods, handler, stream=False):
|
||||
if stream:
|
||||
handler.is_stream = stream
|
||||
|
|
|
@ -210,12 +210,12 @@ def test_bp_with_host(app):
|
|||
app.blueprint(bp)
|
||||
headers = {"Host": "example.com"}
|
||||
request, response = app.test_client.get("/test1/", headers=headers)
|
||||
assert response.text == "Hello"
|
||||
assert response.body == b"Hello"
|
||||
|
||||
headers = {"Host": "sub.example.com"}
|
||||
request, response = app.test_client.get("/test1/", headers=headers)
|
||||
|
||||
assert response.text == "Hello subdomain!"
|
||||
print(app.router.find_route_src)
|
||||
assert response.body == b"Hello subdomain!"
|
||||
|
||||
|
||||
def test_several_bp_with_host(app):
|
||||
|
@ -240,6 +240,7 @@ def test_several_bp_with_host(app):
|
|||
assert bp.host == "example.com"
|
||||
headers = {"Host": "example.com"}
|
||||
request, response = app.test_client.get("/test/", headers=headers)
|
||||
|
||||
assert response.text == "Hello"
|
||||
|
||||
assert bp2.host == "sub.example.com"
|
||||
|
@ -537,19 +538,19 @@ def test_bp_shorthand(app):
|
|||
app.blueprint(blueprint)
|
||||
|
||||
request, response = app.test_client.get("/get")
|
||||
assert response.text == "OK"
|
||||
assert response.body == b"OK"
|
||||
|
||||
request, response = app.test_client.post("/get")
|
||||
assert response.status == 405
|
||||
|
||||
request, response = app.test_client.put("/put")
|
||||
assert response.text == "OK"
|
||||
assert response.body == b"OK"
|
||||
|
||||
request, response = app.test_client.get("/post")
|
||||
assert response.status == 405
|
||||
|
||||
request, response = app.test_client.post("/post")
|
||||
assert response.text == "OK"
|
||||
assert response.body == b"OK"
|
||||
|
||||
request, response = app.test_client.get("/post")
|
||||
assert response.status == 405
|
||||
|
@ -561,19 +562,19 @@ def test_bp_shorthand(app):
|
|||
assert response.status == 405
|
||||
|
||||
request, response = app.test_client.options("/options")
|
||||
assert response.text == "OK"
|
||||
assert response.body == b"OK"
|
||||
|
||||
request, response = app.test_client.get("/options")
|
||||
assert response.status == 405
|
||||
|
||||
request, response = app.test_client.patch("/patch")
|
||||
assert response.text == "OK"
|
||||
assert response.body == b"OK"
|
||||
|
||||
request, response = app.test_client.get("/patch")
|
||||
assert response.status == 405
|
||||
|
||||
request, response = app.test_client.delete("/delete")
|
||||
assert response.text == "OK"
|
||||
assert response.body == b"OK"
|
||||
|
||||
request, response = app.test_client.get("/delete")
|
||||
assert response.status == 405
|
||||
|
|
|
@ -43,7 +43,7 @@ async def test_cookies_asgi(app):
|
|||
response_cookies = SimpleCookie()
|
||||
response_cookies.load(response.headers.get("set-cookie", {}))
|
||||
|
||||
assert response.text == "Cookies are: working!"
|
||||
assert response.body == b"Cookies are: working!"
|
||||
assert response_cookies["right_back"].value == "at you"
|
||||
|
||||
|
||||
|
|
|
@ -45,9 +45,9 @@ def test_unexisting_methods(app):
|
|||
|
||||
app.add_route(DummyView.as_view(), "/")
|
||||
request, response = app.test_client.get("/")
|
||||
assert response.text == "I am get method"
|
||||
assert response.body == b"I am get method"
|
||||
request, response = app.test_client.post("/")
|
||||
assert "Method POST not allowed for URL /" in response.text
|
||||
assert b"Method POST not allowed for URL /" in response.body
|
||||
|
||||
|
||||
def test_argument_methods(app):
|
||||
|
|
Loading…
Reference in New Issue
Block a user