aboutsummaryrefslogtreecommitdiff
path: root/src/test.rs
blob: f2c4219220b84aa755c92e856937189231567ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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()
    }
}