mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-26 00:00:23 +02:00
Change nested routes to see the URI with prefix stripped (#197)
This commit is contained in:
+31
-7
@@ -151,7 +151,7 @@ async fn nested_url_extractor() {
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.text().await.unwrap(), "/foo/bar/baz");
|
||||
assert_eq!(res.text().await.unwrap(), "/baz");
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo/bar/qux", addr))
|
||||
@@ -159,18 +159,18 @@ async fn nested_url_extractor() {
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.text().await.unwrap(), "/foo/bar/qux");
|
||||
assert_eq!(res.text().await.unwrap(), "/qux");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nested_url_nested_extractor() {
|
||||
async fn nested_url_original_extractor() {
|
||||
let app = nest(
|
||||
"/foo",
|
||||
nest(
|
||||
"/bar",
|
||||
route(
|
||||
"/baz",
|
||||
get(|uri: extract::NestedUri| async move { uri.0.to_string() }),
|
||||
get(|uri: extract::OriginalUri| async move { uri.0.to_string() }),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -185,11 +185,11 @@ async fn nested_url_nested_extractor() {
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.text().await.unwrap(), "/baz");
|
||||
assert_eq!(res.text().await.unwrap(), "/foo/bar/baz");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nested_service_sees_original_uri() {
|
||||
async fn nested_service_sees_stripped_uri() {
|
||||
let app = nest(
|
||||
"/foo",
|
||||
nest(
|
||||
@@ -214,5 +214,29 @@ async fn nested_service_sees_original_uri() {
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.text().await.unwrap(), "/foo/bar/baz");
|
||||
assert_eq!(res.text().await.unwrap(), "/baz");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nest_static_file_server() {
|
||||
let app = nest(
|
||||
"/static",
|
||||
service::get(tower_http::services::ServeDir::new(".")).handle_error(|error| {
|
||||
Ok::<_, Infallible>((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
))
|
||||
}),
|
||||
);
|
||||
|
||||
let addr = run_in_background(app).await;
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/static/README.md", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
+187
-11
@@ -1,5 +1,8 @@
|
||||
use serde_json::{json, Value};
|
||||
use tower::{limit::ConcurrencyLimitLayer, timeout::TimeoutLayer};
|
||||
|
||||
use crate::{extract::OriginalUri, response::IntoResponse, Json};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
@@ -302,17 +305,190 @@ async fn services() {
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
// TODO(david): can we make this not compile?
|
||||
// #[tokio::test]
|
||||
// async fn foo() {
|
||||
// let svc_one = service_fn(|_: Request<Body>| async {
|
||||
// Ok::<_, hyper::Error>(Response::new(Body::empty()))
|
||||
// })
|
||||
// .handle_error::<_, _, hyper::Error>(|_| Ok(StatusCode::INTERNAL_SERVER_ERROR));
|
||||
async fn all_the_uris(
|
||||
uri: Uri,
|
||||
OriginalUri(original_uri): OriginalUri,
|
||||
req: Request<Body>,
|
||||
) -> impl IntoResponse {
|
||||
Json(json!({
|
||||
"uri": uri.to_string(),
|
||||
"request_uri": req.uri().to_string(),
|
||||
"original_uri": original_uri.to_string(),
|
||||
}))
|
||||
}
|
||||
|
||||
// let svc_two = svc_one.clone();
|
||||
#[tokio::test]
|
||||
async fn nesting_and_seeing_the_right_uri() {
|
||||
let one = nest("/foo", route("/bar", get(all_the_uris)));
|
||||
let two = route("/foo", get(all_the_uris));
|
||||
|
||||
// let app = svc_one.or(svc_two);
|
||||
let addr = run_in_background(one.or(two)).await;
|
||||
|
||||
// let addr = run_in_background(app).await;
|
||||
// }
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo/bar", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
res.json::<Value>().await.unwrap(),
|
||||
json!({
|
||||
"uri": "/bar",
|
||||
"request_uri": "/bar",
|
||||
"original_uri": "/foo/bar",
|
||||
})
|
||||
);
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
res.json::<Value>().await.unwrap(),
|
||||
json!({
|
||||
"uri": "/foo",
|
||||
"request_uri": "/foo",
|
||||
"original_uri": "/foo",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nesting_and_seeing_the_right_uri_at_more_levels_of_nesting() {
|
||||
let one = nest("/foo", nest("/bar", route("/baz", get(all_the_uris))));
|
||||
let two = route("/foo", get(all_the_uris));
|
||||
|
||||
let addr = run_in_background(one.or(two)).await;
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo/bar/baz", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
res.json::<Value>().await.unwrap(),
|
||||
json!({
|
||||
"uri": "/baz",
|
||||
"request_uri": "/baz",
|
||||
"original_uri": "/foo/bar/baz",
|
||||
})
|
||||
);
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
res.json::<Value>().await.unwrap(),
|
||||
json!({
|
||||
"uri": "/foo",
|
||||
"request_uri": "/foo",
|
||||
"original_uri": "/foo",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nesting_and_seeing_the_right_uri_ors_with_nesting() {
|
||||
let one = nest("/foo", nest("/bar", route("/baz", get(all_the_uris))));
|
||||
let two = nest("/foo", route("/qux", get(all_the_uris)));
|
||||
let three = route("/foo", get(all_the_uris));
|
||||
|
||||
let addr = run_in_background(one.or(two).or(three)).await;
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo/bar/baz", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
res.json::<Value>().await.unwrap(),
|
||||
json!({
|
||||
"uri": "/baz",
|
||||
"request_uri": "/baz",
|
||||
"original_uri": "/foo/bar/baz",
|
||||
})
|
||||
);
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo/qux", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
res.json::<Value>().await.unwrap(),
|
||||
json!({
|
||||
"uri": "/qux",
|
||||
"request_uri": "/qux",
|
||||
"original_uri": "/foo/qux",
|
||||
})
|
||||
);
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
res.json::<Value>().await.unwrap(),
|
||||
json!({
|
||||
"uri": "/foo",
|
||||
"request_uri": "/foo",
|
||||
"original_uri": "/foo",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nesting_and_seeing_the_right_uri_ors_with_multi_segment_uris() {
|
||||
let one = nest("/foo", nest("/bar", route("/baz", get(all_the_uris))));
|
||||
let two = route("/foo/bar", get(all_the_uris));
|
||||
|
||||
let addr = run_in_background(one.or(two)).await;
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo/bar/baz", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
res.json::<Value>().await.unwrap(),
|
||||
json!({
|
||||
"uri": "/baz",
|
||||
"request_uri": "/baz",
|
||||
"original_uri": "/foo/bar/baz",
|
||||
})
|
||||
);
|
||||
|
||||
let res = client
|
||||
.get(format!("http://{}/foo/bar", addr))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
res.json::<Value>().await.unwrap(),
|
||||
json!({
|
||||
"uri": "/foo/bar",
|
||||
"request_uri": "/foo/bar",
|
||||
"original_uri": "/foo/bar",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user