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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
pub mod gandi;
use reqwest::ClientBuilder;
use std::error::Error;
use serde::{Deserialize, Serialize};
use crate::dyndns_service::gandi::Gandi;
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum DynDnsProvider {
GANDI(Gandi)
}
pub struct DynDnsService {}
impl DynDnsService {
pub async fn register(config: &impl DynDnsServiceConfiguration) -> Result<(), Box<dyn Error>> {
let client = ClientBuilder::new().build()?;
client.post(config.get_service_url()).send().await?;
Ok(())
}
}
pub trait DynDnsServiceConfiguration {
fn get_service_url(&self) -> String;
}
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
async fn setup_dyndns_service(service_path: &str) -> MockServer {
let service = MockServer::start().await;
Mock::given(method("POST"))
.and(path(service_path))
.respond_with(ResponseTemplate::new(200))
.mount(&service)
.await;
service
}
#[tokio::test]
async fn successful_dyndns_registration() -> Result<(), Box<dyn Error>> {
let service_path = "register-my-ip-address";
let dyndns_service = setup_dyndns_service(service_path).await;
let service_config = TestDynDnsServiceConfiguration::new(&dyndns_service, service_path);
DynDnsService::register(&service_config).await?;
assert_eq!(1, dyndns_service.received_requests().await.unwrap().len());
Ok(())
}
struct TestDynDnsServiceConfiguration {
service_path: String,
}
impl TestDynDnsServiceConfiguration {
fn new(server: &MockServer, path: &str) -> Self {
Self {
service_path: format!("{}/{}", server.uri(), path),
}
}
}
impl DynDnsServiceConfiguration for TestDynDnsServiceConfiguration {
fn get_service_url(&self) -> String {
self.service_path.clone()
}
}
}
|