diff --git a/Cargo.lock b/Cargo.lock index d73ba180..88a44ebe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1814,7 +1814,9 @@ version = "0.1.0" dependencies = [ "askama", "axum", + "http-body-util", "tokio", + "tower 0.5.2", "tracing", "tracing-subscriber", ] diff --git a/examples/templates/Cargo.toml b/examples/templates/Cargo.toml index 6cba0946..f2716d80 100644 --- a/examples/templates/Cargo.toml +++ b/examples/templates/Cargo.toml @@ -10,3 +10,7 @@ axum = { path = "../../axum" } tokio = { version = "1.0", features = ["full"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } + +[dev-dependencies] +http-body-util = "0.1.0" +tower = { version = "0.5.2", features = ["util"] } diff --git a/examples/templates/src/main.rs b/examples/templates/src/main.rs index 872471c2..4bce0ec1 100644 --- a/examples/templates/src/main.rs +++ b/examples/templates/src/main.rs @@ -25,7 +25,7 @@ async fn main() { .init(); // build our application with some routes - let app = Router::new().route("/greet/{name}", get(greet)); + let app = app(); // run it let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") @@ -35,6 +35,10 @@ async fn main() { axum::serve(listener, app).await.unwrap(); } +fn app() -> Router { + Router::new().route("/greet/{name}", get(greet)) +} + async fn greet(extract::Path(name): extract::Path) -> impl IntoResponse { let template = HelloTemplate { name }; HtmlTemplate(template) @@ -63,3 +67,33 @@ where } } } + +#[cfg(test)] +mod tests { + use super::*; + use axum::{ + body::Body, + http::{Request, StatusCode}, + }; + use http_body_util::BodyExt; + use tower::ServiceExt; + + #[tokio::test] + async fn test_main() { + let response = app() + .oneshot( + Request::builder() + .uri("/greet/Foo") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::OK); + let body = response.into_body(); + let bytes = body.collect().await.unwrap().to_bytes(); + let html = String::from_utf8(bytes.to_vec()).unwrap(); + + assert_eq!(html, "

Hello, Foo!

"); + } +}