use std::time::Duration;
#[tokio::test]
async fn test_rate_limit_headers_present() {
let client = reqwest::Client::new();
let result = tokio::time::timeout(
Duration::from_secs(5),
client
.get("http://localhost:8580/v1/account")
.header("X-API-Key", "sk_test_key")
.send()
).await;
let response = match result {
Ok(Ok(resp)) => resp,
_ => {
println!("⚠️ Servidor não está rodando, pulando teste");
return;
}
};
if response.status() == 200 {
let headers = response.headers();
let has_rate_limit_header = headers.contains_key("x-ratelimit-limit")
|| headers.contains_key("X-RateLimit-Limit")
|| headers.contains_key("x-ratelimit-remaining")
|| headers.contains_key("X-RateLimit-Remaining");
println!("Rate limit headers presentes: {}", has_rate_limit_header);
}
}
#[tokio::test]
async fn test_multiple_requests() {
let client = reqwest::Client::new();
let mut success_count = 0;
let mut rate_limited_count = 0;
for i in 0..10 {
let result = tokio::time::timeout(
Duration::from_secs(5),
client
.get("http://localhost:8580/health")
.send()
).await;
match result {
Ok(Ok(resp)) => {
match resp.status() {
status if status.is_success() => success_count += 1,
status if status == 429 => rate_limited_count += 1,
_ => {}
}
}
_ => {
println!("⚠️ Servidor não está rodando, pulando teste");
return;
}
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
println!("Sucessos: {}, Rate limited: {}", success_count, rate_limited_count);
assert_eq!(success_count, 10, "Todas as requisições para /health devem ter sucesso");
}
#[tokio::test]
async fn test_rate_limit_response() {
println!("ℹ️ Este teste requer configuração específica de rate limit");
println!(" Pulando teste de rate limit 429");
}
#[tokio::test]
async fn test_health_not_rate_limited() {
let client = reqwest::Client::new();
for _ in 0..20 {
let result = tokio::time::timeout(
Duration::from_secs(5),
client.get("http://localhost:8580/health").send()
).await;
let response = match result {
Ok(Ok(resp)) => resp,
_ => {
println!("⚠️ Servidor não está rodando, pulando teste");
return;
}
};
assert_ne!(
response.status(),
429,
"Health check não deve ser rate limited"
);
assert_eq!(response.status(), 200);
}
}
#[tokio::test]
async fn test_rate_limit_info_in_usage() {
let client = reqwest::Client::new();
let result = tokio::time::timeout(
Duration::from_secs(5),
client
.get("http://localhost:8580/v1/account/usage")
.header("X-API-Key", "sk_test_key")
.send()
).await;
let response = match result {
Ok(Ok(resp)) => resp,
_ => {
println!("⚠️ Servidor não está rodando, pulando teste");
return;
}
};
if response.status() == 200 {
let body: serde_json::Value = response.json().await.unwrap();
if let Some(data) = body.get("data") {
let has_rate_limit_info = data.get("rate_limit_remaining").is_some()
|| data.get("rate_limit").is_some();
println!("Info de rate limit presente: {}", has_rate_limit_info);
}
}
}