From cdf63b2f1a886dfe8007daab9a3161064334a098 Mon Sep 17 00:00:00 2001 From: Louis-Marie Baer Date: Mon, 8 Jul 2024 13:17:43 +0200 Subject: [PATCH] feat: add first request test --- Cargo.toml | 4 ++- src/main.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2f4970b..7404f40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ tracing-subscriber = "0.3" axum = {version="0.7", default-features= false, features= ["tokio", "http2", "macros"] } tokio = {version="1", default-features=false, features= ["rt-multi-thread", "sync", "macros"] } reqwest = {version="0.12", default-features=false, features=["rustls-tls", "http2"]} -url = {version="2.5.0", features=["serde"]} +url = {version="2.5", features=["serde"]} moka = {version="0.12", features=["future"]} ahash = "0.8" uuid = {version="1.8", features=["v4", "fast-rng"]} @@ -21,3 +21,5 @@ nohash = "0.2" derive_more = {version="0.99", default-features=false, features=["deref", "deref_mut"]} enclose = "1.2" typesize = "0.1" +[dev-dependencies] +axum-test = "15.2" diff --git a/src/main.rs b/src/main.rs index bb5b1d6..67a0b36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,21 +34,78 @@ async fn main() -> Result<()> { let config = confy::load_path::("/etc/mnemosyne/config.toml")?; let listen = config.listen_address; info!("creating the cache and index..."); - let state = AppState { - cache: Cache::new(&config), - config, - index_cache: Arc::new(Mutex::new(IndexCache::new())), - client: Client::new(), - }; + let state = new_state(config); info!("Done."); // create route for cache API - let route = Router::new() - .route("/delete/:uuid", delete(api::delete_entry)) - .route("/delete_all", delete(api::delete_all)) - .fallback(api::handler) - .with_state(state); + let route = router().with_state(state); info!("starting to listen on {listen}"); let listener = tokio::net::TcpListener::bind(listen).await?; axum::serve(listener, route.into_make_service()).await?; Ok(()) } + +fn router() -> Router { + Router::new() + .route("/delete/:uuid", delete(api::delete_entry)) + .route("/delete_all", delete(api::delete_all)) + .fallback(api::handler) +} +fn new_state(config: Config) -> AppState { + AppState { + cache: Cache::new(&config), + config, + index_cache: Arc::new(Mutex::new(IndexCache::new())), + client: Client::new(), + } +} +// tests + +#[cfg(test)] +// backend +mod test { + use anyhow::Result; + use axum::{routing::get, Router}; + use axum_test::TestServer; + use tokio::{net::TcpListener, spawn}; + use url::Url; + + use crate::{config::Config, new_state, router}; + + async fn backend_handler() -> &'static str { + "Hello, World!" + } + fn router_backend() -> Router { + Router::new().route("/", get(backend_handler)) + } + // needs to start a backend service, will be assigned an open port by the os + async fn app_backend(listener: TcpListener) -> Result<()> { + axum::serve(listener, router_backend().into_make_service()).await?; + Ok(()) + } + #[tokio::test] + async fn first_request() -> Result<()> { + tracing_subscriber::fmt::init(); + // start backend service + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?; + let port = listener.local_addr().unwrap().port(); + spawn(async move { app_backend(listener).await }); + // configuration of Mnemosyne + let config = Config { + endpoints: vec![( + "/test".to_string(), + Url::parse(&format!("http://127.0.0.1:{port}"))?, + )], + ..Default::default() + }; + // state of Mnemosyne + let state = new_state(config); + // router + let router = router().with_state(state); + // start Mnemosyne + let app = TestServer::new(router).unwrap(); + // send get request for the first time + let rep = app.get("/test").await; + rep.assert_status_ok(); + Ok(()) + } +}