use std::net::{IpAddr, Ipv4Addr};
use manydns::cloudflare::CloudflareProvider;
use manydns::HttpClientConfig;
use wiremock::matchers::{header, method, path_regex};
use wiremock::{Mock, MockServer, ResponseTemplate};
fn mock_zones_response() -> serde_json::Value {
serde_json::json!({
"success": true,
"errors": [],
"messages": [],
"result": [{
"id": "test-zone-id",
"name": "example.com",
"status": "active",
"paused": false,
"type": "full"
}],
"result_info": {
"page": 1,
"per_page": 100,
"total_pages": 1,
"count": 1,
"total_count": 1
}
})
}
#[tokio::test]
async fn test_local_address_binding_to_localhost_works() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path_regex(r"^/zones$"))
.and(header("Authorization", "Bearer test-token"))
.respond_with(ResponseTemplate::new(200).set_body_json(mock_zones_response()))
.mount(&server)
.await;
let config = HttpClientConfig::new().local_address(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
assert_eq!(config.local_address, Some(IpAddr::V4(Ipv4Addr::LOCALHOST)));
let result = CloudflareProvider::with_config("test-token", config);
assert!(
result.is_ok(),
"Provider creation with localhost binding should succeed"
);
}
#[tokio::test]
async fn test_local_address_binding_to_unavailable_ip_fails_on_connect() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path_regex(r"^/zones$"))
.and(header("Authorization", "Bearer test-token"))
.respond_with(ResponseTemplate::new(200).set_body_json(mock_zones_response()))
.mount(&server)
.await;
let config = HttpClientConfig::new().local_address(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)));
let provider = manydns::cloudflare::api::Client::with_config("test-token", config);
assert!(
provider.is_ok(),
"Client creation should succeed even with unavailable IP"
);
}
#[tokio::test]
async fn test_timeout_configuration() {
let config = HttpClientConfig::new().timeout(std::time::Duration::from_secs(5));
assert_eq!(config.timeout, Some(std::time::Duration::from_secs(5)));
let result = CloudflareProvider::with_config("test-token", config);
assert!(result.is_ok());
}
#[tokio::test]
async fn test_combined_config_options() {
let config = HttpClientConfig::new()
.local_address(IpAddr::V4(Ipv4Addr::LOCALHOST))
.timeout(std::time::Duration::from_secs(30));
assert_eq!(config.local_address, Some(IpAddr::V4(Ipv4Addr::LOCALHOST)));
assert_eq!(config.timeout, Some(std::time::Duration::from_secs(30)));
let result = CloudflareProvider::with_config("test-token", config);
assert!(result.is_ok());
}
#[cfg(any(
target_os = "linux",
target_os = "macos",
target_os = "android",
target_os = "fuchsia",
))]
#[tokio::test]
async fn test_interface_binding_config() {
let config = HttpClientConfig::new().interface("lo");
assert_eq!(config.interface, Some("lo".to_string()));
let result = CloudflareProvider::with_config("test-token", config);
assert!(result.is_ok());
}