Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions baize/asgi/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ class Router(BaseRouter[ASGIApp]):
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] == "lifespan": # pragma: no cover
raise RuntimeError("Unsupported lifespan in `Router`")
result = self.search(scope["path"])
root_path = scope.get("root_path", "")
path = scope["path"]
if root_path and path.startswith(root_path):
path = path[len(root_path) :]
result = self.search(path)
if result is None:
response: ASGIApp = Response(404)
else:
Expand Down Expand Up @@ -48,14 +52,16 @@ class Subpaths(BaseSubpaths[ASGIApp]):
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] == "lifespan": # pragma: no cover
raise RuntimeError("Unsupported lifespan in `Subpaths`")
root_path = scope.get("root_path", "")
path = scope["path"]
if root_path and path.startswith(root_path):
path = path[len(root_path) :]
result = self.search(path)
if result is None:
response: ASGIApp = Response(404)
else:
prefix, response = result
scope["root_path"] = scope.get("root_path", "") + prefix
scope["path"] = path[len(prefix) :]
scope["root_path"] = root_path + prefix
return await response(scope, receive, send)


Expand Down
2 changes: 1 addition & 1 deletion baize/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def __init__(
elif scope is not None:
scheme = scope.get("scheme", "http")
server = scope.get("server", None)
path = scope.get("root_path", "") + scope["path"]
path = scope["path"]
query_string = scope.get("query_string", b"")

host_header = None
Expand Down
20 changes: 19 additions & 1 deletion tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,30 @@ async def path(request: Request) -> Response:
app=Subpaths(
("/frist", root),
("/latest", path),
(
"/s",
Subpaths(
("/frist", root),
("/latest", path),
),
),
(
"/r",
Router(
("/frist", root),
("/latest", path),
),
),
),
base_url="http://testServer/",
) as client:
assert (await client.get("/")).status_code == 404
assert (await client.get("/frist")).text == "/frist"
assert (await client.get("/latest")).text == ""
assert (await client.get("/latest/")).text == "/latest/"
assert (await client.get("/s/frist/")).text == "/s/frist"
assert (await client.get("/s/latest/")).text == "/s/latest/"
assert (await client.get("/r/frist")).text == "/r"
assert (await client.get("/r/latest")).text == "/r/latest"

async with httpx.AsyncClient(
app=Subpaths(
Expand Down