diff --git a/src/routing/mod.rs b/src/routing/mod.rs index 3167a3a9..22ee12d5 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -197,6 +197,13 @@ where panic!("Invalid route: empty path"); } + let svc = match try_downcast::<Router<B>, _>(svc) { + Ok(_) => { + panic!("Invalid route: `Router::route` cannot be used with `Router`s. Use `Router::nest` instead") + } + Err(svc) => svc, + }; + let id = RouteId::next(); if let Err(err) = self.node.insert(path, id) { diff --git a/src/tests/mod.rs b/src/tests/mod.rs index fc6c314b..f15b2e93 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -751,6 +751,14 @@ async fn middleware_still_run_for_unmatched_requests() { assert_eq!(COUNT.load(Ordering::SeqCst), 2); } +#[tokio::test] +#[should_panic( + expected = "Invalid route: `Router::route` cannot be used with `Router`s. Use `Router::nest` instead" +)] +async fn routing_to_router_panics() { + TestClient::new(Router::new().route("/", Router::new())); +} + pub(crate) fn assert_send<T: Send>() {} pub(crate) fn assert_sync<T: Sync>() {} pub(crate) fn assert_unpin<T: Unpin>() {}