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

Don't allow empty paths ()

This commit is contained in:
David Pedersen 2021-10-26 17:37:25 +02:00 committed by GitHub
parent 9b17d86b92
commit e9533c566f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions
src
routing
tests

View file

@ -178,6 +178,8 @@ where
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # };
/// ```
///
/// Also panics if `path` is empty.
pub fn route<T>(mut self, path: &str, svc: T) -> Self
where
T: Service<Request<B>, Response = Response<BoxBody>, Error = Infallible>
@ -186,6 +188,10 @@ where
+ 'static,
T::Future: Send + 'static,
{
if path.is_empty() {
panic!("Invalid route: empty path");
}
let id = RouteId::next();
if let Err(err) = self.node.insert(path, id) {
@ -310,8 +316,10 @@ where
///
/// # Panics
///
/// Panics if the route overlaps with another route. See [`Router::route`]
/// - If the route overlaps with another route. See [`Router::route`]
/// for more details.
/// - If the route contains a wildcard (`*`).
/// - If `path` is empty.
///
/// [`OriginalUri`]: crate::extract::OriginalUri
pub fn nest<T>(mut self, path: &str, svc: T) -> Self
@ -322,12 +330,16 @@ where
+ 'static,
T::Future: Send + 'static,
{
let id = RouteId::next();
if path.is_empty() {
panic!("Invalid route: empty path");
}
if path.contains('*') {
panic!("Invalid route: nested routes cannot contain wildcards (*)");
}
let id = RouteId::next();
let path = if path == "/" {
format!("/*{}", NEST_TAIL_PARAM)
} else {

View file

@ -658,6 +658,20 @@ async fn static_and_dynamic_paths() {
assert_eq!(res.text().await, "static");
}
#[tokio::test]
#[should_panic(expected = "Invalid route: empty path")]
async fn empty_route() {
let app = Router::new().route("", get(|| async {}));
TestClient::new(app);
}
#[tokio::test]
#[should_panic(expected = "Invalid route: empty path")]
async fn empty_route_nested() {
let app = Router::new().nest("", get(|| async {}));
TestClient::new(app);
}
pub(crate) fn assert_send<T: Send>() {}
pub(crate) fn assert_sync<T: Sync>() {}
pub(crate) fn assert_unpin<T: Unpin>() {}