use std::net::{IpAddr, Ipv4Addr}; use reqwest::Url; use wiremock::{Mock, MockServer, ResponseTemplate}; use wiremock::matchers::{method, path}; use crate::{IpService, IpServiceConfiguration}; async fn setup_ipv4_service(service_path: &str, response: &str) -> MockServer { let ip_service = MockServer::start().await; Mock::given(method("GET")) .and(path(service_path)) .respond_with(ResponseTemplate::new(200) .insert_header("Content-Type", "text/plain; charset=utf-8") .set_body_string(response)) .mount(&ip_service) .await; ip_service } #[tokio::test] async fn successful_ipv4_address_resolution() { let service_path = "get-my-ip-address"; let service_response = "17.5.7.8"; let ip_service = setup_ipv4_service(service_path, service_response).await; let service_config = TestIpServiceConfiguration::new(&ip_service, service_path); let actual = IpService::resolve(&service_config).await.unwrap(); assert_eq!(actual, IpAddr::V4(Ipv4Addr::new(17, 5, 7, 8))) } struct TestIpServiceConfiguration { service_url: Url, } impl TestIpServiceConfiguration { fn new(server: &MockServer, path: &str) -> Self { Self { service_url: Url::parse(format!("{}/{}", server.uri(), path).as_str()).unwrap() } } } impl IpServiceConfiguration for TestIpServiceConfiguration { fn get_service_url(&self) -> Url { self.service_url.clone() } }