|
| 1 | +use axum::Router; |
| 2 | +use axum::body::Body; |
| 3 | +use axum::http::{Request, StatusCode}; |
| 4 | +use axum::routing::get; |
| 5 | +use std::sync::{Arc, RwLock}; |
| 6 | +use tower::ServiceExt; |
| 7 | + |
| 8 | +// Import the Database trait |
| 9 | +use flagpole::db::Database; |
| 10 | + |
| 11 | +#[cfg(not(feature = "redis"))] |
| 12 | +use flagpole::db::mem::InMemoryDb; |
| 13 | + |
| 14 | +#[cfg(feature = "redis")] |
| 15 | +use flagpole::db::redis::RedisDb; |
| 16 | + |
| 17 | +#[derive(Clone)] |
| 18 | +struct AppState<T> |
| 19 | +where |
| 20 | + T: Database, |
| 21 | +{ |
| 22 | + db: Arc<RwLock<T>>, |
| 23 | + api_key: Option<String>, |
| 24 | +} |
| 25 | + |
| 26 | +async fn health_check_handler( |
| 27 | + axum::extract::State(state): axum::extract::State<AppState<impl Database>>, |
| 28 | +) -> StatusCode { |
| 29 | + let db = state.db.read().unwrap(); |
| 30 | + match db.health_check() { |
| 31 | + Ok(_) => StatusCode::OK, |
| 32 | + Err(_) => StatusCode::SERVICE_UNAVAILABLE, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[cfg(not(feature = "redis"))] |
| 37 | +#[tokio::test] |
| 38 | +async fn test_health_check_in_memory() { |
| 39 | + let state = AppState { |
| 40 | + db: Arc::new(RwLock::new(InMemoryDb::new())), |
| 41 | + api_key: None, |
| 42 | + }; |
| 43 | + |
| 44 | + let app = Router::new().route("/health", get(health_check_handler)).with_state(state); |
| 45 | + |
| 46 | + let response = app |
| 47 | + .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap()) |
| 48 | + .await |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + assert_eq!(response.status(), StatusCode::OK); |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(feature = "redis")] |
| 55 | +#[tokio::test] |
| 56 | +async fn test_health_check_redis_not_connected() { |
| 57 | + // Use an invalid Redis URI to simulate connection failure |
| 58 | + let state = AppState { |
| 59 | + db: Arc::new(RwLock::new(RedisDb::new("redis://invalid-host:6379".to_string()))), |
| 60 | + api_key: None, |
| 61 | + }; |
| 62 | + |
| 63 | + let app = Router::new().route("/health", get(health_check_handler)).with_state(state); |
| 64 | + |
| 65 | + let response = app |
| 66 | + .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap()) |
| 67 | + .await |
| 68 | + .unwrap(); |
| 69 | + |
| 70 | + assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE); |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(feature = "redis")] |
| 74 | +#[tokio::test] |
| 75 | +async fn test_health_check_redis_connected() { |
| 76 | + // This test requires a running Redis instance at localhost:6379 |
| 77 | + // Skip if Redis is not available |
| 78 | + let redis_uri = |
| 79 | + std::env::var("REDIS_URI").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string()); |
| 80 | + |
| 81 | + let state = AppState { |
| 82 | + db: Arc::new(RwLock::new(RedisDb::new(redis_uri))), |
| 83 | + api_key: None, |
| 84 | + }; |
| 85 | + |
| 86 | + let app = Router::new().route("/health", get(health_check_handler)).with_state(state); |
| 87 | + |
| 88 | + let response = app |
| 89 | + .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap()) |
| 90 | + .await |
| 91 | + .unwrap(); |
| 92 | + |
| 93 | + // This will pass if Redis is available, otherwise it will fail |
| 94 | + // In a real CI/CD setup, you'd ensure Redis is running or skip this test |
| 95 | + let status = response.status(); |
| 96 | + assert!( |
| 97 | + status == StatusCode::OK || status == StatusCode::SERVICE_UNAVAILABLE, |
| 98 | + "Expected OK or SERVICE_UNAVAILABLE, got {:?}", |
| 99 | + status |
| 100 | + ); |
| 101 | +} |
0 commit comments