aro-web 1.0.0

HTTP/ADR layer for the Aro web framework
Documentation
//! Health check endpoint.
//!
//! Provides a simple `GET /health` route that returns `{"status": "ok"}`.
//!
//! # Example
//!
//! ```
//! use aro_web::health;
//! use aro_web::App;
//!
//! let _app = App::new().routes(health::routes()).build();
//! ```

use axum::Router;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::routing::get;

/// Returns a router with `GET /health` that responds with `{"status": "ok"}`.
pub fn routes() -> Router {
    Router::new().route("/health", get(health_handler))
}

async fn health_handler() -> impl IntoResponse {
    (
        StatusCode::OK,
        [(axum::http::header::CONTENT_TYPE, "application/json")],
        r#"{"status":"ok"}"#,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::App;
    use crate::test::TestClient;

    #[tokio::test]
    async fn health_check_returns_200_with_ok_status() {
        let app = App::new().without_defaults().routes(routes()).build();
        let client = TestClient::new(app);

        let resp = client.get("/health").send().await;
        resp.assert_status(200);

        let body: serde_json::Value = resp.json();
        assert_eq!(body["status"], "ok");
    }
}