debug and working stage--squash

This commit is contained in:
Adam Hopkins 2021-02-08 12:18:29 +02:00
parent c08b153cee
commit 0d5b2a0f69
24 changed files with 454 additions and 259 deletions

View File

@ -404,7 +404,7 @@ class Sanic(BaseSanic):
# find all the parameters we will need to build in the URL # find all the parameters we will need to build in the URL
# matched_params = re.findall(self.router.parameter_pattern, uri) # matched_params = re.findall(self.router.parameter_pattern, uri)
route.finalize_params() route.finalize()
for params in route.params.values(): for params in route.params.values():
# name, _type, pattern = self.router.parse_parameter_string(match) # name, _type, pattern = self.router.parse_parameter_string(match)
# we only want to match against each individual parameter # we only want to match against each individual parameter
@ -552,7 +552,7 @@ class Sanic(BaseSanic):
# Execute Handler # Execute Handler
# -------------------------------------------- # # -------------------------------------------- #
request.uri_template = uri request.uri_template = f"/{uri}"
if handler is None: if handler is None:
raise ServerError( raise ServerError(
( (
@ -561,7 +561,7 @@ class Sanic(BaseSanic):
) )
) )
request.endpoint = endpoint request.endpoint = request.name
# Run response handler # Run response handler
response = handler(request, *args, **kwargs) response = handler(request, *args, **kwargs)
@ -1035,12 +1035,13 @@ class Sanic(BaseSanic):
"""To be ASGI compliant, our instance must be a callable that accepts """To be ASGI compliant, our instance must be a callable that accepts
three arguments: scope, receive, send. See the ASGI reference for more three arguments: scope, receive, send. See the ASGI reference for more
details: https://asgi.readthedocs.io/en/latest/""" details: https://asgi.readthedocs.io/en/latest/"""
# raise Exception("call")
self.asgi = True self.asgi = True
self.router.finalize() self.router.finalize()
asgi_app = await ASGIApp.create(self, scope, receive, send) asgi_app = await ASGIApp.create(self, scope, receive, send)
await asgi_app() await asgi_app()
_asgi_single_callable = True # We conform to ASGI 3.0 single-callable # _asgi_single_callable = True # We conform to ASGI 3.0 single-callable
# -------------------------------------------------------------------- # # -------------------------------------------------------------------- #
# Configuration # Configuration

View File

@ -131,6 +131,7 @@ class Lifespan:
in sequence since the ASGI lifespan protocol only supports a single in sequence since the ASGI lifespan protocol only supports a single
startup event. startup event.
""" """
print(">>> starting up")
self.asgi_app.sanic_app.router.finalize() self.asgi_app.sanic_app.router.finalize()
listeners = self.asgi_app.sanic_app.listeners.get( listeners = self.asgi_app.sanic_app.listeners.get(
"before_server_start", [] "before_server_start", []
@ -191,6 +192,7 @@ class ASGIApp:
async def create( async def create(
cls, sanic_app, scope: ASGIScope, receive: ASGIReceive, send: ASGISend cls, sanic_app, scope: ASGIScope, receive: ASGIReceive, send: ASGISend
) -> "ASGIApp": ) -> "ASGIApp":
raise Exception("create")
instance = cls() instance = cls()
instance.sanic_app = sanic_app instance.sanic_app = sanic_app
instance.transport = MockTransport(scope, receive, send) instance.transport = MockTransport(scope, receive, send)
@ -204,6 +206,7 @@ class ASGIApp:
] ]
) )
instance.lifespan = Lifespan(instance) instance.lifespan = Lifespan(instance)
print(instance.lifespan)
if scope["type"] == "lifespan": if scope["type"] == "lifespan":
await instance.lifespan(scope, receive, send) await instance.lifespan(scope, receive, send)
@ -293,4 +296,5 @@ class ASGIApp:
""" """
Handle the incoming request. Handle the incoming request.
""" """
print("......")
await self.sanic_app.handle_request(self.request) await self.sanic_app.handle_request(self.request)

View File

@ -115,8 +115,7 @@ class Blueprint(BaseSanic):
and self.strict_slashes is not None and self.strict_slashes is not None
else future.strict_slashes else future.strict_slashes
) )
name = app._generate_name(future.name)
print(uri, strict_slashes)
apply_route = FutureRoute( apply_route = FutureRoute(
future.handler, future.handler,
@ -126,7 +125,7 @@ class Blueprint(BaseSanic):
strict_slashes, strict_slashes,
future.stream, future.stream,
future.version or self.version, future.version or self.version,
future.name, name,
future.ignore_body, future.ignore_body,
future.websocket, future.websocket,
future.subprotocols, future.subprotocols,

View File

@ -29,6 +29,10 @@ class ExceptionMixin:
nonlocal apply nonlocal apply
nonlocal exceptions nonlocal exceptions
if isinstance(exceptions[0], list):
exceptions = tuple(*exceptions)
print(handler, exceptions)
future_exception = FutureException(handler, exceptions) future_exception = FutureException(handler, exceptions)
self._future_exceptions.add(future_exception) self._future_exceptions.add(future_exception)
if apply: if apply:

View File

@ -539,6 +539,7 @@ class RouteMixin:
def _generate_name(self, *objects) -> str: def _generate_name(self, *objects) -> str:
name = None name = None
for obj in objects: for obj in objects:
if obj: if obj:
if isinstance(obj, str): if isinstance(obj, str):
@ -546,9 +547,12 @@ class RouteMixin:
break break
try: try:
name = obj.__name__ name = obj.name
except AttributeError: except AttributeError:
continue try:
name = obj.__name__
except AttributeError:
continue
else: else:
break break

View File

@ -20,7 +20,7 @@ class Router(BaseRouter):
DEFAULT_METHOD = "GET" DEFAULT_METHOD = "GET"
ALLOWED_METHODS = HTTP_METHODS ALLOWED_METHODS = HTTP_METHODS
# @lru_cache @lru_cache
def get(self, request: Request): def get(self, request: Request):
""" """
Retrieve a `Route` object containg the details about how to handle Retrieve a `Route` object containg the details about how to handle
@ -42,6 +42,12 @@ class Router(BaseRouter):
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:
print(
"Method {} not allowed for URL {}".format(
request.method, request.path
),
e.allowed_methods,
)
raise MethodNotSupported( raise MethodNotSupported(
"Method {} not allowed for URL {}".format( "Method {} not allowed for URL {}".format(
request.method, request.path request.method, request.path
@ -175,8 +181,14 @@ class Router(BaseRouter):
if not view_name: if not view_name:
return None return None
name = self.ctx.app._generate_name(view_name) # TODO:
route = self.name_index.get(name) # - Check blueprint naming, we shouldn't need to double check here
# but it seems like blueprints are not receiving full names
# probably need tocheck the blueprint registration func
route = self.name_index.get(view_name)
if not route:
full_name = self.ctx.app._generate_name(view_name)
route = self.name_index.get(full_name)
if not route: if not route:
return None return None
@ -185,7 +197,16 @@ class Router(BaseRouter):
@property @property
def routes_all(self): def routes_all(self):
return { return self.routes
**self.static_routes,
**self.dynamic_routes, @property
} def routes_static(self):
return self.static_routes
@property
def routes_dynamic(self):
return self.dynamic_routes
@property
def routes_regex(self):
return self.regex_routes

View File

@ -159,7 +159,7 @@ def register(
# If we're not trying to match a file directly, # If we're not trying to match a file directly,
# serve from the folder # serve from the folder
if not path.isfile(file_or_directory): if not path.isfile(file_or_directory):
uri += "/<file_uri:path>" uri += "/<file_uri>"
# special prefix for static files # special prefix for static files
# if not static.name.startswith("_static_"): # if not static.name.startswith("_static_"):

View File

@ -4,6 +4,8 @@ from pytest import mark
import sanic.router import sanic.router
from sanic.request import Request
seed("Pack my box with five dozen liquor jugs.") seed("Pack my box with five dozen liquor jugs.")
@ -23,8 +25,17 @@ class TestSanicRouteResolution:
route_to_call = choice(simple_routes) route_to_call = choice(simple_routes)
result = benchmark.pedantic( result = benchmark.pedantic(
router._get, router.get,
("/{}".format(route_to_call[-1]), route_to_call[0], "localhost"), (
Request(
"/{}".format(route_to_call[-1]).encode(),
{"host": "localhost"},
"v1",
route_to_call[0],
None,
None,
),
),
iterations=1000, iterations=1000,
rounds=1000, rounds=1000,
) )
@ -47,8 +58,17 @@ class TestSanicRouteResolution:
print("{} -> {}".format(route_to_call[-1], url)) print("{} -> {}".format(route_to_call[-1], url))
result = benchmark.pedantic( result = benchmark.pedantic(
router._get, router.get,
("/{}".format(url), route_to_call[0], "localhost"), (
Request(
"/{}".format(url).encode(),
{"host": "localhost"},
"v1",
route_to_call[0],
None,
None,
),
),
iterations=1000, iterations=1000,
rounds=1000, rounds=1000,
) )

View File

@ -4,14 +4,16 @@ import string
import sys import sys
import uuid import uuid
from typing import Tuple
import pytest import pytest
from sanic_routing.exceptions import RouteExists
from sanic_testing import TestManager from sanic_testing import TestManager
from sanic import Sanic from sanic import Sanic
from sanic.constants import HTTP_METHODS
from sanic.router import Router
# from sanic.router import RouteExists, Router
random.seed("Pack my box with five dozen liquor jugs.") random.seed("Pack my box with five dozen liquor jugs.")
@ -40,12 +42,12 @@ async def _handler(request):
TYPE_TO_GENERATOR_MAP = { TYPE_TO_GENERATOR_MAP = {
"string": lambda: "".join( "string": lambda: "".join(
[random.choice(string.ascii_letters + string.digits) for _ in range(4)] [random.choice(string.ascii_lowercase) for _ in range(4)]
), ),
"int": lambda: random.choice(range(1000000)), "int": lambda: random.choice(range(1000000)),
"number": lambda: random.random(), "number": lambda: random.random(),
"alpha": lambda: "".join( "alpha": lambda: "".join(
[random.choice(string.ascii_letters) for _ in range(4)] [random.choice(string.ascii_lowercase) for _ in range(4)]
), ),
"uuid": lambda: str(uuid.uuid1()), "uuid": lambda: str(uuid.uuid1()),
} }
@ -54,7 +56,7 @@ TYPE_TO_GENERATOR_MAP = {
class RouteStringGenerator: class RouteStringGenerator:
ROUTE_COUNT_PER_DEPTH = 100 ROUTE_COUNT_PER_DEPTH = 100
HTTP_METHODS = ["GET", "PUT", "POST", "PATCH", "DELETE", "OPTION"] HTTP_METHODS = HTTP_METHODS
ROUTE_PARAM_TYPES = ["string", "int", "number", "alpha", "uuid"] ROUTE_PARAM_TYPES = ["string", "int", "number", "alpha", "uuid"]
def generate_random_direct_route(self, max_route_depth=4): def generate_random_direct_route(self, max_route_depth=4):
@ -106,25 +108,25 @@ class RouteStringGenerator:
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def sanic_router(app): def sanic_router(app):
... # noinspection PyProtectedMember
# # noinspection PyProtectedMember def _setup(route_details: tuple) -> Tuple[Router, tuple]:
# def _setup(route_details: tuple) -> (Router, tuple): router = Router()
# router = Router(app) added_router = []
# added_router = [] for method, route in route_details:
# for method, route in route_details: try:
# try: router.add(
# router._add( uri=f"/{route}",
# uri=f"/{route}", methods=frozenset({method}),
# methods=frozenset({method}), host="localhost",
# host="localhost", handler=_handler,
# handler=_handler, )
# ) added_router.append((method, route))
# added_router.append((method, route)) except RouteExists:
# except RouteExists: pass
# pass router.finalize()
# return router, added_router return router, added_router
# return _setup return _setup
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
@ -140,5 +142,4 @@ def url_param_generator():
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def app(request): def app(request):
app = Sanic(request.node.name) app = Sanic(request.node.name)
# TestManager(app)
return app return app

View File

@ -45,7 +45,8 @@ def protocol(transport):
return transport.get_protocol() return transport.get_protocol()
def test_listeners_triggered(app): def test_listeners_triggered():
app = Sanic("app")
before_server_start = False before_server_start = False
after_server_start = False after_server_start = False
before_server_stop = False before_server_stop = False
@ -53,6 +54,7 @@ def test_listeners_triggered(app):
@app.listener("before_server_start") @app.listener("before_server_start")
def do_before_server_start(*args, **kwargs): def do_before_server_start(*args, **kwargs):
raise Exception("......")
nonlocal before_server_start nonlocal before_server_start
before_server_start = True before_server_start = True
@ -78,8 +80,8 @@ def test_listeners_triggered(app):
config = uvicorn.Config(app=app, loop="asyncio", limit_max_requests=0) config = uvicorn.Config(app=app, loop="asyncio", limit_max_requests=0)
server = CustomServer(config=config) server = CustomServer(config=config)
with pytest.warns(UserWarning): # with pytest.warns(UserWarning):
server.run() server.run()
all_tasks = ( all_tasks = (
asyncio.Task.all_tasks() asyncio.Task.all_tasks()
@ -304,18 +306,24 @@ async def test_cookie_customization(app):
_, response = await app.asgi_client.get("/cookie") _, response = await app.asgi_client.get("/cookie")
CookieDef = namedtuple("CookieDef", ("value", "httponly")) CookieDef = namedtuple("CookieDef", ("value", "httponly"))
Cookie = namedtuple("Cookie", ("domain", "path", "value", "httponly"))
cookie_map = { cookie_map = {
"test": CookieDef("Cookie1", True), "test": CookieDef("Cookie1", True),
"c2": CookieDef("Cookie2", False), "c2": CookieDef("Cookie2", False),
} }
cookies = {
c.name: Cookie(c.domain, c.path, c.value, "HttpOnly" in c._rest.keys())
for c in response.cookies.jar
}
for name, definition in cookie_map.items(): for name, definition in cookie_map.items():
cookie = response.cookies.get(name) cookie = cookies.get(name)
assert cookie assert cookie
assert cookie.value == definition.value assert cookie.value == definition.value
assert cookie.get("domain") == "mockserver.local" assert cookie.domain == "mockserver.local"
assert cookie.get("path") == "/" assert cookie.path == "/"
assert cookie.get("httponly", False) == definition.httponly assert cookie.httponly == definition.httponly
@pytest.mark.asyncio @pytest.mark.asyncio

View File

@ -88,18 +88,18 @@ def test_bp_strict_slash(app):
app.blueprint(bp) app.blueprint(bp)
request, response = app.test_client.get("/get") # request, response = app.test_client.get("/get")
assert response.text == "OK" # assert response.text == "OK"
assert response.json is None # assert response.json is None
request, response = app.test_client.get("/get/") # request, response = app.test_client.get("/get/")
assert response.status == 404 # assert response.status == 404
request, response = app.test_client.post("/post/") request, response = app.test_client.post("/post/")
assert response.text == "OK" assert response.text == "OK"
request, response = app.test_client.post("/post") # request, response = app.test_client.post("/post")
assert response.status == 404 # assert response.status == 404
def test_bp_strict_slash_default_value(app): def test_bp_strict_slash_default_value(app):
@ -197,12 +197,7 @@ def test_several_bp_with_url_prefix(app):
def test_bp_with_host(app): def test_bp_with_host(app):
bp = Blueprint( bp = Blueprint("test_bp_host", url_prefix="/test1", host="example.com")
"test_bp_host",
url_prefix="/test1",
host="example.com",
strict_slashes=True,
)
@bp.route("/") @bp.route("/")
def handler1(request): def handler1(request):
@ -214,10 +209,9 @@ def test_bp_with_host(app):
app.blueprint(bp) app.blueprint(bp)
headers = {"Host": "example.com"} headers = {"Host": "example.com"}
app.router.finalize()
request, response = app.test_client.get("/test1/", headers=headers) request, response = app.test_client.get("/test1/", headers=headers)
assert response.body == b"Hello" assert response.text == "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)

View File

@ -103,7 +103,13 @@ def test_logging_pass_customer_logconfig():
assert fmt._fmt == modified_config["formatters"]["access"]["format"] assert fmt._fmt == modified_config["formatters"]["access"]["format"]
@pytest.mark.parametrize("debug", (True, False)) @pytest.mark.parametrize(
"debug",
(
True,
False,
),
)
def test_log_connection_lost(app, debug, monkeypatch): def test_log_connection_lost(app, debug, monkeypatch):
""" Should not log Connection lost exception on non debug """ """ Should not log Connection lost exception on non debug """
stream = StringIO() stream = StringIO()
@ -117,7 +123,7 @@ def test_log_connection_lost(app, debug, monkeypatch):
request.transport.close() request.transport.close()
return response return response
req, res = app.test_client.get("/conn_lost", debug=debug) req, res = app.test_client.get("/conn_lost", debug=debug, allow_none=True)
assert res is None assert res is None
log = stream.getvalue() log = stream.getvalue()

View File

@ -102,6 +102,7 @@ def test_middleware_response_raise_exception(app, caplog):
async def process_response(request, response): async def process_response(request, response):
raise Exception("Exception at response middleware") raise Exception("Exception at response middleware")
app.route("/")(lambda x: x)
with caplog.at_level(logging.ERROR): with caplog.at_level(logging.ERROR):
reqrequest, response = app.test_client.get("/fail") reqrequest, response = app.test_client.get("/fail")
@ -129,7 +130,7 @@ def test_middleware_override_request(app):
async def handler(request): async def handler(request):
return text("FAIL") return text("FAIL")
response = app.test_client.get("/", gather_request=False) _, response = app.test_client.get("/", gather_request=False)
assert response.status == 200 assert response.status == 200
assert response.text == "OK" assert response.text == "OK"

View File

@ -68,6 +68,7 @@ def handler(request):
@pytest.mark.parametrize("protocol", [3, 4]) @pytest.mark.parametrize("protocol", [3, 4])
def test_pickle_app(app, protocol): def test_pickle_app(app, protocol):
app.route("/")(handler) app.route("/")(handler)
app.router.finalize()
p_app = pickle.dumps(app, protocol=protocol) p_app = pickle.dumps(app, protocol=protocol)
del app del app
up_p_app = pickle.loads(p_app) up_p_app = pickle.loads(p_app)

View File

@ -5,6 +5,7 @@ import asyncio
import pytest import pytest
from sanic import Sanic
from sanic.blueprints import Blueprint from sanic.blueprints import Blueprint
from sanic.constants import HTTP_METHODS from sanic.constants import HTTP_METHODS
from sanic.exceptions import URLBuildError from sanic.exceptions import URLBuildError
@ -17,7 +18,9 @@ from sanic.response import text
@pytest.mark.parametrize("method", HTTP_METHODS) @pytest.mark.parametrize("method", HTTP_METHODS)
def test_versioned_named_routes_get(app, method): def test_versioned_named_routes_get(method):
app = Sanic("app")
bp = Blueprint("test_bp", url_prefix="/bp") bp = Blueprint("test_bp", url_prefix="/bp")
method = method.lower() method = method.lower()
@ -48,10 +51,24 @@ def test_versioned_named_routes_get(app, method):
app.blueprint(bp) app.blueprint(bp)
assert app.router.routes_all[f"/v1/{method}"].name == route_name assert (
app.router.routes_all[
(
"v1",
method,
)
].name
== f"app.{route_name}"
)
route = app.router.routes_all[f"/v1/bp/{method}"] route = app.router.routes_all[
assert route.name == f"test_bp.{route_name2}" (
"v1",
"bp",
method,
)
]
assert route.name == f"app.test_bp.{route_name2}"
assert app.url_for(route_name) == f"/v1/{method}" assert app.url_for(route_name) == f"/v1/{method}"
url = app.url_for(f"test_bp.{route_name2}") url = app.url_for(f"test_bp.{route_name2}")
@ -60,16 +77,19 @@ def test_versioned_named_routes_get(app, method):
app.url_for("handler") app.url_for("handler")
def test_shorthand_default_routes_get(app): def test_shorthand_default_routes_get():
app = Sanic("app")
@app.get("/get") @app.get("/get")
def handler(request): def handler(request):
return text("OK") return text("OK")
assert app.router.routes_all["/get"].name == "handler" assert app.router.routes_all[("get",)].name == "app.handler"
assert app.url_for("handler") == "/get" assert app.url_for("handler") == "/get"
def test_shorthand_named_routes_get(app): def test_shorthand_named_routes_get():
app = Sanic("app")
bp = Blueprint("test_bp", url_prefix="/bp") bp = Blueprint("test_bp", url_prefix="/bp")
@app.get("/get", name="route_get") @app.get("/get", name="route_get")
@ -82,84 +102,106 @@ def test_shorthand_named_routes_get(app):
app.blueprint(bp) app.blueprint(bp)
assert app.router.routes_all["/get"].name == "route_get" assert app.router.routes_all[("get",)].name == "app.route_get"
assert app.url_for("route_get") == "/get" assert app.url_for("route_get") == "/get"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
assert app.router.routes_all["/bp/get"].name == "test_bp.route_bp" assert (
app.router.routes_all[
(
"bp",
"get",
)
].name
== "app.test_bp.route_bp"
)
assert app.url_for("test_bp.route_bp") == "/bp/get" assert app.url_for("test_bp.route_bp") == "/bp/get"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("test_bp.handler2") app.url_for("test_bp.handler2")
def test_shorthand_named_routes_post(app): def test_shorthand_named_routes_post():
app = Sanic("app")
@app.post("/post", name="route_name") @app.post("/post", name="route_name")
def handler(request): def handler(request):
return text("OK") return text("OK")
assert app.router.routes_all["/post"].name == "route_name" assert app.router.routes_all[("post",)].name == "app.route_name"
assert app.url_for("route_name") == "/post" assert app.url_for("route_name") == "/post"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_shorthand_named_routes_put(app): def test_shorthand_named_routes_put():
app = Sanic("app")
@app.put("/put", name="route_put") @app.put("/put", name="route_put")
def handler(request): def handler(request):
return text("OK") return text("OK")
assert app.router.routes_all["/put"].name == "route_put" assert app.router.routes_all[("put",)].name == "app.route_put"
assert app.url_for("route_put") == "/put" assert app.url_for("route_put") == "/put"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_shorthand_named_routes_delete(app): def test_shorthand_named_routes_delete():
app = Sanic("app")
@app.delete("/delete", name="route_delete") @app.delete("/delete", name="route_delete")
def handler(request): def handler(request):
return text("OK") return text("OK")
assert app.router.routes_all["/delete"].name == "route_delete" assert app.router.routes_all[("delete",)].name == "app.route_delete"
assert app.url_for("route_delete") == "/delete" assert app.url_for("route_delete") == "/delete"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_shorthand_named_routes_patch(app): def test_shorthand_named_routes_patch():
app = Sanic("app")
@app.patch("/patch", name="route_patch") @app.patch("/patch", name="route_patch")
def handler(request): def handler(request):
return text("OK") return text("OK")
assert app.router.routes_all["/patch"].name == "route_patch" assert app.router.routes_all[("patch",)].name == "app.route_patch"
assert app.url_for("route_patch") == "/patch" assert app.url_for("route_patch") == "/patch"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_shorthand_named_routes_head(app): def test_shorthand_named_routes_head():
app = Sanic("app")
@app.head("/head", name="route_head") @app.head("/head", name="route_head")
def handler(request): def handler(request):
return text("OK") return text("OK")
assert app.router.routes_all["/head"].name == "route_head" assert app.router.routes_all[("head",)].name == "app.route_head"
assert app.url_for("route_head") == "/head" assert app.url_for("route_head") == "/head"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_shorthand_named_routes_options(app): def test_shorthand_named_routes_options():
app = Sanic("app")
@app.options("/options", name="route_options") @app.options("/options", name="route_options")
def handler(request): def handler(request):
return text("OK") return text("OK")
assert app.router.routes_all["/options"].name == "route_options" assert app.router.routes_all[("options",)].name == "app.route_options"
assert app.url_for("route_options") == "/options" assert app.url_for("route_options") == "/options"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_named_static_routes(app): def test_named_static_routes():
app = Sanic("app")
@app.route("/test", name="route_test") @app.route("/test", name="route_test")
async def handler1(request): async def handler1(request):
return text("OK1") return text("OK1")
@ -168,20 +210,21 @@ def test_named_static_routes(app):
async def handler2(request): async def handler2(request):
return text("OK2") return text("OK2")
assert app.router.routes_all["/test"].name == "route_test" assert app.router.routes_all[("test",)].name == "app.route_test"
assert app.router.routes_static["/test"].name == "route_test" assert app.router.routes_static[("test",)].name == "app.route_test"
assert app.url_for("route_test") == "/test" assert app.url_for("route_test") == "/test"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler1") app.url_for("handler1")
assert app.router.routes_all["/pizazz"].name == "route_pizazz" assert app.router.routes_all[("pizazz",)].name == "app.route_pizazz"
assert app.router.routes_static["/pizazz"].name == "route_pizazz" assert app.router.routes_static[("pizazz",)].name == "app.route_pizazz"
assert app.url_for("route_pizazz") == "/pizazz" assert app.url_for("route_pizazz") == "/pizazz"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler2") app.url_for("handler2")
def test_named_dynamic_route(app): def test_named_dynamic_route():
app = Sanic("app")
results = [] results = []
@app.route("/folder/<name>", name="route_dynamic") @app.route("/folder/<name>", name="route_dynamic")
@ -189,52 +232,83 @@ def test_named_dynamic_route(app):
results.append(name) results.append(name)
return text("OK") return text("OK")
assert app.router.routes_all["/folder/<name>"].name == "route_dynamic" assert (
app.router.routes_all[
(
"folder",
"<name>",
)
].name
== "app.route_dynamic"
)
assert app.url_for("route_dynamic", name="test") == "/folder/test" assert app.url_for("route_dynamic", name="test") == "/folder/test"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_dynamic_named_route_regex(app): def test_dynamic_named_route_regex():
app = Sanic("app")
@app.route("/folder/<folder_id:[A-Za-z0-9]{0,4}>", name="route_re") @app.route("/folder/<folder_id:[A-Za-z0-9]{0,4}>", name="route_re")
async def handler(request, folder_id): async def handler(request, folder_id):
return text("OK") return text("OK")
route = app.router.routes_all["/folder/<folder_id:[A-Za-z0-9]{0,4}>"] route = app.router.routes_all[
assert route.name == "route_re" (
"folder",
"<folder_id:[A-Za-z0-9]{0,4}>",
)
]
assert route.name == "app.route_re"
assert app.url_for("route_re", folder_id="test") == "/folder/test" assert app.url_for("route_re", folder_id="test") == "/folder/test"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_dynamic_named_route_path(app): def test_dynamic_named_route_path():
app = Sanic("app")
@app.route("/<path:path>/info", name="route_dynamic_path") @app.route("/<path:path>/info", name="route_dynamic_path")
async def handler(request, path): async def handler(request, path):
return text("OK") return text("OK")
route = app.router.routes_all["/<path:path>/info"] route = app.router.routes_all[
assert route.name == "route_dynamic_path" (
"<path:path>",
"info",
)
]
assert route.name == "app.route_dynamic_path"
assert app.url_for("route_dynamic_path", path="path/1") == "/path/1/info" assert app.url_for("route_dynamic_path", path="path/1") == "/path/1/info"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_dynamic_named_route_unhashable(app): def test_dynamic_named_route_unhashable():
app = Sanic("app")
@app.route( @app.route(
"/folder/<unhashable:[A-Za-z0-9/]+>/end/", name="route_unhashable" "/folder/<unhashable:[A-Za-z0-9/]+>/end/", name="route_unhashable"
) )
async def handler(request, unhashable): async def handler(request, unhashable):
return text("OK") return text("OK")
route = app.router.routes_all["/folder/<unhashable:[A-Za-z0-9/]+>/end/"] route = app.router.routes_all[
assert route.name == "route_unhashable" (
"folder",
"<unhashable:[A-Za-z0-9/]+>",
"end",
)
]
assert route.name == "app.route_unhashable"
url = app.url_for("route_unhashable", unhashable="test/asdf") url = app.url_for("route_unhashable", unhashable="test/asdf")
assert url == "/folder/test/asdf/end" assert url == "/folder/test/asdf/end"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_websocket_named_route(app): def test_websocket_named_route():
app = Sanic("app")
ev = asyncio.Event() ev = asyncio.Event()
@app.websocket("/ws", name="route_ws") @app.websocket("/ws", name="route_ws")
@ -242,26 +316,29 @@ def test_websocket_named_route(app):
assert ws.subprotocol is None assert ws.subprotocol is None
ev.set() ev.set()
assert app.router.routes_all["/ws"].name == "route_ws" assert app.router.routes_all[("ws",)].name == "app.route_ws"
assert app.url_for("route_ws") == "/ws" assert app.url_for("route_ws") == "/ws"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_websocket_named_route_with_subprotocols(app): def test_websocket_named_route_with_subprotocols():
app = Sanic("app")
results = [] results = []
@app.websocket("/ws", subprotocols=["foo", "bar"], name="route_ws") @app.websocket("/ws", subprotocols=["foo", "bar"], name="route_ws")
async def handler(request, ws): async def handler(request, ws):
results.append(ws.subprotocol) results.append(ws.subprotocol)
assert app.router.routes_all["/ws"].name == "route_ws" assert app.router.routes_all[("ws",)].name == "app.route_ws"
assert app.url_for("route_ws") == "/ws" assert app.url_for("route_ws") == "/ws"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_static_add_named_route(app): def test_static_add_named_route():
app = Sanic("app")
async def handler1(request): async def handler1(request):
return text("OK1") return text("OK1")
@ -271,20 +348,21 @@ def test_static_add_named_route(app):
app.add_route(handler1, "/test", name="route_test") app.add_route(handler1, "/test", name="route_test")
app.add_route(handler2, "/test2", name="route_test2") app.add_route(handler2, "/test2", name="route_test2")
assert app.router.routes_all["/test"].name == "route_test" assert app.router.routes_all[("test",)].name == "app.route_test"
assert app.router.routes_static["/test"].name == "route_test" assert app.router.routes_static[("test",)].name == "app.route_test"
assert app.url_for("route_test") == "/test" assert app.url_for("route_test") == "/test"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler1") app.url_for("handler1")
assert app.router.routes_all["/test2"].name == "route_test2" assert app.router.routes_all[("test2",)].name == "app.route_test2"
assert app.router.routes_static["/test2"].name == "route_test2" assert app.router.routes_static[("test2",)].name == "app.route_test2"
assert app.url_for("route_test2") == "/test2" assert app.url_for("route_test2") == "/test2"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler2") app.url_for("handler2")
def test_dynamic_add_named_route(app): def test_dynamic_add_named_route():
app = Sanic("app")
results = [] results = []
async def handler(request, name): async def handler(request, name):
@ -292,13 +370,17 @@ def test_dynamic_add_named_route(app):
return text("OK") return text("OK")
app.add_route(handler, "/folder/<name>", name="route_dynamic") app.add_route(handler, "/folder/<name>", name="route_dynamic")
assert app.router.routes_all["/folder/<name>"].name == "route_dynamic" assert (
app.router.routes_all[("folder", "<name>")].name == "app.route_dynamic"
)
assert app.url_for("route_dynamic", name="test") == "/folder/test" assert app.url_for("route_dynamic", name="test") == "/folder/test"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_dynamic_add_named_route_unhashable(app): def test_dynamic_add_named_route_unhashable():
app = Sanic("app")
async def handler(request, unhashable): async def handler(request, unhashable):
return text("OK") return text("OK")
@ -307,15 +389,23 @@ def test_dynamic_add_named_route_unhashable(app):
"/folder/<unhashable:[A-Za-z0-9/]+>/end/", "/folder/<unhashable:[A-Za-z0-9/]+>/end/",
name="route_unhashable", name="route_unhashable",
) )
route = app.router.routes_all["/folder/<unhashable:[A-Za-z0-9/]+>/end/"] route = app.router.routes_all[
assert route.name == "route_unhashable" (
"folder",
"<unhashable:[A-Za-z0-9/]+>",
"end",
)
]
assert route.name == "app.route_unhashable"
url = app.url_for("route_unhashable", unhashable="folder1") url = app.url_for("route_unhashable", unhashable="folder1")
assert url == "/folder/folder1/end" assert url == "/folder/folder1/end"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler") app.url_for("handler")
def test_overload_routes(app): def test_overload_routes():
app = Sanic("app")
@app.route("/overload", methods=["GET"], name="route_first") @app.route("/overload", methods=["GET"], name="route_first")
async def handler1(request): async def handler1(request):
return text("OK1") return text("OK1")
@ -342,7 +432,7 @@ def test_overload_routes(app):
request, response = app.test_client.put(app.url_for("route_second")) request, response = app.test_client.put(app.url_for("route_second"))
assert response.text == "OK2" assert response.text == "OK2"
assert app.router.routes_all["/overload"].name == "route_first" assert app.router.routes_all[("overload",)].name == "app.route_first"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):
app.url_for("handler1") app.url_for("handler1")

View File

@ -13,7 +13,7 @@ def test_payload_too_large_from_error_handler(app):
def handler_exception(request, exception): def handler_exception(request, exception):
return text("Payload Too Large from error_handler.", 413) return text("Payload Too Large from error_handler.", 413)
response = app.test_client.get("/1", gather_request=False) _, response = app.test_client.get("/1", gather_request=False)
assert response.status == 413 assert response.status == 413
assert response.text == "Payload Too Large from error_handler." assert response.text == "Payload Too Large from error_handler."
@ -25,7 +25,7 @@ def test_payload_too_large_at_data_received_default(app):
async def handler2(request): async def handler2(request):
return text("OK") return text("OK")
response = app.test_client.get("/1", gather_request=False) _, response = app.test_client.get("/1", gather_request=False)
assert response.status == 413 assert response.status == 413
assert "Request header" in response.text assert "Request header" in response.text
@ -38,6 +38,6 @@ def test_payload_too_large_at_on_header_default(app):
return text("OK") return text("OK")
data = "a" * 1000 data = "a" * 1000
response = app.test_client.post("/1", gather_request=False, data=data) _, response = app.test_client.post("/1", gather_request=False, data=data)
assert response.status == 413 assert response.status == 413
assert "Request body" in response.text assert "Request body" in response.text

View File

@ -1,4 +1,4 @@
from urllib.parse import quote from urllib.parse import quote, unquote
import pytest import pytest
@ -109,7 +109,14 @@ def test_redirect_with_header_injection(redirect_app):
assert not response.text.startswith("test-body") assert not response.text.startswith("test-body")
@pytest.mark.parametrize("test_str", ["sanic-test", "sanictest", "sanic test"]) @pytest.mark.parametrize(
"test_str",
[
"sanic-test",
"sanictest",
"sanic test",
],
)
def test_redirect_with_params(app, test_str): def test_redirect_with_params(app, test_str):
use_in_uri = quote(test_str) use_in_uri = quote(test_str)
@ -117,7 +124,7 @@ def test_redirect_with_params(app, test_str):
async def init_handler(request, test): async def init_handler(request, test):
return redirect(f"/api/v2/test/{use_in_uri}/") return redirect(f"/api/v2/test/{use_in_uri}/")
@app.route("/api/v2/test/<test>/") @app.route("/api/v2/test/<test>/", unquote=True)
async def target_handler(request, test): async def target_handler(request, test):
assert test == test_str assert test == test_str
return text("OK") return text("OK")
@ -125,4 +132,4 @@ def test_redirect_with_params(app, test_str):
_, response = app.test_client.get(f"/api/v1/test/{use_in_uri}/") _, response = app.test_client.get(f"/api/v1/test/{use_in_uri}/")
assert response.status == 200 assert response.status == 200
assert response.content == b"OK" assert response.body == b"OK"

View File

@ -42,6 +42,8 @@ def write_app(filename, **runargs):
app = Sanic(__name__) app = Sanic(__name__)
app.route("/")(lambda x: x)
@app.listener("after_server_start") @app.listener("after_server_start")
def complete(*args): def complete(*args):
print("complete", os.getpid(), {text!r}) print("complete", os.getpid(), {text!r})

View File

@ -7,6 +7,7 @@ from json import loads as json_loads
from urllib.parse import urlparse from urllib.parse import urlparse
import pytest import pytest
import ujson
from sanic_testing.testing import ( from sanic_testing.testing import (
ASGI_BASE_URL, ASGI_BASE_URL,
@ -19,7 +20,7 @@ from sanic_testing.testing import (
from sanic import Blueprint, Sanic from sanic import Blueprint, Sanic
from sanic.exceptions import ServerError from sanic.exceptions import ServerError
from sanic.request import DEFAULT_HTTP_CONTENT_TYPE, Request, RequestParameters from sanic.request import DEFAULT_HTTP_CONTENT_TYPE, RequestParameters
from sanic.response import html, json, text from sanic.response import html, json, text
@ -35,7 +36,7 @@ def test_sync(app):
request, response = app.test_client.get("/") request, response = app.test_client.get("/")
assert response.text == "Hello" assert response.body == b"Hello"
@pytest.mark.asyncio @pytest.mark.asyncio
@ -46,7 +47,7 @@ async def test_sync_asgi(app):
request, response = await app.asgi_client.get("/") request, response = await app.asgi_client.get("/")
assert response.text == "Hello" assert response.body == b"Hello"
def test_ip(app): def test_ip(app):
@ -56,7 +57,7 @@ def test_ip(app):
request, response = app.test_client.get("/") request, response = app.test_client.get("/")
assert response.text == "127.0.0.1" assert response.body == b"127.0.0.1"
@pytest.mark.asyncio @pytest.mark.asyncio
@ -67,10 +68,12 @@ async def test_url_asgi(app):
request, response = await app.asgi_client.get("/") request, response = await app.asgi_client.get("/")
if response.text.endswith("/") and not ASGI_BASE_URL.endswith("/"): if response.body.decode().endswith("/") and not ASGI_BASE_URL.endswith(
response.text[:-1] == ASGI_BASE_URL "/"
):
response.body[:-1] == ASGI_BASE_URL.encode()
else: else:
assert response.text == ASGI_BASE_URL assert response.body == ASGI_BASE_URL.encode()
def test_text(app): def test_text(app):
@ -80,7 +83,7 @@ def test_text(app):
request, response = app.test_client.get("/") request, response = app.test_client.get("/")
assert response.text == "Hello" assert response.body == b"Hello"
def test_html(app): def test_html(app):
@ -109,13 +112,13 @@ def test_html(app):
request, response = app.test_client.get("/") request, response = app.test_client.get("/")
assert response.content_type == "text/html; charset=utf-8" assert response.content_type == "text/html; charset=utf-8"
assert response.text == "<h1>Hello</h1>" assert response.body == b"<h1>Hello</h1>"
request, response = app.test_client.get("/foo") request, response = app.test_client.get("/foo")
assert response.text == "<h1>Foo</h1>" assert response.body == b"<h1>Foo</h1>"
request, response = app.test_client.get("/bar") request, response = app.test_client.get("/bar")
assert response.text == "<h1>Bar object repr</h1>" assert response.body == b"<h1>Bar object repr</h1>"
@pytest.mark.asyncio @pytest.mark.asyncio
@ -126,7 +129,7 @@ async def test_text_asgi(app):
request, response = await app.asgi_client.get("/") request, response = await app.asgi_client.get("/")
assert response.text == "Hello" assert response.body == b"Hello"
def test_headers(app): def test_headers(app):
@ -186,7 +189,7 @@ def test_invalid_response(app):
request, response = app.test_client.get("/") request, response = app.test_client.get("/")
assert response.status == 500 assert response.status == 500
assert response.text == "Internal Server Error." assert response.body == b"Internal Server Error."
@pytest.mark.asyncio @pytest.mark.asyncio
@ -201,7 +204,7 @@ async def test_invalid_response_asgi(app):
request, response = await app.asgi_client.get("/") request, response = await app.asgi_client.get("/")
assert response.status == 500 assert response.status == 500
assert response.text == "Internal Server Error." assert response.body == b"Internal Server Error."
def test_json(app): def test_json(app):
@ -224,7 +227,7 @@ async def test_json_asgi(app):
request, response = await app.asgi_client.get("/") request, response = await app.asgi_client.get("/")
results = json_loads(response.text) results = json_loads(response.body)
assert results.get("test") is True assert results.get("test") is True
@ -237,7 +240,7 @@ def test_empty_json(app):
request, response = app.test_client.get("/") request, response = app.test_client.get("/")
assert response.status == 200 assert response.status == 200
assert response.text == "null" assert response.body == b"null"
@pytest.mark.asyncio @pytest.mark.asyncio
@ -249,7 +252,7 @@ async def test_empty_json_asgi(app):
request, response = await app.asgi_client.get("/") request, response = await app.asgi_client.get("/")
assert response.status == 200 assert response.status == 200
assert response.text == "null" assert response.body == b"null"
def test_invalid_json(app): def test_invalid_json(app):
@ -423,12 +426,12 @@ def test_content_type(app):
request, response = app.test_client.get("/") request, response = app.test_client.get("/")
assert request.content_type == DEFAULT_HTTP_CONTENT_TYPE assert request.content_type == DEFAULT_HTTP_CONTENT_TYPE
assert response.text == DEFAULT_HTTP_CONTENT_TYPE assert response.body.decode() == DEFAULT_HTTP_CONTENT_TYPE
headers = {"content-type": "application/json"} headers = {"content-type": "application/json"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.content_type == "application/json" assert request.content_type == "application/json"
assert response.text == "application/json" assert response.body == b"application/json"
@pytest.mark.asyncio @pytest.mark.asyncio
@ -439,12 +442,12 @@ async def test_content_type_asgi(app):
request, response = await app.asgi_client.get("/") request, response = await app.asgi_client.get("/")
assert request.content_type == DEFAULT_HTTP_CONTENT_TYPE assert request.content_type == DEFAULT_HTTP_CONTENT_TYPE
assert response.text == DEFAULT_HTTP_CONTENT_TYPE assert response.body.decode() == DEFAULT_HTTP_CONTENT_TYPE
headers = {"content-type": "application/json"} headers = {"content-type": "application/json"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.content_type == "application/json" assert request.content_type == "application/json"
assert response.text == "application/json" assert response.body == b"application/json"
def test_standard_forwarded(app): def test_standard_forwarded(app):
@ -581,14 +584,15 @@ async def test_standard_forwarded_asgi(app):
"X-Scheme": "ws", "X-Scheme": "ws",
} }
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == {"for": "127.0.0.2", "proto": "ws"}
assert response.json == {"for": "127.0.0.2", "proto": "ws"}
assert request.remote_addr == "127.0.0.2" assert request.remote_addr == "127.0.0.2"
assert request.scheme == "ws" assert request.scheme == "ws"
assert request.server_port == ASGI_PORT assert request.server_port == ASGI_PORT
app.config.FORWARDED_SECRET = "mySecret" app.config.FORWARDED_SECRET = "mySecret"
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == { assert response.json == {
"for": "[::2]", "for": "[::2]",
"proto": "https", "proto": "https",
"host": "me.tld", "host": "me.tld",
@ -603,13 +607,13 @@ async def test_standard_forwarded_asgi(app):
# Empty Forwarded header -> use X-headers # Empty Forwarded header -> use X-headers
headers["Forwarded"] = "" headers["Forwarded"] = ""
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == {"for": "127.0.0.2", "proto": "ws"} assert response.json == {"for": "127.0.0.2", "proto": "ws"}
# Header present but not matching anything # Header present but not matching anything
request, response = await app.asgi_client.get( request, response = await app.asgi_client.get(
"/", headers={"Forwarded": "."} "/", headers={"Forwarded": "."}
) )
assert response.json() == {} assert response.json == {}
# Forwarded header present but no matching secret -> use X-headers # Forwarded header present but no matching secret -> use X-headers
headers = { headers = {
@ -617,13 +621,13 @@ async def test_standard_forwarded_asgi(app):
"X-Real-IP": "127.0.0.2", "X-Real-IP": "127.0.0.2",
} }
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == {"for": "127.0.0.2"} assert response.json == {"for": "127.0.0.2"}
assert request.remote_addr == "127.0.0.2" assert request.remote_addr == "127.0.0.2"
# Different formatting and hitting both ends of the header # Different formatting and hitting both ends of the header
headers = {"Forwarded": 'Secret="mySecret";For=127.0.0.4;Port=1234'} headers = {"Forwarded": 'Secret="mySecret";For=127.0.0.4;Port=1234'}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == { assert response.json == {
"for": "127.0.0.4", "for": "127.0.0.4",
"port": 1234, "port": 1234,
"secret": "mySecret", "secret": "mySecret",
@ -632,7 +636,7 @@ async def test_standard_forwarded_asgi(app):
# Test escapes (modify this if you see anyone implementing quoted-pairs) # Test escapes (modify this if you see anyone implementing quoted-pairs)
headers = {"Forwarded": 'for=test;quoted="\\,x=x;y=\\";secret=mySecret'} headers = {"Forwarded": 'for=test;quoted="\\,x=x;y=\\";secret=mySecret'}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == { assert response.json == {
"for": "test", "for": "test",
"quoted": "\\,x=x;y=\\", "quoted": "\\,x=x;y=\\",
"secret": "mySecret", "secret": "mySecret",
@ -641,17 +645,17 @@ async def test_standard_forwarded_asgi(app):
# Secret insulated by malformed field #1 # Secret insulated by malformed field #1
headers = {"Forwarded": "for=test;secret=mySecret;b0rked;proto=wss;"} headers = {"Forwarded": "for=test;secret=mySecret;b0rked;proto=wss;"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == {"for": "test", "secret": "mySecret"} assert response.json == {"for": "test", "secret": "mySecret"}
# Secret insulated by malformed field #2 # Secret insulated by malformed field #2
headers = {"Forwarded": "for=test;b0rked;secret=mySecret;proto=wss"} headers = {"Forwarded": "for=test;b0rked;secret=mySecret;proto=wss"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == {"proto": "wss", "secret": "mySecret"} assert response.json == {"proto": "wss", "secret": "mySecret"}
# Unexpected termination should not lose existing acceptable values # Unexpected termination should not lose existing acceptable values
headers = {"Forwarded": "b0rked;secret=mySecret;proto=wss"} headers = {"Forwarded": "b0rked;secret=mySecret;proto=wss"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == {"proto": "wss", "secret": "mySecret"} assert response.json == {"proto": "wss", "secret": "mySecret"}
# Field normalization # Field normalization
headers = { headers = {
@ -659,7 +663,7 @@ async def test_standard_forwarded_asgi(app):
'PATH="/With%20Spaces%22Quoted%22/sanicApp?key=val";SECRET=mySecret' 'PATH="/With%20Spaces%22Quoted%22/sanicApp?key=val";SECRET=mySecret'
} }
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == { assert response.json == {
"proto": "wss", "proto": "wss",
"by": "[cafe::8000]", "by": "[cafe::8000]",
"host": "a:2", "host": "a:2",
@ -671,7 +675,10 @@ async def test_standard_forwarded_asgi(app):
app.config.FORWARDED_SECRET = "_proxySecret" app.config.FORWARDED_SECRET = "_proxySecret"
headers = {"Forwarded": "for=1.2.3.4; by=_proxySecret"} headers = {"Forwarded": "for=1.2.3.4; by=_proxySecret"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert response.json() == {"for": "1.2.3.4", "by": "_proxySecret"} assert response.json == {
"for": "1.2.3.4",
"by": "_proxySecret",
}
def test_remote_addr_with_two_proxies(app): def test_remote_addr_with_two_proxies(app):
@ -685,33 +692,33 @@ def test_remote_addr_with_two_proxies(app):
headers = {"X-Real-IP": "127.0.0.2", "X-Forwarded-For": "127.0.1.1"} headers = {"X-Real-IP": "127.0.0.2", "X-Forwarded-For": "127.0.1.1"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.2" assert request.remote_addr == "127.0.0.2"
assert response.text == "127.0.0.2" assert response.body == b"127.0.0.2"
headers = {"X-Forwarded-For": "127.0.1.1"} headers = {"X-Forwarded-For": "127.0.1.1"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"X-Forwarded-For": "127.0.0.1, 127.0.1.2"} headers = {"X-Forwarded-For": "127.0.0.1, 127.0.1.2"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.1" assert request.remote_addr == "127.0.0.1"
assert response.text == "127.0.0.1" assert response.body == b"127.0.0.1"
request, response = app.test_client.get("/") request, response = app.test_client.get("/")
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"X-Forwarded-For": "127.0.0.1, , ,,127.0.1.2"} headers = {"X-Forwarded-For": "127.0.0.1, , ,,127.0.1.2"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.1" assert request.remote_addr == "127.0.0.1"
assert response.text == "127.0.0.1" assert response.body == b"127.0.0.1"
headers = { headers = {
"X-Forwarded-For": ", 127.0.2.2, , ,127.0.0.1, , ,,127.0.1.2" "X-Forwarded-For": ", 127.0.2.2, , ,127.0.0.1, , ,,127.0.1.2"
} }
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.1" assert request.remote_addr == "127.0.0.1"
assert response.text == "127.0.0.1" assert response.body == b"127.0.0.1"
@pytest.mark.asyncio @pytest.mark.asyncio
@ -726,33 +733,33 @@ async def test_remote_addr_with_two_proxies_asgi(app):
headers = {"X-Real-IP": "127.0.0.2", "X-Forwarded-For": "127.0.1.1"} headers = {"X-Real-IP": "127.0.0.2", "X-Forwarded-For": "127.0.1.1"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.2" assert request.remote_addr == "127.0.0.2"
assert response.text == "127.0.0.2" assert response.body == b"127.0.0.2"
headers = {"X-Forwarded-For": "127.0.1.1"} headers = {"X-Forwarded-For": "127.0.1.1"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"X-Forwarded-For": "127.0.0.1, 127.0.1.2"} headers = {"X-Forwarded-For": "127.0.0.1, 127.0.1.2"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.1" assert request.remote_addr == "127.0.0.1"
assert response.text == "127.0.0.1" assert response.body == b"127.0.0.1"
request, response = await app.asgi_client.get("/") request, response = await app.asgi_client.get("/")
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"X-Forwarded-For": "127.0.0.1, , ,,127.0.1.2"} headers = {"X-Forwarded-For": "127.0.0.1, , ,,127.0.1.2"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.1" assert request.remote_addr == "127.0.0.1"
assert response.text == "127.0.0.1" assert response.body == b"127.0.0.1"
headers = { headers = {
"X-Forwarded-For": ", 127.0.2.2, , ,127.0.0.1, , ,,127.0.1.2" "X-Forwarded-For": ", 127.0.2.2, , ,127.0.0.1, , ,,127.0.1.2"
} }
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.1" assert request.remote_addr == "127.0.0.1"
assert response.text == "127.0.0.1" assert response.body == b"127.0.0.1"
def test_remote_addr_without_proxy(app): def test_remote_addr_without_proxy(app):
@ -765,17 +772,17 @@ def test_remote_addr_without_proxy(app):
headers = {"X-Real-IP": "127.0.0.2", "X-Forwarded-For": "127.0.1.1"} headers = {"X-Real-IP": "127.0.0.2", "X-Forwarded-For": "127.0.1.1"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"X-Forwarded-For": "127.0.1.1"} headers = {"X-Forwarded-For": "127.0.1.1"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"X-Forwarded-For": "127.0.0.1, 127.0.1.2"} headers = {"X-Forwarded-For": "127.0.0.1, 127.0.1.2"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
@pytest.mark.asyncio @pytest.mark.asyncio
@ -789,17 +796,17 @@ async def test_remote_addr_without_proxy_asgi(app):
headers = {"X-Real-IP": "127.0.0.2", "X-Forwarded-For": "127.0.1.1"} headers = {"X-Real-IP": "127.0.0.2", "X-Forwarded-For": "127.0.1.1"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"X-Forwarded-For": "127.0.1.1"} headers = {"X-Forwarded-For": "127.0.1.1"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"X-Forwarded-For": "127.0.0.1, 127.0.1.2"} headers = {"X-Forwarded-For": "127.0.0.1, 127.0.1.2"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
def test_remote_addr_custom_headers(app): def test_remote_addr_custom_headers(app):
@ -814,17 +821,17 @@ def test_remote_addr_custom_headers(app):
headers = {"X-Real-IP": "127.0.0.2", "Forwarded": "127.0.1.1"} headers = {"X-Real-IP": "127.0.0.2", "Forwarded": "127.0.1.1"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "127.0.1.1" assert request.remote_addr == "127.0.1.1"
assert response.text == "127.0.1.1" assert response.body == b"127.0.1.1"
headers = {"X-Forwarded-For": "127.0.1.1"} headers = {"X-Forwarded-For": "127.0.1.1"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"Client-IP": "127.0.0.2", "Forwarded": "127.0.1.1"} headers = {"Client-IP": "127.0.0.2", "Forwarded": "127.0.1.1"}
request, response = app.test_client.get("/", headers=headers) request, response = app.test_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.2" assert request.remote_addr == "127.0.0.2"
assert response.text == "127.0.0.2" assert response.body == b"127.0.0.2"
@pytest.mark.asyncio @pytest.mark.asyncio
@ -840,17 +847,17 @@ async def test_remote_addr_custom_headers_asgi(app):
headers = {"X-Real-IP": "127.0.0.2", "Forwarded": "127.0.1.1"} headers = {"X-Real-IP": "127.0.0.2", "Forwarded": "127.0.1.1"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "127.0.1.1" assert request.remote_addr == "127.0.1.1"
assert response.text == "127.0.1.1" assert response.body == b"127.0.1.1"
headers = {"X-Forwarded-For": "127.0.1.1"} headers = {"X-Forwarded-For": "127.0.1.1"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "" assert request.remote_addr == ""
assert response.text == "" assert response.body == b""
headers = {"Client-IP": "127.0.0.2", "Forwarded": "127.0.1.1"} headers = {"Client-IP": "127.0.0.2", "Forwarded": "127.0.1.1"}
request, response = await app.asgi_client.get("/", headers=headers) request, response = await app.asgi_client.get("/", headers=headers)
assert request.remote_addr == "127.0.0.2" assert request.remote_addr == "127.0.0.2"
assert response.text == "127.0.0.2" assert response.body == b"127.0.0.2"
def test_forwarded_scheme(app): def test_forwarded_scheme(app):
@ -894,7 +901,7 @@ async def test_match_info_asgi(app):
request, response = await app.asgi_client.get("/api/v1/user/sanic_user/") request, response = await app.asgi_client.get("/api/v1/user/sanic_user/")
assert request.match_info == {"user_id": "sanic_user"} assert request.match_info == {"user_id": "sanic_user"}
assert json_loads(response.text) == {"user_id": "sanic_user"} assert json_loads(response.body) == {"user_id": "sanic_user"}
# ------------------------------------------------------------ # # ------------------------------------------------------------ #
@ -916,7 +923,7 @@ def test_post_json(app):
assert request.json.get("test") == "OK" assert request.json.get("test") == "OK"
assert request.json.get("test") == "OK" # for request.parsed_json assert request.json.get("test") == "OK" # for request.parsed_json
assert response.text == "OK" assert response.body == b"OK"
@pytest.mark.asyncio @pytest.mark.asyncio
@ -934,7 +941,7 @@ async def test_post_json_asgi(app):
assert request.json.get("test") == "OK" assert request.json.get("test") == "OK"
assert request.json.get("test") == "OK" # for request.parsed_json assert request.json.get("test") == "OK" # for request.parsed_json
assert response.text == "OK" assert response.body == b"OK"
def test_post_form_urlencoded(app): def test_post_form_urlencoded(app):
@ -2136,7 +2143,7 @@ def test_safe_method_with_body_ignored(app):
assert request.body == b"" assert request.body == b""
assert request.json == None assert request.json == None
assert response.text == "OK" assert response.body == b"OK"
def test_safe_method_with_body(app): def test_safe_method_with_body(app):
@ -2153,4 +2160,4 @@ def test_safe_method_with_body(app):
assert request.body == data.encode("utf-8") assert request.body == data.encode("utf-8")
assert request.json.get("test") == "OK" assert request.json.get("test") == "OK"
assert response.text == "OK" assert response.body == b"OK"

View File

@ -14,6 +14,7 @@ import pytest
from aiofiles import os as async_os from aiofiles import os as async_os
from sanic_testing.testing import HOST, PORT from sanic_testing.testing import HOST, PORT
from sanic import Sanic
from sanic.response import ( from sanic.response import (
HTTPResponse, HTTPResponse,
StreamingHTTPResponse, StreamingHTTPResponse,
@ -51,16 +52,22 @@ async def sample_streaming_fn(response):
await response.write("bar") await response.write("bar")
def test_method_not_allowed(app): def test_method_not_allowed():
app = Sanic("app")
@app.get("/") @app.get("/")
async def test_get(request): async def test_get(request):
return response.json({"hello": "world"}) return response.json({"hello": "world"})
request, response = app.test_client.head("/") request, response = app.test_client.head("/")
assert response.headers["Allow"] == "GET" assert set(response.headers["Allow"].split(", ")) == {
"GET",
}
request, response = app.test_client.post("/") request, response = app.test_client.post("/")
assert response.headers["Allow"] == "GET" assert set(response.headers["Allow"].split(", ")) == {"GET", "HEAD"}
app.router.reset()
@app.post("/") @app.post("/")
async def test_post(request): async def test_post(request):
@ -68,12 +75,20 @@ def test_method_not_allowed(app):
request, response = app.test_client.head("/") request, response = app.test_client.head("/")
assert response.status == 405 assert response.status == 405
assert set(response.headers["Allow"].split(", ")) == {"GET", "POST"} assert set(response.headers["Allow"].split(", ")) == {
"GET",
"POST",
"HEAD",
}
assert response.headers["Content-Length"] == "0" assert response.headers["Content-Length"] == "0"
request, response = app.test_client.patch("/") request, response = app.test_client.patch("/")
assert response.status == 405 assert response.status == 405
assert set(response.headers["Allow"].split(", ")) == {"GET", "POST"} assert set(response.headers["Allow"].split(", ")) == {
"GET",
"POST",
"HEAD",
}
assert response.headers["Content-Length"] == "0" assert response.headers["Content-Length"] == "0"
@ -237,7 +252,7 @@ def test_chunked_streaming_returns_correct_content(streaming_app):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_chunked_streaming_returns_correct_content_asgi(streaming_app): async def test_chunked_streaming_returns_correct_content_asgi(streaming_app):
request, response = await streaming_app.asgi_client.get("/") request, response = await streaming_app.asgi_client.get("/")
assert response.text == "foo,bar" assert response.body == b"foo,bar"
def test_non_chunked_streaming_adds_correct_headers(non_chunked_streaming_app): def test_non_chunked_streaming_adds_correct_headers(non_chunked_streaming_app):

View File

@ -574,44 +574,46 @@ def test_dynamic_route_uuid(app):
assert response.status == 404 assert response.status == 404
# def test_dynamic_route_path(app): def test_dynamic_route_path(app):
# @app.route("/<path:path>/info") @app.route("/<path:path>/info")
# async def handler(request, path): async def handler(request, path):
# return text("OK") return text("OK")
# request, response = app.test_client.get("/path/1/info") request, response = app.test_client.get("/path/1/info")
# assert response.status == 200 assert response.status == 200
# request, response = app.test_client.get("/info") request, response = app.test_client.get("/info")
# assert response.status == 404 assert response.status == 404
# @app.route("/<path:path>") app.router.reset()
# async def handler1(request, path):
# return text("OK")
# request, response = app.test_client.get("/info") @app.route("/<path:path>")
# assert response.status == 200 async def handler1(request, path):
return text("OK")
# request, response = app.test_client.get("/whatever/you/set") request, response = app.test_client.get("/info")
# assert response.status == 200 assert response.status == 200
request, response = app.test_client.get("/whatever/you/set")
assert response.status == 200
# def test_dynamic_route_unhashable(app): def test_dynamic_route_unhashable(app):
# @app.route("/folder/<unhashable:[A-Za-z0-9/]+>/end/") @app.route("/folder/<unhashable:[A-Za-z0-9/]+>/end/")
# async def handler(request, unhashable): async def handler(request, unhashable):
# return text("OK") return text("OK")
# request, response = app.test_client.get("/folder/test/asdf/end/") request, response = app.test_client.get("/folder/test/asdf/end/")
# assert response.status == 200 assert response.status == 200
# request, response = app.test_client.get("/folder/test///////end/") request, response = app.test_client.get("/folder/test///////end/")
# assert response.status == 200 assert response.status == 200
# request, response = app.test_client.get("/folder/test/end/") request, response = app.test_client.get("/folder/test/end/")
# assert response.status == 200 assert response.status == 200
# request, response = app.test_client.get("/folder/test/nope/") request, response = app.test_client.get("/folder/test/nope/")
# assert response.status == 404 assert response.status == 404
@pytest.mark.parametrize("url", ["/ws", "ws"]) @pytest.mark.parametrize("url", ["/ws", "ws"])
@ -629,17 +631,17 @@ def test_websocket_route(app, url):
assert ev.is_set() assert ev.is_set()
# @pytest.mark.asyncio @pytest.mark.asyncio
# @pytest.mark.parametrize("url", ["/ws", "ws"]) @pytest.mark.parametrize("url", ["/ws", "ws"])
# async def test_websocket_route_asgi(app, url): async def test_websocket_route_asgi(app, url):
# ev = asyncio.Event() ev = asyncio.Event()
# @app.websocket(url) @app.websocket(url)
# async def handler(request, ws): async def handler(request, ws):
# ev.set() ev.set()
# request, response = await app.asgi_client.websocket(url) request, response = await app.asgi_client.websocket(url)
# assert ev.is_set() assert ev.is_set()
def test_websocket_route_with_subprotocols(app): def test_websocket_route_with_subprotocols(app):
@ -878,23 +880,23 @@ def test_dynamic_add_route_regex(app):
assert response.status == 200 assert response.status == 200
# def test_dynamic_add_route_unhashable(app): def test_dynamic_add_route_unhashable(app):
# async def handler(request, unhashable): async def handler(request, unhashable):
# return text("OK") return text("OK")
# app.add_route(handler, "/folder/<unhashable:[A-Za-z0-9/]+>/end/") app.add_route(handler, "/folder/<unhashable:[A-Za-z0-9/]+>/end/")
# request, response = app.test_client.get("/folder/test/asdf/end/") request, response = app.test_client.get("/folder/test/asdf/end/")
# assert response.status == 200 assert response.status == 200
# request, response = app.test_client.get("/folder/test///////end/") request, response = app.test_client.get("/folder/test///////end/")
# assert response.status == 200 assert response.status == 200
# request, response = app.test_client.get("/folder/test/end/") request, response = app.test_client.get("/folder/test/end/")
# assert response.status == 200 assert response.status == 200
# request, response = app.test_client.get("/folder/test/nope/") request, response = app.test_client.get("/folder/test/nope/")
# assert response.status == 404 assert response.status == 404
def test_add_route_duplicate(app): def test_add_route_duplicate(app):

View File

@ -7,6 +7,7 @@ import pytest as pytest
from sanic_testing.testing import HOST as test_host from sanic_testing.testing import HOST as test_host
from sanic_testing.testing import PORT as test_port from sanic_testing.testing import PORT as test_port
from sanic import Sanic
from sanic.blueprints import Blueprint from sanic.blueprints import Blueprint
from sanic.exceptions import URLBuildError from sanic.exceptions import URLBuildError
from sanic.response import text from sanic.response import text
@ -98,15 +99,16 @@ def test_url_for_with_server_name(app):
assert response.text == "this should pass" assert response.text == "this should pass"
def test_fails_if_endpoint_not_found(app): def test_fails_if_endpoint_not_found():
app = Sanic("app")
@app.route("/fail") @app.route("/fail")
def fail(request): def fail(request):
return text("this should fail") return text("this should fail")
with pytest.raises(URLBuildError) as e: with pytest.raises(URLBuildError) as e:
app.url_for("passes") app.url_for("passes")
e.match("Endpoint with name `app.passes` was not found")
assert str(e.value) == "Endpoint with name `passes` was not found"
def test_fails_url_build_if_param_not_passed(app): def test_fails_url_build_if_param_not_passed(app):
@ -251,7 +253,8 @@ def test_adds_other_supplied_values_as_query_string(app):
@pytest.fixture @pytest.fixture
def blueprint_app(app): def blueprint_app():
app = Sanic("app")
first_print = Blueprint("first", url_prefix="/first") first_print = Blueprint("first", url_prefix="/first")
second_print = Blueprint("second", url_prefix="/second") second_print = Blueprint("second", url_prefix="/second")
@ -279,6 +282,7 @@ def blueprint_app(app):
def test_blueprints_are_named_correctly(blueprint_app): def test_blueprints_are_named_correctly(blueprint_app):
print(f"{blueprint_app.router.name_index=}")
first_url = blueprint_app.url_for("first.foo") first_url = blueprint_app.url_for("first.foo")
assert first_url == "/first/foo" assert first_url == "/first/foo"

View File

@ -2,10 +2,13 @@ import pytest
from sanic_routing.exceptions import RouteExists from sanic_routing.exceptions import RouteExists
from sanic import Sanic
from sanic.response import text from sanic.response import text
def test_vhosts(app): def test_vhosts():
app = Sanic("app")
@app.route("/", host="example.com") @app.route("/", host="example.com")
async def handler1(request): async def handler1(request):
return text("You're at example.com!") return text("You're at example.com!")

View File

@ -215,17 +215,18 @@ def test_composition_view_runs_methods_as_expected(app, method):
if method in ["GET", "POST", "PUT"]: if method in ["GET", "POST", "PUT"]:
request, response = getattr(app.test_client, method.lower())("/") request, response = getattr(app.test_client, method.lower())("/")
assert response.status == 200
assert response.text == "first method" assert response.text == "first method"
response = view(request) # response = view(request)
assert response.body.decode() == "first method" # assert response.body.decode() == "first method"
if method in ["DELETE", "PATCH"]: # if method in ["DELETE", "PATCH"]:
request, response = getattr(app.test_client, method.lower())("/") # request, response = getattr(app.test_client, method.lower())("/")
assert response.text == "second method" # assert response.text == "second method"
response = view(request) # response = view(request)
assert response.body.decode() == "second method" # assert response.body.decode() == "second method"
@pytest.mark.parametrize("method", HTTP_METHODS) @pytest.mark.parametrize("method", HTTP_METHODS)