Update unix-domain-socket, listen-multiple-addrs, and testing examples (#2358)

This commit is contained in:
David Pedersen
2023-11-27 10:23:52 +01:00
committed by GitHub
parent db344fab3f
commit e63cc49f55
6 changed files with 335 additions and 403 deletions
+171 -181
View File
@@ -4,208 +4,198 @@
//! cargo test -p example-testing
//! ```
fn main() {
// This example has not yet been updated to Hyper 1.0
use std::net::SocketAddr;
use axum::{
extract::ConnectInfo,
routing::{get, post},
Json, Router,
};
use tower_http::trace::TraceLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_testing=debug,tower_http=debug".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
tracing::debug!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app()).await.unwrap();
}
//use std::net::SocketAddr;
/// Having a function that produces our app makes it easy to call it from tests
/// without having to create an HTTP server.
fn app() -> Router {
Router::new()
.route("/", get(|| async { "Hello, World!" }))
.route(
"/json",
post(|payload: Json<serde_json::Value>| async move {
Json(serde_json::json!({ "data": payload.0 }))
}),
)
.route(
"/requires-connect-into",
get(|ConnectInfo(addr): ConnectInfo<SocketAddr>| async move { format!("Hi {addr}") }),
)
// We can still add middleware
.layer(TraceLayer::new_for_http())
}
//use axum::{
// extract::ConnectInfo,
// routing::{get, post},
// Json, Router,
//};
//use tower_http::trace::TraceLayer;
//use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[cfg(test)]
mod tests {
use super::*;
use axum::{
body::Body,
extract::connect_info::MockConnectInfo,
http::{self, Request, StatusCode},
};
use http_body_util::BodyExt;
use serde_json::{json, Value};
use std::net::SocketAddr;
use tokio::net::TcpListener;
use tower::Service; // for `call`
use tower::ServiceExt; // for `oneshot` and `ready` // for `collect`
//#[tokio::main]
//async fn main() {
// tracing_subscriber::registry()
// .with(
// tracing_subscriber::EnvFilter::try_from_default_env()
// .unwrap_or_else(|_| "example_testing=debug,tower_http=debug".into()),
// )
// .with(tracing_subscriber::fmt::layer())
// .init();
#[tokio::test]
async fn hello_world() {
let app = app();
// let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
// .await
// .unwrap();
// tracing::debug!("listening on {}", listener.local_addr().unwrap());
// axum::serve(listener, app()).await.unwrap();
//}
// `Router` implements `tower::Service<Request<Body>>` so we can
// call it like any tower service, no need to run an HTTP server.
let response = app
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
.await
.unwrap();
///// Having a function that produces our app makes it easy to call it from tests
///// without having to create an HTTP server.
//fn app() -> Router {
// Router::new()
// .route("/", get(|| async { "Hello, World!" }))
// .route(
// "/json",
// post(|payload: Json<serde_json::Value>| async move {
// Json(serde_json::json!({ "data": payload.0 }))
// }),
// )
// .route(
// "/requires-connect-into",
// get(|ConnectInfo(addr): ConnectInfo<SocketAddr>| async move { format!("Hi {addr}") }),
// )
// // We can still add middleware
// .layer(TraceLayer::new_for_http())
//}
assert_eq!(response.status(), StatusCode::OK);
//#[cfg(test)]
//mod tests {
// use super::*;
// use axum::{
// body::Body,
// extract::connect_info::MockConnectInfo,
// http::{self, Request, StatusCode},
// };
// use http_body_util::BodyExt;
// use serde_json::{json, Value};
// use std::net::SocketAddr;
// use tokio::net::{TcpListener, TcpStream};
// use tower::Service; // for `call`
// use tower::ServiceExt; // for `oneshot` and `ready` // for `collect`
let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], b"Hello, World!");
}
// #[tokio::test]
// async fn hello_world() {
// let app = app();
#[tokio::test]
async fn json() {
let app = app();
// // `Router` implements `tower::Service<Request<Body>>` so we can
// // call it like any tower service, no need to run an HTTP server.
// let response = app
// .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
// .await
// .unwrap();
let response = app
.oneshot(
Request::builder()
.method(http::Method::POST)
.uri("/json")
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(
serde_json::to_vec(&json!([1, 2, 3, 4])).unwrap(),
))
.unwrap(),
)
.await
.unwrap();
// assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
// let body = response.into_body().collect().await.unwrap().to_bytes();
// assert_eq!(&body[..], b"Hello, World!");
// }
let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(body, json!({ "data": [1, 2, 3, 4] }));
}
// #[tokio::test]
// async fn json() {
// let app = app();
#[tokio::test]
async fn not_found() {
let app = app();
// let response = app
// .oneshot(
// Request::builder()
// .method(http::Method::POST)
// .uri("/json")
// .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
// .body(Body::from(
// serde_json::to_vec(&json!([1, 2, 3, 4])).unwrap(),
// ))
// .unwrap(),
// )
// .await
// .unwrap();
let response = app
.oneshot(
Request::builder()
.uri("/does-not-exist")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
// assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let body = response.into_body().collect().await.unwrap().to_bytes();
assert!(body.is_empty());
}
// let body = response.into_body().collect().await.unwrap().to_bytes();
// let body: Value = serde_json::from_slice(&body).unwrap();
// assert_eq!(body, json!({ "data": [1, 2, 3, 4] }));
// }
// You can also spawn a server and talk to it like any other HTTP server:
#[tokio::test]
async fn the_real_deal() {
let listener = TcpListener::bind("0.0.0.0:0").await.unwrap();
let addr = listener.local_addr().unwrap();
// #[tokio::test]
// async fn not_found() {
// let app = app();
tokio::spawn(async move {
axum::serve(listener, app()).await.unwrap();
});
// let response = app
// .oneshot(
// Request::builder()
// .uri("/does-not-exist")
// .body(Body::empty())
// .unwrap(),
// )
// .await
// .unwrap();
let client =
hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new())
.build_http();
// assert_eq!(response.status(), StatusCode::NOT_FOUND);
// let body = response.into_body().collect().await.unwrap().to_bytes();
// assert!(body.is_empty());
// }
let response = client
.request(
Request::builder()
.uri(format!("http://{addr}"))
.header("Host", "localhost")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
// // You can also spawn a server and talk to it like any other HTTP server:
// #[tokio::test]
// async fn the_real_deal() {
// // TODO(david): convert this to hyper-util when thats published
let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], b"Hello, World!");
}
// use hyper::client::conn;
// You can use `ready()` and `call()` to avoid using `clone()`
// in multiple request
#[tokio::test]
async fn multiple_request() {
let mut app = app().into_service();
// let listener = TcpListener::bind("0.0.0.0:0").await.unwrap();
// let addr = listener.local_addr().unwrap();
let request = Request::builder().uri("/").body(Body::empty()).unwrap();
let response = ServiceExt::<Request<Body>>::ready(&mut app)
.await
.unwrap()
.call(request)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
// tokio::spawn(async move {
// axum::serve(listener, app()).await.unwrap();
// });
let request = Request::builder().uri("/").body(Body::empty()).unwrap();
let response = ServiceExt::<Request<Body>>::ready(&mut app)
.await
.unwrap()
.call(request)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
// let target_stream = TcpStream::connect(addr).await.unwrap();
// Here we're calling `/requires-connect-into` which requires `ConnectInfo`
//
// That is normally set with `Router::into_make_service_with_connect_info` but we can't easily
// use that during tests. The solution is instead to set the `MockConnectInfo` layer during
// tests.
#[tokio::test]
async fn with_into_make_service_with_connect_info() {
let mut app = app()
.layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000))))
.into_service();
// let (mut request_sender, connection) = conn::http1::handshake(target_stream).await.unwrap();
// tokio::spawn(async move { connection.await.unwrap() });
// let response = request_sender
// .send_request(
// Request::builder()
// .uri(format!("http://{addr}"))
// .header("Host", "localhost")
// .body(Body::empty())
// .unwrap(),
// )
// .await
// .unwrap();
// let body = response.into_body().collect().await.unwrap().to_bytes();
// 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().into_service();
// let request = Request::builder().uri("/").body(Body::empty()).unwrap();
// let response = ServiceExt::<Request<Body>>::ready(&mut app)
// .await
// .unwrap()
// .call(request)
// .await
// .unwrap();
// assert_eq!(response.status(), StatusCode::OK);
// let request = Request::builder().uri("/").body(Body::empty()).unwrap();
// let response = ServiceExt::<Request<Body>>::ready(&mut app)
// .await
// .unwrap()
// .call(request)
// .await
// .unwrap();
// assert_eq!(response.status(), StatusCode::OK);
// }
// // Here we're calling `/requires-connect-into` which requires `ConnectInfo`
// //
// // That is normally set with `Router::into_make_service_with_connect_info` but we can't easily
// // use that during tests. The solution is instead to set the `MockConnectInfo` layer during
// // tests.
// #[tokio::test]
// async fn with_into_make_service_with_connect_info() {
// let mut app = app()
// .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000))))
// .into_service();
// let request = Request::builder()
// .uri("/requires-connect-into")
// .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("/requires-connect-into")
.body(Body::empty())
.unwrap();
let response = app.ready().await.unwrap().call(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
}