Skip to content

Test health check endpoint #7

@MrSheerluck

Description

@MrSheerluck

Add a test for the GET /api/health endpoint. When the database is up, it should return 200 { "status": "ok" }.

If you want to discuss anything, please tag me here. Before picking the issue, comment that you want to work on this

Here's a suggested solution for you to try:
Create a test module in api/src/main.rs:

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{body::Body, http::Request, routing::get};
    use tower::ServiceExt;

    #[tokio::test]
    async fn test_health_check_ok() {
        let pool = sqlx::SqlitePool::connect("sqlite::memory:")
            .await
            .expect("failed to create pool");
        sqlx::query("CREATE TABLE IF NOT EXISTS notes (id TEXT)")
            .execute(&pool)
            .await
            .unwrap();

        let state = AppState {
            db: pool,
            client: Client::new_empty(),
            bucket: String::new(),
        };

        let app = Router::new()
            .route("/api/health", get(health_check))
            .with_state(state);

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/health")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);

        let body: serde_json::Value = axum::body::to_bytes(response.into_body(), 1024)
            .await
            .map(|b| serde_json::from_slice(&b).unwrap())
            .unwrap();
        assert_eq!(body, serde_json::json!({"status": "ok"}));
    }
}

Add tower and http-body-util to dev dependencies in Cargo.toml:

[dev-dependencies]
tower = "0.5"
http-body-util = "0.1"

Running Tests

# Backend tests
cd api && cargo test

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions