checkpoint

This commit is contained in:
David Pedersen
2021-06-02 22:07:37 +02:00
parent 0d7e1e74c4
commit 00737c4e0a
3 changed files with 353 additions and 40 deletions
+89
View File
@@ -397,6 +397,95 @@ async fn layer_on_whole_router() {
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
#[tokio::test]
async fn nesting() {
let api = app()
.at("/users")
.get(|_: Request<Body>| async { "users#index" })
.post(|_: Request<Body>| async { "users#create" })
.at("/users/:id")
.get(
|_: Request<Body>, params: extract::UrlParams<(i32,)>| async move {
let (id,) = params.0;
format!("users#show {}", id)
},
);
let app = app()
.at("/foo")
.get(|_: Request<Body>| async { "foo" })
.at("/api")
.nest(api)
.at("/bar")
.get(|_: Request<Body>| async { "bar" })
.into_service();
let addr = run_in_background(app).await;
let client = reqwest::Client::new();
let res = client
.get(format!("http://{}/api/users", addr))
.send()
.await
.unwrap();
assert_eq!(res.text().await.unwrap(), "users#index");
let res = client
.post(format!("http://{}/api/users", addr))
.send()
.await
.unwrap();
assert_eq!(res.text().await.unwrap(), "users#create");
let res = client
.get(format!("http://{}/api/users/42", addr))
.send()
.await
.unwrap();
assert_eq!(res.text().await.unwrap(), "users#show 42");
let res = client
.get(format!("http://{}/foo", addr))
.send()
.await
.unwrap();
assert_eq!(res.text().await.unwrap(), "foo");
let res = client
.get(format!("http://{}/bar", addr))
.send()
.await
.unwrap();
assert_eq!(res.text().await.unwrap(), "bar");
}
#[tokio::test]
async fn nesting_with_dynamic_part() {
let api = app().at("/users/:id").get(
|_: Request<Body>, params: extract::UrlParams<(String, i32)>| async move {
let (version, id) = params.0;
format!("users#show {} {}", version, id)
},
);
let app = app().at("/:version/api").nest(api).into_service();
let addr = run_in_background(app).await;
let client = reqwest::Client::new();
let res = client
.get(format!("http://{}/v0/api/users/123", addr))
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await.unwrap(), "users#show v0 123");
}
// TODO(david): nesting more deeply
// TODO(david): composing two apps
// TODO(david): composing two apps with one at a "sub path"
// TODO(david): composing two boxed apps