diff --git a/starlette/routing.py b/starlette/routing.py index 2c6965be0..479d4ae65 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -283,6 +283,12 @@ def __eq__(self, other: typing.Any) -> bool: and self.methods == other.methods ) + def __repr__(self) -> str: + class_name = self.__class__.__name__ + methods = sorted(self.methods or []) + path, name = self.path, self.name + return f"{class_name}(path={path!r}, name={name!r}, methods={methods!r})" + class WebSocketRoute(BaseRoute): def __init__( @@ -341,6 +347,9 @@ def __eq__(self, other: typing.Any) -> bool: and self.endpoint == other.endpoint ) + def __repr__(self) -> str: + return f"{self.__class__.__name__}(path={self.path!r}, name={self.name!r})" + class Mount(BaseRoute): def __init__( @@ -440,6 +449,11 @@ def __eq__(self, other: typing.Any) -> bool: and self.app == other.app ) + def __repr__(self) -> str: + class_name = self.__class__.__name__ + name = self.name or "" + return f"{class_name}(path={self.path!r}, name={name!r}, app={self.app!r})" + class Host(BaseRoute): def __init__( @@ -507,6 +521,11 @@ def __eq__(self, other: typing.Any) -> bool: and self.app == other.app ) + def __repr__(self) -> str: + class_name = self.__class__.__name__ + name = self.name or "" + return f"{class_name}(host={self.host!r}, name={name!r}, app={self.app!r})" + _T = typing.TypeVar("_T") diff --git a/tests/test_routing.py b/tests/test_routing.py index 750f32496..e2c2eb2c4 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -952,3 +952,71 @@ async def modified_send(msg: Message) -> None: resp = client.get("/mount/err") assert resp.status_code == 403, resp.content assert "X-Mounted" not in resp.headers + + +def test_route_repr() -> None: + route = Route("/welcome", endpoint=homepage) + assert ( + repr(route) + == "Route(path='/welcome', name='homepage', methods=['GET', 'HEAD'])" + ) + + +def test_route_repr_without_methods() -> None: + route = Route("/welcome", endpoint=Endpoint, methods=None) + assert repr(route) == "Route(path='/welcome', name='Endpoint', methods=[])" + + +def test_websocket_route_repr() -> None: + route = WebSocketRoute("/ws", endpoint=websocket_endpoint) + assert repr(route) == "WebSocketRoute(path='/ws', name='websocket_endpoint')" + + +def test_mount_repr() -> None: + route = Mount( + "/app", + routes=[ + Route("/", endpoint=homepage), + ], + ) + # test for substring because repr(Router) returns unique object ID + assert repr(route).startswith("Mount(path='/app', name='', app=") + + +def test_mount_named_repr() -> None: + route = Mount( + "/app", + name="app", + routes=[ + Route("/", endpoint=homepage), + ], + ) + # test for substring because repr(Router) returns unique object ID + assert repr(route).startswith("Mount(path='/app', name='app', app=") + + +def test_host_repr() -> None: + route = Host( + "example.com", + app=Router( + [ + Route("/", endpoint=homepage), + ] + ), + ) + # test for substring because repr(Router) returns unique object ID + assert repr(route).startswith("Host(host='example.com', name='', app=") + + +def test_host_named_repr() -> None: + route = Host( + "example.com", + name="app", + app=Router( + [ + Route("/", endpoint=homepage), + ] + ), + ) + # test for substring because repr(Router) returns unique object ID + assert repr(route).startswith("Host(host='example.com', name='app', app=")