diff options
| author | Gus Power <gus@infinitesidequests.com> | 2025-05-12 10:52:28 +0100 |
|---|---|---|
| committer | Gus Power <gus@infinitesidequests.com> | 2025-05-12 10:52:28 +0100 |
| commit | 1d6fd8359fddbae8aeac17b90ed50d41143ffbb6 (patch) | |
| tree | 7872e1e112ea9ed440caecad9755da751de59ccd /src/dyndns_service.rs | |
| parent | e1f43bb48b23338ce103dc92a53a13e72021299d (diff) | |
skeleton dyndns service, split into ip and dyndns modules
Diffstat (limited to 'src/dyndns_service.rs')
| -rw-r--r-- | src/dyndns_service.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/dyndns_service.rs b/src/dyndns_service.rs new file mode 100644 index 0000000..caa163d --- /dev/null +++ b/src/dyndns_service.rs @@ -0,0 +1,67 @@ +use reqwest::ClientBuilder; +use std::error::Error; + +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(()) + } +} + +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() + } + } +} |
