Add MatchedPath extractor (#412)

Fixes #386
This commit is contained in:
David Pedersen
2021-10-25 21:38:29 +00:00
committed by GitHub
parent e43bdf0ecf
commit 02a035fb14
7 changed files with 139 additions and 3 deletions
+22
View File
@@ -1,6 +1,7 @@
#![allow(clippy::blacklisted_name)]
use crate::error_handling::HandleErrorLayer;
use crate::extract::MatchedPath;
use crate::BoxError;
use crate::{
extract::{self, Path},
@@ -27,6 +28,7 @@ use std::{
};
use tower::{service_fn, timeout::TimeoutLayer, ServiceBuilder};
use tower_http::auth::RequireAuthorizationLayer;
use tower_http::trace::TraceLayer;
use tower_service::Service;
pub(crate) use helpers::*;
@@ -618,6 +620,26 @@ async fn with_and_without_trailing_slash() {
assert_eq!(res.text().await, "without tsr");
}
#[tokio::test]
async fn access_matched_path() {
let app = Router::new()
.route(
"/:key",
get(|path: MatchedPath| async move { path.as_str().to_string() }),
)
.layer(
TraceLayer::new_for_http().make_span_with(|req: &Request<_>| {
let path = req.extensions().get::<MatchedPath>().unwrap().as_str();
tracing::info_span!("http-request", %path)
}),
);
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
assert_eq!(res.text().await, "/:key");
}
pub(crate) fn assert_send<T: Send>() {}
pub(crate) fn assert_sync<T: Sync>() {}
pub(crate) fn assert_unpin<T: Unpin>() {}