From 7cc4a9301fe8629028b738280b4055d867d6b36f Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 10 Sep 2022 15:26:42 +0530 Subject: [PATCH] feat: add health endpoint and tests --- src/api/v1/meta.rs | 52 +++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/api/v1/meta.rs b/src/api/v1/meta.rs index f1d7d6d..447f88d 100644 --- a/src/api/v1/meta.rs +++ b/src/api/v1/meta.rs @@ -69,7 +69,7 @@ async fn health(ctx: crate::AppCtx) -> impl Responder { resp_builder.db(ctx.db.ping().await); - HttpResponse::Ok() //.json(resp_builder.build().unwrap()) + HttpResponse::Ok().json(resp_builder.build().unwrap()) } pub fn services(cfg: &mut web::ServiceConfig) { @@ -100,34 +100,24 @@ pub mod tests { assert_eq!(resp.status(), StatusCode::OK); } - // #[actix_rt::test] - // async fn health_works_pg() { - // let data = crate::tests::pg::get_data().await; - // health_works(data).await; - // } - // - // #[actix_rt::test] - // async fn health_works_maria() { - // let data = crate::tests::maria::get_data().await; - // health_works(data).await; - // } - // - // pub async fn health_works(data: ArcCtx) { - // println!("{}", API_V1_ROUTES.meta.health); - // let data = &data; - // let app = get_app!(data).await; - // - // let resp = test::call_service( - // &app, - // test::TestRequest::get() - // .uri(API_V1_ROUTES.meta.health) - // .to_request(), - // ) - // .await; - // assert_eq!(resp.status(), StatusCode::OK); - // - // let health_resp: Health = test::read_body_json(resp).await; - // assert!(health_resp.db); - // assert_eq!(health_resp.redis, Some(true)); - // } + #[actix_rt::test] + async fn health_works() { + use actix_web::test; + + let settings = Settings::new().unwrap(); + let ctx = AppCtx::new(crate::ctx::Ctx::new(&settings).await); + let app = test::init_service(App::new().app_data(ctx.clone()).configure(services)).await; + + let resp = test::call_service( + &app, + test::TestRequest::get() + .uri(API_V1_ROUTES.meta.health) + .to_request(), + ) + .await; + assert_eq!(resp.status(), StatusCode::OK); + + let health_resp: super::Health = test::read_body_json(resp).await; + assert!(health_resp.db); + } }