test(sdk_relay): ajout tests HTTP (/health, /metrics) et WS (handshake)

This commit is contained in:
Nicolas Cantu 2025-08-25 15:39:35 +02:00
parent 62cfd25d3d
commit 636061f064
3 changed files with 75 additions and 0 deletions

22
tests/http_health.rs Normal file
View File

@ -0,0 +1,22 @@
use std::time::Duration;
#[tokio::test]
async fn http_health_endpoint_should_return_healthy() {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.expect("cannot build client");
let url = std::env::var("SDK_RELAY_HTTP").unwrap_or_else(|_| "http://localhost:8091".to_string());
let res = client
.get(format!("{}/health", url))
.send()
.await
.expect("cannot call /health");
assert!(res.status().is_success(), "status: {}", res.status());
let json: serde_json::Value = res.json().await.expect("invalid json");
let status = json.get("status").and_then(|v| v.as_str()).unwrap_or("");
assert_eq!(status, "healthy", "health status should be healthy, got: {}", status);
}

23
tests/http_metrics.rs Normal file
View File

@ -0,0 +1,23 @@
use std::time::Duration;
#[tokio::test]
async fn http_metrics_endpoint_should_return_expected_fields() {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.expect("cannot build client");
let url = std::env::var("SDK_RELAY_HTTP").unwrap_or_else(|_| "http://localhost:8091".to_string());
let res = client
.get(format!("{}/metrics", url))
.send()
.await
.expect("cannot call /metrics");
assert!(res.status().is_success(), "status: {}", res.status());
let json: serde_json::Value = res.json().await.expect("invalid json");
assert!(json.get("sync_metrics").is_some(), "missing sync_metrics");
assert!(json.get("system_metrics").is_some(), "missing system_metrics");
}

30
tests/ws_handshake.rs Normal file
View File

@ -0,0 +1,30 @@
use futures_util::{SinkExt, StreamExt};
use serde_json::json;
use tokio_tungstenite::connect_async;
#[tokio::test]
async fn websocket_handshake_should_be_accepted() {
let url = std::env::var("SDK_RELAY_WS").unwrap_or_else(|_| "ws://localhost:8090".to_string());
let (mut ws, _resp) = connect_async(url).await.expect("cannot connect ws");
let handshake = json!({
"type": "handshake",
"client_id": "test-client",
"version": "1.0.0",
"capabilities": ["sync", "notifications", "health"],
"timestamp": 1703001600u64
})
.to_string();
ws.send(tokio_tungstenite::tungstenite::Message::Text(handshake))
.await
.expect("cannot send handshake");
let msg = ws.next().await.expect("no response").expect("ws error");
let txt = msg.into_text().expect("not text");
let json: serde_json::Value = serde_json::from_str(&txt).expect("invalid json");
assert_eq!(json.get("type").and_then(|v| v.as_str()).unwrap_or(""), "handshake_response");
assert_eq!(json.get("status").and_then(|v| v.as_str()).unwrap_or(""), "accepted");
}