From 8a5430662e6ec8a0af4426d6d1c0bb7cc8b8dad9 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 2 Apr 2025 21:04:38 -0400 Subject: [PATCH] Added test --- Cargo.toml | 3 ++ src/main.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 75 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 112e2b2..489e626 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,11 @@ tower = { version = "0.5.2" } hyper = { version = "1.6.0" } sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio-native-tls"] } dotenv = { version = "0.15" } +http-body-util = "0.1.3" icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" } [dev-dependencies] testcontainers = { version = "0.23.3" } testcontainers-modules = { version = "0.11.6", features = ["blocking", "postgres"] } +reqwest = { version = "0.12", features = ["json"] } # For making HTTP requests in tests +once_cell = "1.19" # Useful for lazy initialization in tests/app setup \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index bfd25b9..489ae66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,28 +6,87 @@ use axum::{ use icarus_auth::callers; use icarus_auth::config; +// use sqlx::Postgres; #[tokio::main] async fn main() { // initialize tracing tracing_subscriber::fmt::init(); - let pool = icarus_auth::db_pool::create_pool() - .await - .expect("Failed to create pool"); - - // build our application with a route - let app = Router::new() - .route(callers::endpoints::DBTEST, get(callers::common::db_ping)) - .route(callers::endpoints::ROOT, get(callers::common::root)) - .route( - callers::endpoints::REGISTER, - post(callers::register::register_user), - ) - .layer(axum::Extension(pool)); + let app = app().await; // run our app with hyper, listening globally on port 3000 let url = config::get_full(); let listener = tokio::net::TcpListener::bind(url).await.unwrap(); axum::serve(listener, app).await.unwrap(); } + +async fn app() -> Router { + let pool = icarus_auth::db_pool::create_pool() + .await + .expect("Failed to create pool"); + + // build our application with a route + Router::new() + .route(callers::endpoints::DBTEST, get(callers::common::db_ping)) + .route(callers::endpoints::ROOT, get(callers::common::root)) + .route( + callers::endpoints::REGISTER, + post(callers::register::register_user), + ) + .layer(axum::Extension(pool)) +} + +#[cfg(test)] +mod tests { + use super::*; + use axum::{ + body::Body, + extract::connect_info::MockConnectInfo, + http::{self, Request, StatusCode}, + }; + use http_body_util::BodyExt; + // use http_body_util::BodyExt; // for `collect` + use serde_json::{Value, json}; + use tokio::net::TcpListener; + use tower::{Service, ServiceExt}; // for `call`, `oneshot`, and `ready` + + #[tokio::test] + async fn hello_world() { + let app = app().await; + + // `Router` implements `tower::Service>` so we can + // call it like any tower service, no need to run an HTTP server. + let response = app + .oneshot( + Request::builder() + .uri(callers::endpoints::ROOT) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + + assert_eq!(response.status(), StatusCode::OK); + + /* + match response.into_body().collect().await { + Ok(o) => { + let parsed: String = match String::from_utf8(o.to_bytes()) { + Ok(s) => s, + Err(err) => { + String::new() + } + }; + } + Err(err) => { + assert!(false, + "Error: {:?}", err.to_string()); + } + } + */ + + let body = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(&body[..], b"Hello, World!"); + } +}