More clearly document what wildcards matches (#1873)

This commit is contained in:
David Pedersen 2023-03-22 11:48:23 +01:00 committed by GitHub
parent 2600c22703
commit a0af61ea46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View file

@ -50,6 +50,11 @@ Examples:
- `/assets/*path`
- `/:id/:repo/*tree`
Note that `/*key` doesn't match empty segments. Thus:
- `/*key` doesn't match `/` but does match `/a`, `/a/`, etc.
- `/x/*key` doesn't match `/x` or `/x/` but does match `/x/a`, `/x/a/`, etc.
Wildcard captures can also be extracted using [`Path`](crate::extract::Path).
Note that the leading slash is not included, i.e. for the route `/foo/*rest` and
the path `/foo/bar/baz` the value of `rest` will be `bar/baz`.

View file

@ -380,6 +380,34 @@ async fn wildcard_doesnt_match_just_trailing_slash() {
assert_eq!(res.text().await, "foo/bar");
}
#[crate::test]
async fn what_matches_wildcard() {
let app = Router::new()
.route("/*key", get(|| async { "root" }))
.route("/x/*key", get(|| async { "x" }))
.fallback(|| async { "fallback" });
let client = TestClient::new(app);
let get = |path| {
let f = client.get(path).send();
async move { f.await.text().await }
};
assert_eq!(get("/").await, "fallback");
assert_eq!(get("/a").await, "root");
assert_eq!(get("/a/").await, "root");
assert_eq!(get("/a/b").await, "root");
assert_eq!(get("/a/b/").await, "root");
assert_eq!(get("/x").await, "root");
assert_eq!(get("/x/").await, "root");
assert_eq!(get("/x/a").await, "x");
assert_eq!(get("/x/a/").await, "x");
assert_eq!(get("/x/a/b").await, "x");
assert_eq!(get("/x/a/b/").await, "x");
}
#[crate::test]
async fn static_and_dynamic_paths() {
let app = Router::new()