#[cfg(feature = "examples")]
#[cfg(test)]
mod tests {
use std::time::Duration;
use tokio::time::timeout;
struct TestClient {
base_url: String,
}
impl TestClient {
fn new(base_url: String) -> Self {
Self { base_url }
}
async fn get_with_timeout(
&self,
path: &str,
timeout_duration: Duration,
) -> Result<String, Box<dyn std::error::Error>> {
let url = format!("{}{}", self.base_url, path);
let client = reqwest::Client::new();
let response = timeout(timeout_duration, client.get(&url).send()).await??;
let text = response.text().await?;
Ok(text)
}
async fn get_status(&self, path: &str) -> Result<u16, Box<dyn std::error::Error>> {
let url = format!("{}{}", self.base_url, path);
let client = reqwest::Client::new();
let response = client.get(&url).send().await?;
Ok(response.status().as_u16())
}
}
#[tokio::test]
#[ignore] async fn test_timeout_behavior() {
let client = TestClient::new("http://127.0.0.1:8080".to_string());
let result = client
.get_with_timeout("/delay/5", Duration::from_secs(1))
.await;
assert!(result.is_err(), "Expected timeout error");
let error_msg = result.unwrap_err().to_string();
assert!(
error_msg.contains("timeout") || error_msg.contains("elapsed"),
"Error should be a timeout: {}",
error_msg
);
}
#[tokio::test]
#[ignore] async fn test_successful_request() {
let client = TestClient::new("http://127.0.0.1:8080".to_string());
let result = client
.get_with_timeout("/delay/1", Duration::from_secs(5))
.await;
assert!(result.is_ok(), "Request should succeed");
let response = result.unwrap();
assert!(
response.contains("args"),
"Response should contain request info"
);
}
#[tokio::test]
#[ignore] async fn test_status_codes() {
let client = TestClient::new("http://127.0.0.1:8080".to_string());
let status = client.get_status("/status/404").await.unwrap();
assert_eq!(status, 404, "Should return 404 status");
let status = client.get_status("/status/200").await.unwrap();
assert_eq!(status, 200, "Should return 200 status");
let status = client.get_status("/status/500").await.unwrap();
assert_eq!(status, 500, "Should return 500 status");
}
#[tokio::test]
#[ignore] async fn test_json_response() {
let client = TestClient::new("http://127.0.0.1:8080".to_string());
let response = client
.get_with_timeout("/json", Duration::from_secs(5))
.await
.unwrap();
let json: serde_json::Value = serde_json::from_str(&response).unwrap();
assert!(
json.get("slideshow").is_some(),
"JSON should contain slideshow"
);
}
#[tokio::test]
async fn test_client_configuration() {
let client = TestClient::new("http://example.com".to_string());
assert_eq!(client.base_url, "http://example.com");
}
}