1
0
Fork 0
mirror of https://github.com/tokio-rs/axum.git synced 2025-03-21 22:48:51 +01:00

Panic if routing to router ()

This panics if you pass a `Router` to `Router::route`. Thats invalid and
will result in unreachable routes.

Unfortunately I don't think we can make it a type error while supporting
general `Service`s. So I think this is a decent workaround.

Fixes https://github.com/tokio-rs/axum/issues/174
This commit is contained in:
David Pedersen 2021-10-26 22:44:16 +02:00 committed by GitHub
parent 92f96a201c
commit 94330b7796
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions
src
routing
tests

View file

@ -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) {

View file

@ -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>() {}