From 219c0b22a82af042524bc57de7f2a1865a5ae80a Mon Sep 17 00:00:00 2001 From: bouzuya Date: Fri, 17 Nov 2023 06:34:11 +0900 Subject: [PATCH] Extract handler::root mod and add test --- rust/crates/web/src/handler.rs | 4 +++- rust/crates/web/src/handler/root.rs | 31 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 rust/crates/web/src/handler/root.rs diff --git a/rust/crates/web/src/handler.rs b/rust/crates/web/src/handler.rs index 09396e2..5bd6d47 100644 --- a/rust/crates/web/src/handler.rs +++ b/rust/crates/web/src/handler.rs @@ -1,3 +1,5 @@ +mod root; + use async_graphql::http::GraphiQLSource; use async_graphql_axum::{GraphQLRequest, GraphQLResponse}; use axum::{ @@ -38,7 +40,7 @@ pub struct Data(pub Box); pub fn route(store: T) -> Router { Router::new() + .merge(root::route::()) .route("/graphql", routing::get(graphiql).post(handler::)) - .route("/", routing::get(|| async { "Hello, World!" })) .with_state(store) } diff --git a/rust/crates/web/src/handler/root.rs b/rust/crates/web/src/handler/root.rs new file mode 100644 index 0000000..f017b48 --- /dev/null +++ b/rust/crates/web/src/handler/root.rs @@ -0,0 +1,31 @@ +use axum::{routing, Router}; + +async fn handler() -> &'static str { + "Hello, World!" +} + +pub fn route() -> Router { + Router::new().route("/", routing::get(handler::)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_utils::{send_request, ResponseExt}; + use axum::http::{Request, StatusCode}; + + #[tokio::test] + async fn test() -> anyhow::Result<()> { + let app = route(); + let request = Request::builder() + .method("GET") + .uri("/") + .header("Content-Type", "application/json") + .body(hyper::Body::empty())?; + let response = send_request(app, request).await?; + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.into_body_as_string().await?, "Hello, World!"); + Ok(()) + } +}