diff --git a/tests/http_health.rs b/tests/http_health.rs new file mode 100644 index 0000000..516a849 --- /dev/null +++ b/tests/http_health.rs @@ -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); +} diff --git a/tests/http_metrics.rs b/tests/http_metrics.rs new file mode 100644 index 0000000..72cb814 --- /dev/null +++ b/tests/http_metrics.rs @@ -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"); +} diff --git a/tests/ws_handshake.rs b/tests/ws_handshake.rs new file mode 100644 index 0000000..a742cae --- /dev/null +++ b/tests/ws_handshake.rs @@ -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"); +}