mockforge-http 0.3.116

HTTP/REST protocol support for MockForge
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
//! Kubernetes-native health check endpoints
//!
//! This module provides comprehensive health check endpoints following Kubernetes best practices:
//! - **Liveness probe**: Indicates if the container is alive
//! - **Readiness probe**: Indicates if the container is ready to accept traffic
//! - **Startup probe**: Indicates if the container has finished initialization
//!
//! These endpoints are essential for:
//! - Kubernetes deployment orchestration
//! - Load balancer health checks
//! - Service discovery integration
//! - Graceful shutdown coordination

use axum::{extract::State, http::StatusCode, response::Json, routing::get};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

/// Service initialization status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServiceStatus {
    /// Service is initializing (not ready)
    Initializing,
    /// Service is ready to accept traffic
    Ready,
    /// Service is shutting down (not accepting new requests)
    ShuttingDown,
    /// Service has failed and is unhealthy
    Failed,
}

impl ServiceStatus {
    /// Check if service is ready to accept traffic
    pub fn is_ready(&self) -> bool {
        matches!(self, ServiceStatus::Ready)
    }

    /// Check if service is alive (not failed)
    pub fn is_alive(&self) -> bool {
        !matches!(self, ServiceStatus::Failed)
    }
}

/// Health check manager for tracking service state
#[derive(Debug, Clone)]
pub struct HealthManager {
    /// Current service status
    status: Arc<RwLock<ServiceStatus>>,
    /// Server startup time
    start_time: Arc<Instant>,
    /// Service initialization deadline (timeout)
    init_deadline: Arc<Option<Instant>>,
    /// Shutdown signal for graceful termination
    shutdown_signal: Arc<RwLock<Option<tokio::sync::oneshot::Sender<()>>>>,
}

impl HealthManager {
    /// Create a new health manager
    pub fn new() -> Self {
        Self {
            status: Arc::new(RwLock::new(ServiceStatus::Initializing)),
            start_time: Arc::new(Instant::now()),
            init_deadline: Arc::new(None),
            shutdown_signal: Arc::new(RwLock::new(None)),
        }
    }

    /// Create a new health manager with initialization timeout
    pub fn with_init_timeout(timeout: Duration) -> Self {
        let deadline = Instant::now() + timeout;
        Self {
            status: Arc::new(RwLock::new(ServiceStatus::Initializing)),
            start_time: Arc::new(Instant::now()),
            init_deadline: Arc::new(Some(deadline)),
            shutdown_signal: Arc::new(RwLock::new(None)),
        }
    }

    /// Mark service as ready
    pub async fn set_ready(&self) {
        let mut status = self.status.write().await;
        *status = ServiceStatus::Ready;
        info!("Service marked as ready");
    }

    /// Mark service as failed
    pub async fn set_failed(&self, reason: &str) {
        let mut status = self.status.write().await;
        *status = ServiceStatus::Failed;
        error!("Service marked as failed: {}", reason);
    }

    /// Mark service as shutting down
    pub async fn set_shutting_down(&self) {
        let mut status = self.status.write().await;
        *status = ServiceStatus::ShuttingDown;
        info!("Service marked as shutting down");
    }

    /// Get current service status
    pub async fn get_status(&self) -> ServiceStatus {
        *self.status.read().await
    }

    /// Get server uptime in seconds
    pub fn uptime_seconds(&self) -> u64 {
        self.start_time.elapsed().as_secs()
    }

    /// Check if initialization has timed out
    pub fn is_init_timeout(&self) -> bool {
        if let Some(deadline) = *self.init_deadline {
            Instant::now() > deadline
        } else {
            false
        }
    }

    /// Set shutdown signal receiver for graceful shutdown
    pub async fn set_shutdown_signal(&self, sender: tokio::sync::oneshot::Sender<()>) {
        let mut signal = self.shutdown_signal.write().await;
        *signal = Some(sender);
    }

    /// Trigger graceful shutdown
    pub async fn trigger_shutdown(&self) {
        self.set_shutting_down().await;
        let mut signal = self.shutdown_signal.write().await;
        if let Some(sender) = signal.take() {
            let _ = sender.send(());
            info!("Graceful shutdown signal sent");
        }
    }
}

impl Default for HealthManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Health check response structure
#[derive(Debug, Serialize, Deserialize)]
pub struct HealthResponse {
    /// Service status
    pub status: String,
    /// ISO 8601 timestamp
    pub timestamp: String,
    /// Server uptime in seconds
    pub uptime_seconds: u64,
    /// Service version
    pub version: String,
    /// Additional status information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<HealthDetails>,
}

/// Detailed health information
#[derive(Debug, Serialize, Deserialize)]
pub struct HealthDetails {
    /// Service initialization status
    pub initialization: String,
    /// Active connection count (if available)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub connections: Option<u64>,
    /// Memory usage in bytes (if available)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memory_bytes: Option<u64>,
}

/// Liveness probe endpoint
///
/// Kubernetes uses this to determine if the container should be restarted.
/// Returns 200 if the service is alive, 503 if it has failed.
///
/// This should be a lightweight check that doesn't depend on external services.
async fn liveness_probe(
    State(health): State<Arc<HealthManager>>,
) -> Result<Json<HealthResponse>, StatusCode> {
    let status = health.get_status().await;
    let uptime = health.uptime_seconds();

    // Liveness checks if the process is alive (not failed)
    if status.is_alive() {
        let response = HealthResponse {
            status: "alive".to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            uptime_seconds: uptime,
            version: env!("CARGO_PKG_VERSION").to_string(),
            details: None,
        };
        Ok(Json(response))
    } else {
        Err(StatusCode::SERVICE_UNAVAILABLE)
    }
}

/// Readiness probe endpoint
///
/// Kubernetes uses this to determine if the container is ready to receive traffic.
/// Returns 200 if the service is ready, 503 if it's not ready or shutting down.
///
/// This checks if the service has completed initialization and is ready to serve requests.
async fn readiness_probe(
    State(health): State<Arc<HealthManager>>,
) -> Result<Json<HealthResponse>, (StatusCode, Json<HealthResponse>)> {
    let status = health.get_status().await;
    let uptime = health.uptime_seconds();

    // Readiness checks if the service is ready to accept traffic
    if status.is_ready() {
        let response = HealthResponse {
            status: "ready".to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            uptime_seconds: uptime,
            version: env!("CARGO_PKG_VERSION").to_string(),
            details: Some(HealthDetails {
                initialization: "complete".to_string(),
                connections: None,
                memory_bytes: None,
            }),
        };
        Ok(Json(response))
    } else {
        let details = match status {
            ServiceStatus::Initializing => {
                if health.is_init_timeout() {
                    "initialization_timeout".to_string()
                } else {
                    "initializing".to_string()
                }
            }
            ServiceStatus::ShuttingDown => "shutting_down".to_string(),
            ServiceStatus::Failed => "failed".to_string(),
            ServiceStatus::Ready => unreachable!(),
        };

        let response = HealthResponse {
            status: "not_ready".to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            uptime_seconds: uptime,
            version: env!("CARGO_PKG_VERSION").to_string(),
            details: Some(HealthDetails {
                initialization: details,
                connections: None,
                memory_bytes: None,
            }),
        };

        Err((StatusCode::SERVICE_UNAVAILABLE, Json(response)))
    }
}

/// Startup probe endpoint
///
/// Kubernetes uses this to determine if the container has finished initialization.
/// This is useful for services that take a long time to start.
/// Returns 200 once initialization is complete, 503 while still initializing.
async fn startup_probe(
    State(health): State<Arc<HealthManager>>,
) -> Result<Json<HealthResponse>, StatusCode> {
    let status = health.get_status().await;
    let uptime = health.uptime_seconds();

    // Startup probe checks if initialization is complete
    match status {
        ServiceStatus::Ready => {
            let response = HealthResponse {
                status: "startup_complete".to_string(),
                timestamp: chrono::Utc::now().to_rfc3339(),
                uptime_seconds: uptime,
                version: env!("CARGO_PKG_VERSION").to_string(),
                details: Some(HealthDetails {
                    initialization: "complete".to_string(),
                    connections: None,
                    memory_bytes: None,
                }),
            };
            Ok(Json(response))
        }
        ServiceStatus::Initializing => {
            if health.is_init_timeout() {
                warn!("Startup probe: initialization timeout exceeded");
                Err(StatusCode::SERVICE_UNAVAILABLE)
            } else {
                debug!("Startup probe: still initializing");
                Err(StatusCode::SERVICE_UNAVAILABLE)
            }
        }
        ServiceStatus::Failed => Err(StatusCode::SERVICE_UNAVAILABLE),
        ServiceStatus::ShuttingDown => {
            // During shutdown, startup probe should return ready (service was started)
            let response = HealthResponse {
                status: "startup_complete".to_string(),
                timestamp: chrono::Utc::now().to_rfc3339(),
                uptime_seconds: uptime,
                version: env!("CARGO_PKG_VERSION").to_string(),
                details: Some(HealthDetails {
                    initialization: "complete".to_string(),
                    connections: None,
                    memory_bytes: None,
                }),
            };
            Ok(Json(response))
        }
    }
}

/// Combined health check endpoint (backwards compatibility)
///
/// This endpoint provides a general health check that combines liveness and readiness.
/// For Kubernetes deployments, prefer using the specific probe endpoints.
async fn health_check(
    State(health): State<Arc<HealthManager>>,
) -> Result<Json<HealthResponse>, (StatusCode, Json<HealthResponse>)> {
    let status = health.get_status().await;
    let uptime = health.uptime_seconds();

    if status.is_ready() {
        let response = HealthResponse {
            status: "healthy".to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            uptime_seconds: uptime,
            version: env!("CARGO_PKG_VERSION").to_string(),
            details: Some(HealthDetails {
                initialization: "complete".to_string(),
                connections: None,
                memory_bytes: None,
            }),
        };
        Ok(Json(response))
    } else {
        let status_str = match status {
            ServiceStatus::Initializing => "initializing",
            ServiceStatus::ShuttingDown => "shutting_down",
            ServiceStatus::Failed => "failed",
            ServiceStatus::Ready => unreachable!(),
        };

        let response = HealthResponse {
            status: status_str.to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            uptime_seconds: uptime,
            version: env!("CARGO_PKG_VERSION").to_string(),
            details: Some(HealthDetails {
                initialization: status_str.to_string(),
                connections: None,
                memory_bytes: None,
            }),
        };

        Err((StatusCode::SERVICE_UNAVAILABLE, Json(response)))
    }
}

/// Create health check router with all probe endpoints
pub fn health_router(health_manager: Arc<HealthManager>) -> axum::Router {
    use axum::Router;
    Router::new()
        .route("/health", get(health_check))
        .route("/health/live", get(liveness_probe))
        .route("/health/ready", get(readiness_probe))
        .route("/health/startup", get(startup_probe))
        .with_state(health_manager)
}

/// Create health check router with custom prefix
pub fn health_router_with_prefix(health_manager: Arc<HealthManager>, prefix: &str) -> axum::Router {
    use axum::Router;
    Router::new()
        .route(&format!("{}/health", prefix), get(health_check))
        .route(&format!("{}/health/live", prefix), get(liveness_probe))
        .route(&format!("{}/health/ready", prefix), get(readiness_probe))
        .route(&format!("{}/health/startup", prefix), get(startup_probe))
        .with_state(health_manager)
}

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

    // ==================== ServiceStatus Tests ====================

    #[test]
    fn test_service_status() {
        assert!(ServiceStatus::Ready.is_ready());
        assert!(!ServiceStatus::Initializing.is_ready());
        assert!(!ServiceStatus::ShuttingDown.is_ready());
        assert!(!ServiceStatus::Failed.is_ready());

        assert!(ServiceStatus::Ready.is_alive());
        assert!(ServiceStatus::Initializing.is_alive());
        assert!(ServiceStatus::ShuttingDown.is_alive());
        assert!(!ServiceStatus::Failed.is_alive());
    }

    #[test]
    fn test_service_status_eq() {
        assert_eq!(ServiceStatus::Ready, ServiceStatus::Ready);
        assert_ne!(ServiceStatus::Ready, ServiceStatus::Failed);
        assert_eq!(ServiceStatus::Initializing, ServiceStatus::Initializing);
    }

    #[test]
    fn test_service_status_clone() {
        let status = ServiceStatus::Ready;
        let cloned = status;
        assert_eq!(status, cloned);
    }

    #[test]
    fn test_service_status_debug() {
        let debug = format!("{:?}", ServiceStatus::Ready);
        assert!(debug.contains("Ready"));
    }

    // ==================== HealthManager Tests ====================

    #[tokio::test]
    async fn test_health_manager_new() {
        let manager = HealthManager::new();
        let status = manager.get_status().await;
        assert_eq!(status, ServiceStatus::Initializing);
    }

    #[tokio::test]
    async fn test_health_manager_default() {
        let manager = HealthManager::default();
        let status = manager.get_status().await;
        assert_eq!(status, ServiceStatus::Initializing);
    }

    #[tokio::test]
    async fn test_health_manager_with_init_timeout() {
        let manager = HealthManager::with_init_timeout(Duration::from_secs(30));
        let status = manager.get_status().await;
        assert_eq!(status, ServiceStatus::Initializing);
        assert!(!manager.is_init_timeout());
    }

    #[tokio::test]
    async fn test_health_manager_set_ready() {
        let manager = HealthManager::new();
        manager.set_ready().await;
        let status = manager.get_status().await;
        assert_eq!(status, ServiceStatus::Ready);
    }

    #[tokio::test]
    async fn test_health_manager_set_failed() {
        let manager = HealthManager::new();
        manager.set_failed("test error").await;
        let status = manager.get_status().await;
        assert_eq!(status, ServiceStatus::Failed);
    }

    #[tokio::test]
    async fn test_health_manager_set_shutting_down() {
        let manager = HealthManager::new();
        manager.set_shutting_down().await;
        let status = manager.get_status().await;
        assert_eq!(status, ServiceStatus::ShuttingDown);
    }

    #[tokio::test]
    async fn test_health_manager_uptime() {
        let manager = HealthManager::new();
        let uptime = manager.uptime_seconds();
        // Uptime should be very small immediately after creation
        assert!(uptime < 5);
    }

    #[tokio::test]
    async fn test_health_manager_clone() {
        let manager = HealthManager::new();
        manager.set_ready().await;
        let cloned = manager.clone();
        let status = cloned.get_status().await;
        assert_eq!(status, ServiceStatus::Ready);
    }

    #[tokio::test]
    async fn test_health_manager_trigger_shutdown() {
        let manager = HealthManager::new();
        manager.set_ready().await;
        manager.trigger_shutdown().await;
        let status = manager.get_status().await;
        assert_eq!(status, ServiceStatus::ShuttingDown);
    }

    // ==================== HealthResponse Tests ====================

    #[test]
    fn test_health_response_serialization() {
        let response = HealthResponse {
            status: "healthy".to_string(),
            timestamp: "2024-01-15T10:30:00Z".to_string(),
            uptime_seconds: 3600,
            version: "1.0.0".to_string(),
            details: None,
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("healthy"));
        assert!(json.contains("3600"));
        assert!(json.contains("1.0.0"));
    }

    #[test]
    fn test_health_response_with_details() {
        let response = HealthResponse {
            status: "healthy".to_string(),
            timestamp: "2024-01-15T10:30:00Z".to_string(),
            uptime_seconds: 3600,
            version: "1.0.0".to_string(),
            details: Some(HealthDetails {
                initialization: "complete".to_string(),
                connections: Some(10),
                memory_bytes: Some(1024 * 1024),
            }),
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("complete"));
        assert!(json.contains("connections"));
    }

    #[test]
    fn test_health_response_deserialization() {
        let json = r#"{
            "status": "healthy",
            "timestamp": "2024-01-15T10:30:00Z",
            "uptime_seconds": 7200,
            "version": "2.0.0"
        }"#;

        let response: HealthResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.status, "healthy");
        assert_eq!(response.uptime_seconds, 7200);
        assert_eq!(response.version, "2.0.0");
    }

    // ==================== HealthDetails Tests ====================

    #[test]
    fn test_health_details_serialization() {
        let details = HealthDetails {
            initialization: "complete".to_string(),
            connections: Some(5),
            memory_bytes: Some(2048),
        };

        let json = serde_json::to_string(&details).unwrap();
        assert!(json.contains("complete"));
        assert!(json.contains("5"));
        assert!(json.contains("2048"));
    }

    #[test]
    fn test_health_details_optional_fields() {
        let details = HealthDetails {
            initialization: "initializing".to_string(),
            connections: None,
            memory_bytes: None,
        };

        let json = serde_json::to_string(&details).unwrap();
        assert!(json.contains("initializing"));
        assert!(!json.contains("connections"));
        assert!(!json.contains("memory_bytes"));
    }

    // ==================== Probe Endpoint Tests ====================

    #[tokio::test]
    async fn test_liveness_probe_alive() {
        let health = Arc::new(HealthManager::new());
        health.set_ready().await;

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

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

    #[tokio::test]
    async fn test_liveness_probe_failed() {
        let health = Arc::new(HealthManager::new());
        health.set_failed("test failure").await;

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

        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
    }

    #[tokio::test]
    async fn test_liveness_probe_initializing() {
        let health = Arc::new(HealthManager::new());
        // Initializing is still alive

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

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

    #[tokio::test]
    async fn test_liveness_probe_shutting_down() {
        let health = Arc::new(HealthManager::new());
        health.set_shutting_down().await;

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

        // Shutting down is still alive
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn test_readiness_probe_ready() {
        let health = Arc::new(HealthManager::new());
        health.set_ready().await;

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

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

    #[tokio::test]
    async fn test_readiness_probe_initializing() {
        let health = Arc::new(HealthManager::new());

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

        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
    }

    #[tokio::test]
    async fn test_readiness_probe_shutting_down() {
        let health = Arc::new(HealthManager::new());
        health.set_shutting_down().await;

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

        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
    }

    #[tokio::test]
    async fn test_startup_probe_ready() {
        let health = Arc::new(HealthManager::new());
        health.set_ready().await;

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

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

    #[tokio::test]
    async fn test_startup_probe_initializing() {
        let health = Arc::new(HealthManager::new());

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

        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
    }

    #[tokio::test]
    async fn test_startup_probe_shutting_down() {
        let health = Arc::new(HealthManager::new());
        health.set_ready().await;
        health.set_shutting_down().await;

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

        // During shutdown, startup probe returns OK (service was started)
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn test_health_check_ready() {
        let health = Arc::new(HealthManager::new());
        health.set_ready().await;

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

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

    #[tokio::test]
    async fn test_health_check_not_ready() {
        let health = Arc::new(HealthManager::new());

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

        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
    }

    // ==================== Router Tests ====================

    #[test]
    fn test_health_router_creation() {
        let health = Arc::new(HealthManager::new());
        let router = health_router(health);
        let _ = router;
    }

    #[test]
    fn test_health_router_with_prefix() {
        let health = Arc::new(HealthManager::new());
        let router = health_router_with_prefix(health, "/api/v1");
        let _ = router;
    }
}