mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-08 00:00:24 +02:00
Add empty path capture docs and tests (#2127)
This commit is contained in:
@@ -22,7 +22,8 @@ be called.
|
|||||||
# Captures
|
# Captures
|
||||||
|
|
||||||
Paths can contain segments like `/:key` which matches any single segment and
|
Paths can contain segments like `/:key` which matches any single segment and
|
||||||
will store the value captured at `key`.
|
will store the value captured at `key`. The value captured can be zero-length
|
||||||
|
except for in the invalid path `//`.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
|
|||||||
@@ -629,7 +629,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[crate::test]
|
#[crate::test]
|
||||||
async fn captures_dont_match_empty_segments() {
|
async fn captures_dont_match_empty_path() {
|
||||||
let app = Router::new().route("/:key", get(|| async {}));
|
let app = Router::new().route("/:key", get(|| async {}));
|
||||||
|
|
||||||
let client = TestClient::new(app);
|
let client = TestClient::new(app);
|
||||||
@@ -641,6 +641,63 @@ mod tests {
|
|||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[crate::test]
|
||||||
|
async fn captures_match_empty_inner_segments() {
|
||||||
|
let app = Router::new().route(
|
||||||
|
"/:key/method",
|
||||||
|
get(|Path(param): Path<String>| async move { param.to_string() }),
|
||||||
|
);
|
||||||
|
|
||||||
|
let client = TestClient::new(app);
|
||||||
|
|
||||||
|
let res = client.get("/abc/method").send().await;
|
||||||
|
assert_eq!(res.text().await, "abc");
|
||||||
|
|
||||||
|
let res = client.get("//method").send().await;
|
||||||
|
assert_eq!(res.text().await, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[crate::test]
|
||||||
|
async fn captures_match_empty_inner_segments_near_end() {
|
||||||
|
let app = Router::new().route(
|
||||||
|
"/method/:key/",
|
||||||
|
get(|Path(param): Path<String>| async move { param.to_string() }),
|
||||||
|
);
|
||||||
|
|
||||||
|
let client = TestClient::new(app);
|
||||||
|
|
||||||
|
let res = client.get("/method/abc").send().await;
|
||||||
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
|
let res = client.get("/method/abc/").send().await;
|
||||||
|
assert_eq!(res.text().await, "abc");
|
||||||
|
|
||||||
|
let res = client.get("/method//").send().await;
|
||||||
|
assert_eq!(res.text().await, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[crate::test]
|
||||||
|
async fn captures_match_empty_trailing_segment() {
|
||||||
|
let app = Router::new().route(
|
||||||
|
"/method/:key",
|
||||||
|
get(|Path(param): Path<String>| async move { param.to_string() }),
|
||||||
|
);
|
||||||
|
|
||||||
|
let client = TestClient::new(app);
|
||||||
|
|
||||||
|
let res = client.get("/method/abc/").send().await;
|
||||||
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
|
let res = client.get("/method/abc").send().await;
|
||||||
|
assert_eq!(res.text().await, "abc");
|
||||||
|
|
||||||
|
let res = client.get("/method/").send().await;
|
||||||
|
assert_eq!(res.text().await, "");
|
||||||
|
|
||||||
|
let res = client.get("/method").send().await;
|
||||||
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
#[crate::test]
|
#[crate::test]
|
||||||
async fn str_reference_deserialize() {
|
async fn str_reference_deserialize() {
|
||||||
struct Param(String);
|
struct Param(String);
|
||||||
|
|||||||
Reference in New Issue
Block a user