diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index 2893188bf1..0bb9b352a0 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -56,7 +56,8 @@ mod tests { }; use serde_json::{json, Value}; use std::net::{SocketAddr, TcpListener}; - use tower::ServiceExt; // for `app.oneshot()` + use tower::Service; // for `call` + use tower::ServiceExt; // for `oneshot` and `ready` #[tokio::test] async fn hello_world() { @@ -148,4 +149,19 @@ mod tests { let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); assert_eq!(&body[..], b"Hello, World!"); } + + // You can use `ready()` and `call()` to avoid using `clone()` + // in multiple request + #[tokio::test] + async fn multiple_request() { + let mut app = app(); + + let request = Request::builder().uri("/").body(Body::empty()).unwrap(); + let response = app.ready().await.unwrap().call(request).await.unwrap(); + assert_eq!(response.status(), StatusCode::OK); + + let request = Request::builder().uri("/").body(Body::empty()).unwrap(); + let response = app.ready().await.unwrap().call(request).await.unwrap(); + assert_eq!(response.status(), StatusCode::OK); + } }