use super::*;
use std::time::Duration;
#[test]
fn test_http_methods() {
assert_eq!(Method::GET.as_str(), "GET");
assert_eq!(Method::POST.as_str(), "POST");
assert_eq!(Method::PUT.as_str(), "PUT");
assert_eq!(Method::DELETE.as_str(), "DELETE");
assert_eq!(Method::HEAD.as_str(), "HEAD");
assert_eq!(Method::OPTIONS.as_str(), "OPTIONS");
assert_eq!(Method::PATCH.as_str(), "PATCH");
}
#[test]
fn test_url_parsing() {
let test_cases = [
("http://example.com", "/", None),
("http://example.com/", "/", None),
("http://example.com/path", "/path", None),
("http://example.com/path?query=1", "/path", Some("query=1")),
];
for (url_str, expected_path, expected_query) in test_cases {
let url = url::Url::parse(url_str).unwrap();
assert_eq!(url.path(), expected_path);
assert_eq!(url.query(), expected_query);
}
}
#[test]
fn test_invalid_url() {
let client = default();
let result = client.get("not_a_valid_url").send();
assert!(result.is_err());
assert!(result
.unwrap_err()
.to_string()
.contains("relative URL without a base"));
}
#[test]
fn test_url_port() {
let urls = [
("http://example.com", 80),
("http://example.com:8080", 8080),
("https://example.com", 443),
("https://example.com:8443", 8443),
];
for (url_str, expected_port) in urls {
let url = url::Url::parse(url_str).unwrap();
let port = url
.port()
.unwrap_or(if url.scheme() == "https" { 443 } else { 80 });
assert_eq!(port, expected_port, "URL: {}", url_str);
}
}
#[test]
fn test_request_builder() {
let client = Client::default();
let builder = client
.get("http://httpbin.org/get")
.header("X-Custom-Header", "custom-value")
.header("Accept", "application/json");
assert_eq!(builder.method.as_str(), "GET");
assert_eq!(builder.url, "http://httpbin.org/get");
assert_eq!(
builder.headers.get("X-Custom-Header").unwrap(),
"custom-value"
);
assert_eq!(builder.headers.get("Accept").unwrap(), "application/json");
let body = b"test-body".to_vec();
let builder = client
.post("http://httpbin.org/post")
.header("Content-Type", "application/json")
.body(body.clone());
assert_eq!(builder.method.as_str(), "POST");
assert_eq!(builder.url, "http://httpbin.org/post");
assert_eq!(
builder.headers.get("Content-Type").unwrap(),
"application/json"
);
assert_eq!(builder.body.unwrap(), body);
}
#[test]
fn test_custom_config() {
let config = ClientConfig {
timeout: Duration::from_secs(5),
user_agent: "Custom Agent".to_string(),
max_retries: 2,
retry_interval: Duration::from_millis(500),
allow_redirects: true,
max_redirects: 5,
verify_ssl: false,
..Default::default()
};
let client = Client::new(config.clone());
assert_eq!(client.config.timeout, Duration::from_secs(5));
assert_eq!(client.config.max_retries, 2);
assert_eq!(client.config.user_agent, "Custom Agent");
assert_eq!(client.config.retry_interval, Duration::from_millis(500));
assert_eq!(client.config.allow_redirects, true);
assert_eq!(client.config.max_redirects, 5);
assert_eq!(client.config.verify_ssl, false);
}
#[test]
fn test_body_formats() {
assert_eq!(BodyFormat::Json.content_type(), "application/json");
assert_eq!(
BodyFormat::FormUrlEncoded.content_type(),
"application/x-www-form-urlencoded"
);
assert_eq!(BodyFormat::Text.content_type(), "text/plain");
let boundary = "boundary123";
assert_eq!(
BodyFormat::FormData(boundary.to_string()).content_type(),
format!("multipart/form-data; boundary={}", boundary)
);
assert_eq!(
BodyFormat::Custom("application/xml".to_string()).content_type(),
"application/xml"
);
}
#[test]
fn test_all_http_methods() {
let client = default();
let url = "http://example.com";
let methods = [
(client.get(url), "GET"),
(client.post(url), "POST"),
(client.put(url), "PUT"),
(client.delete(url), "DELETE"),
(client.head(url), "HEAD"),
(client.options(url), "OPTIONS"),
(client.patch(url), "PATCH"),
];
for (builder, expected_method) in methods {
assert_eq!(builder.method.as_str(), expected_method);
assert_eq!(builder.url, url);
}
}
#[test]
fn test_request_builder_options() {
let client = default();
let builder = client
.post("http://example.com")
.header("X-Custom", "value")
.body("data".as_bytes().to_vec())
.body_format(BodyFormat::Json)
.timeout(Duration::from_secs(5))
.retries(2);
assert_eq!(builder.method.as_str(), "POST");
assert_eq!(builder.headers.get("X-Custom").unwrap(), "value");
assert_eq!(builder.body.as_ref().unwrap(), b"data");
assert!(matches!(builder.body_format.unwrap(), BodyFormat::Json));
assert_eq!(builder.timeout.unwrap(), Duration::from_secs(5));
assert_eq!(builder.retries.unwrap(), 2);
}
#[test]
#[ignore]
fn test_http_requests() {
let client = default();
let response = client
.get("http://httpbin.org/get")
.header("X-Test-Header", "test-value")
.timeout(Duration::from_secs(5))
.retries(2)
.send()
.unwrap();
assert_eq!(response.status, 200);
println!("GET Response: {}", String::from_utf8_lossy(&response.body));
let json_data = r#"{"key": "value"}"#;
let response = client
.post("http://httpbin.org/post")
.body(json_data.as_bytes().to_vec())
.body_format(BodyFormat::Json)
.send()
.unwrap();
assert_eq!(response.status, 200);
println!("POST Response: {}", String::from_utf8_lossy(&response.body));
let response = client
.put("http://httpbin.org/put")
.body(b"test data".to_vec())
.body_format(BodyFormat::Text)
.send()
.unwrap();
assert_eq!(response.status, 200);
println!("PUT Response: {}", String::from_utf8_lossy(&response.body));
let response = client.delete("http://httpbin.org/delete").send().unwrap();
assert_eq!(response.status, 200);
println!(
"DELETE Response: {}",
String::from_utf8_lossy(&response.body)
);
}
#[test]
#[ignore]
fn test_redirects() {
let client = default();
let response = client
.get("http://httpbin.org/redirect/1")
.follow_redirects(true) .send()
.unwrap();
assert_eq!(response.status, 200);
let body = String::from_utf8_lossy(&response.body);
assert!(body.contains("httpbin.org/get"));
let response = client
.get("http://httpbin.org/redirect/1")
.follow_redirects(false) .send()
.unwrap();
assert_eq!(response.status, 302);
assert!(response.header("Location").is_some());
}
#[test]
#[ignore]
fn test_save_to_file() {
let client = default();
let response = client.get("http://httpbin.org/get").send().unwrap();
let file = response.save_to_file("test.json").unwrap();
assert_eq!(file.exists(), true);
crate::zfile::remove("test.json", false).unwrap();
}