Make sure nested services still see full URI (#166)

They'd previously see the nested URI as we mutated the request. Now we
always route based on the nested URI (if present) without mutating the
request. Also meant we could get rid of `OriginalUri` which is nice.
This commit is contained in:
David Pedersen
2021-08-08 17:27:23 +02:00
committed by GitHub
parent 0674c9136a
commit bc27b09f5c
3 changed files with 59 additions and 28 deletions
+33
View File
@@ -1,4 +1,5 @@
use super::*;
use crate::body::box_body;
use std::collections::HashMap;
#[tokio::test]
@@ -149,6 +150,7 @@ async fn nested_url_extractor() {
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await.unwrap(), "/foo/bar/baz");
let res = client
@@ -156,6 +158,7 @@ async fn nested_url_extractor() {
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await.unwrap(), "/foo/bar/qux");
}
@@ -181,5 +184,35 @@ async fn nested_url_nested_extractor() {
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await.unwrap(), "/baz");
}
#[tokio::test]
async fn nested_service_sees_original_uri() {
let app = nest(
"/foo",
nest(
"/bar",
route(
"/baz",
service_fn(|req: Request<Body>| async move {
let body = box_body(Body::from(req.uri().to_string()));
Ok::<_, Infallible>(Response::new(body))
}),
),
),
);
let addr = run_in_background(app).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.text().await.unwrap(), "/foo/bar/baz");
}