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