* perf: stop use re on get_route_path
* add test
import asyncio
import functools
-import re
import sys
import typing
from contextlib import contextmanager
def get_route_path(scope: Scope) -> str:
+ path: str = scope["path"]
root_path = scope.get("root_path", "")
- route_path = re.sub(r"^" + root_path + r"(?=/|$)", "", scope["path"])
- return route_path
+ if not root_path:
+ return path
+
+ if not path.startswith(root_path):
+ return path
+
+ if path == root_path:
+ return ""
+
+ if path[len(root_path)] == "/":
+ return path[len(root_path) :]
+
+ return path
({"path": "/foo-123/bar", "root_path": "/foo"}, "/foo-123/bar"),
({"path": "/foo/bar", "root_path": "/foo"}, "/bar"),
({"path": "/foo", "root_path": "/foo"}, ""),
+ ({"path": "/foo/bar", "root_path": "/bar"}, "/foo/bar"),
],
)
def test_get_route_path(scope: Scope, expected_result: str) -> None: