Skip to content

Commit 1c8d141

Browse files
committed
🐛 Root path on mount endpoints
1 parent bea3ff7 commit 1c8d141

File tree

7 files changed

+15
-15
lines changed

7 files changed

+15
-15
lines changed

flama/resources/routing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(
1818
self,
1919
path: str,
2020
resource: t.Union["Resource", type["Resource"]],
21-
tags: t.Optional[dict[str, dict[str, t.Any]]] = None,
21+
tags: t.Optional[dict[str, t.Any]] = None,
2222
):
2323
tags = tags or {}
2424

flama/routing/routes/mount.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(
4141

4242
app = Router(routes=routes, components=components)
4343

44-
super().__init__(url.Path(path.rstrip("/")), app, name=name, tags=tags)
44+
super().__init__(path, app, name=name, tags=tags)
4545

4646
async def __call__(self, scope: types.Scope, receive: types.Receive, send: types.Send) -> None:
4747
if scope["type"] in ("http", "websocket") or (
@@ -111,8 +111,8 @@ def route_scope(self, scope: types.Scope) -> types.Scope:
111111
match = self.path.match(path)
112112
result.update(
113113
{
114-
"root_path": "" if is_flama else str(url.Path(scope.get("root_path", "")) / match.matched),
115-
"path": str(url.Path("/") / match.unmatched),
114+
"root_path": "" if is_flama else str(url.Path(scope.get("root_path", "")) / (match.matched or "")),
115+
"path": str(url.Path("/") / (match.unmatched or "")),
116116
}
117117
)
118118

flama/url.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def __str__(self) -> str:
271271
def __repr__(self) -> str:
272272
return self.path.__repr__()
273273

274-
def __truediv__(self, other) -> "Path":
274+
def __truediv__(self, other: t.Union["Path", str]) -> "Path":
275275
if isinstance(other, Path):
276276
a, b = self.path.rstrip("/"), other.path.lstrip("/")
277277
elif isinstance(other, str):
@@ -281,7 +281,7 @@ def __truediv__(self, other) -> "Path":
281281

282282
return Path(f"{a}/{b}")
283283

284-
def __rtruediv__(self, other) -> "Path":
284+
def __rtruediv__(self, other: t.Union["Path", str]) -> "Path":
285285
if isinstance(other, Path):
286286
a, b = other.path.rstrip("/"), self.path.lstrip("/") # pragma: no cover # covered by __truediv__
287287
elif isinstance(other, str):
@@ -291,7 +291,7 @@ def __rtruediv__(self, other) -> "Path":
291291

292292
return Path(f"{a}/{b}")
293293

294-
def __itruediv__(self, other) -> "Path":
294+
def __itruediv__(self, other: t.Union["Path", str]) -> "Path":
295295
path = self / other
296296
self.path = path.path
297297
self._fragments = path._fragments

tests/debug/test_data_structures.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def bar_handler(): ...
9494
"apps": [
9595
{
9696
"name": "subapp",
97-
"path": "/subapp",
97+
"path": "/subapp/",
9898
"apps": [],
9999
"endpoints": [
100100
{

tests/resources/test_routing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_init(self, resource):
4141
},
4242
)
4343

44-
assert resource_route.path == "/puppy"
44+
assert resource_route.path == "/puppy/"
4545
assert resource_route.resource == resource
4646
for route in resource_route.routes:
4747
assert isinstance(route, Route)

tests/routing/routes/test_mount.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_init(self, app, routes, exception):
5151
app = Router(routes=routes)
5252

5353
assert mount.app == app
54-
assert mount.path == "/foo"
54+
assert mount.path == "/foo/"
5555

5656
@pytest.mark.parametrize(
5757
["scope_type", "handle_call"],
@@ -149,19 +149,19 @@ def bar(): ...
149149
assert route_scope == {
150150
"app": app if used else asgi_scope["app"],
151151
"path": "/bar",
152-
"root_path": "" if used else "/foo/1",
152+
"root_path": "" if used else "/foo/1/",
153153
}
154154

155155
@pytest.mark.parametrize(
156156
["name", "params", "expected_url", "exception"],
157157
(
158158
pytest.param(
159-
"foo", {"x": 1, "path": "/foo"}, url.URL(scheme="http", path="/foo/1"), None, id="match_full_name"
159+
"foo", {"x": 1, "path": "/foo"}, url.URL(scheme="http", path="/foo/1/"), None, id="match_full_name"
160160
),
161161
pytest.param("foo:bar", {"x": 1}, url.URL(scheme="http", path="/foo/1/bar"), None, id="match_route"),
162162
pytest.param(
163163
"foo:nested",
164-
{"x": 1, "path": "/foo"},
164+
{"x": 1, "path": "/foo/"},
165165
url.URL(scheme="http", path="/foo/1/nested"),
166166
None,
167167
id="match_nested_app",

tests/routing/test_router.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def test_mount_app(self, app, app_mock, tags):
355355

356356
assert len(app.routes) == 1
357357
assert isinstance(app.routes[0], Mount)
358-
assert app.routes[0].path == "/app"
358+
assert app.routes[0].path == "/app/"
359359
assert app.routes[0].app == app_mock
360360
assert app.routes[0].tags == tags
361361

@@ -368,7 +368,7 @@ def test_mount_router(self, app, component_mock, tags):
368368
# Check mount is initialized
369369
assert isinstance(app.routes[0], Mount)
370370
mount_route = app.router.routes[0]
371-
assert mount_route.path == "/app"
371+
assert mount_route.path == "/app/"
372372
assert mount_route.tags == tags
373373
# Check router is created and initialized, also shares components and modules with main app
374374
assert isinstance(mount_route.app, Router)

0 commit comments

Comments
 (0)