Skip to content

Commit

Permalink
Extract handler::root mod and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 16, 2023
1 parent 0836959 commit 219c0b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion 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::{
Expand Down Expand Up @@ -38,7 +40,7 @@ pub struct Data(pub Box<dyn Store + Send + Sync + 'static>);

pub fn route<T: Clone + HasSchema + HasStore + Send + Sync + 'static>(store: T) -> Router {
Router::new()
.merge(root::route::<T>())
.route("/graphql", routing::get(graphiql).post(handler::<T>))
.route("/", routing::get(|| async { "Hello, World!" }))
.with_state(store)
}
31 changes: 31 additions & 0 deletions rust/crates/web/src/handler/root.rs
@@ -0,0 +1,31 @@
use axum::{routing, Router};

async fn handler<T>() -> &'static str {
"Hello, World!"
}

pub fn route<T: Clone + Send + Sync + 'static>() -> Router<T> {
Router::new().route("/", routing::get(handler::<T>))
}

#[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(())
}
}

0 comments on commit 219c0b2

Please sign in to comment.