def get_route_path(scope: Scope) -> str:
root_path = scope.get("root_path", "")
- route_path = re.sub(r"^" + root_path, "", scope["path"])
+ route_path = re.sub(r"^" + root_path + r"(?=/|$)", "", scope["path"])
return route_path
import functools
from typing import Any
-from starlette._utils import is_async_callable
+import pytest
+
+from starlette._utils import get_route_path, is_async_callable
+from starlette.types import Scope
def test_async_func() -> None:
partial = functools.partial(async_func, b=2)
nested_partial = functools.partial(partial, a=1)
assert is_async_callable(nested_partial)
+
+
+@pytest.mark.parametrize(
+ "scope, expected_result",
+ [
+ ({"path": "/foo-123/bar", "root_path": "/foo"}, "/foo-123/bar"),
+ ({"path": "/foo/bar", "root_path": "/foo"}, "/bar"),
+ ({"path": "/foo", "root_path": "/foo"}, ""),
+ ],
+)
+def test_get_route_path(scope: Scope, expected_result: str) -> None:
+ assert get_route_path(scope) == expected_result
name="path",
methods=["GET"],
),
+ Route(
+ "/root-queue/path",
+ functools.partial(echo_paths, name="queue_path"),
+ name="queue_path",
+ methods=["POST"],
+ ),
Mount("/asgipath", app=functools.partial(pure_asgi_echo_paths, name="asgipath")),
Mount(
"/sub",
"path": "/root/sub/path",
"root_path": "/root/sub",
}
+
+ response = client.post("/root/root-queue/path")
+ assert response.status_code == 200
+ assert response.json() == {
+ "name": "queue_path",
+ "path": "/root/root-queue/path",
+ "root_path": "/root",
+ }