use rustapi_core::{get, RustApi};
use std::time::Duration;
use tokio::sync::oneshot;
#[tokio::test]
async fn test_status_page() {
async fn task_handler() -> &'static str {
"ok"
}
let app = RustApi::new()
.status_page() .route("/task", get(task_handler));
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let port = addr.port();
drop(listener);
let (tx, rx) = oneshot::channel();
let addr_str = format!("127.0.0.1:{}", port);
let server_handle = tokio::spawn(async move {
app.run_with_shutdown(&addr_str, async {
rx.await.ok();
})
.await
});
tokio::time::sleep(Duration::from_millis(200)).await;
let client = reqwest::Client::new();
let base_url = format!("http://127.0.0.1:{}", port);
let res = client
.get(format!("{}/status", base_url))
.send()
.await
.expect("Failed to get status");
assert_eq!(res.status(), 200);
let body = res.text().await.unwrap();
assert!(body.contains("System Status"));
assert!(body.contains("Total Requests"));
for _ in 0..2 {
let res = client
.get(format!("{}/task", base_url))
.send()
.await
.expect("Failed to get task");
assert_eq!(res.status(), 200);
}
let res = client
.get(format!("{}/status", base_url))
.send()
.await
.expect("Failed to get status");
assert_eq!(res.status(), 200);
let body = res.text().await.unwrap();
assert!(body.contains("/task"));
tx.send(()).unwrap();
let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}