mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-19 00:00:14 +02:00
Fix URI captures matching empty segments (#264)
It was never the intention that `/:key` should match `/`. This fixes that. Part of https://github.com/tokio-rs/axum/issues/259
This commit is contained in:
+1
-1
@@ -762,7 +762,7 @@ impl PathPattern {
|
||||
if let Some(key) = part.strip_prefix(':') {
|
||||
capture_group_names.push(Bytes::copy_from_slice(key.as_bytes()));
|
||||
|
||||
Cow::Owned(format!("(?P<{}>[^/]*)", key))
|
||||
Cow::Owned(format!("(?P<{}>[^/]+)", key))
|
||||
} else {
|
||||
Cow::Borrowed(part)
|
||||
}
|
||||
|
||||
@@ -672,6 +672,25 @@ async fn when_multiple_routes_match() {
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn captures_dont_match_empty_segments() {
|
||||
let app = Router::new().route("/:key", get(|| async {}));
|
||||
|
||||
let addr = run_in_background(app).await;
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let res = client.get(format!("http://{}", addr)).send().await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
/// Run a `tower::Service` in the background and get a URI for it.
|
||||
pub(crate) async fn run_in_background<S, ResBody>(svc: S) -> SocketAddr
|
||||
where
|
||||
|
||||
Reference in New Issue
Block a user