Add empty path capture docs and tests (#2127)

This commit is contained in:
Kristopher Wuollett
2023-08-02 20:57:15 +02:00
committed by GitHub
parent 5b89f1dfaa
commit e4865e17fa
2 changed files with 60 additions and 2 deletions
+2 -1
View File
@@ -22,7 +22,8 @@ be called.
# Captures
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:
+58 -1
View File
@@ -629,7 +629,7 @@ mod tests {
}
#[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 client = TestClient::new(app);
@@ -641,6 +641,63 @@ mod tests {
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]
async fn str_reference_deserialize() {
struct Param(String);