use serde::{Deserialize, Serialize};
use url::{Host, Url};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HttpServerSettings {
pub host: String,
pub port: u16,
}
impl HttpServerSettings {
pub fn address(&self) -> String {
format!("{}:{}", self.host, self.port)
}
pub fn url_host(&self) -> Result<Host, url::ParseError> {
Host::parse(self.host.as_str())
}
pub fn url(&self, scheme: impl Into<String>) -> Result<Url, url::ParseError> {
let url_rep = format!("{}://{}:{}", scheme.into(), self.host, self.port);
Url::parse(url_rep.as_str())
}
}
#[cfg(test)]
mod tests {
use assert_matches2::assert_let;
use pretty_assertions::assert_eq;
use trim_margin::MarginTrimmable;
use super::*;
#[test]
fn test_url_host_invalid() {
let settings = HttpServerSettings { host: "invalid host".to_string(), port: 80 };
assert!(settings.url_host().is_err());
}
#[test]
fn test_url_invalid() {
let settings = HttpServerSettings { host: "localhost".to_string(), port: 80 };
assert!(settings.url("http").is_ok());
let settings_bad = HttpServerSettings { host: "###".to_string(), port: 80 };
assert!(settings_bad.url("http").is_err());
}
#[test]
fn test_address() {
let settings = HttpServerSettings { host: "localhost".to_string(), port: 8080 };
assert_eq!(settings.address(), "localhost:8080");
}
#[test]
fn test_url_host() {
let local = HttpServerSettings { host: "127.0.0.1".to_string(), port: 80 };
assert_let!(Ok(actual) = local.url_host());
assert_let!(Ok(expected) = Host::parse("127.0.0.1"));
assert_eq!(actual, expected);
let example = HttpServerSettings { host: "example.com".to_string(), port: 8080 };
assert_let!(Ok(actual) = example.url_host());
assert_let!(Ok(expected) = Host::parse("example.com"));
assert_eq!(actual, expected);
let dns = HttpServerSettings { host: "job_manager".to_string(), port: 8080 };
assert_let!(Ok(actual) = dns.url_host());
assert_let!(Ok(expected) = Host::parse("job_manager"));
assert_eq!(actual, expected);
}
#[test]
fn test_http_settings_ser() {
let settings = HttpServerSettings { host: "example.com".to_string(), port: 80 };
assert_let!(Ok(yaml) = serde_yaml::to_string(&settings));
assert_eq!(
yaml,
r##"|host: example.com
|port: 80
|"##
.trim_margin()
.unwrap()
);
assert_let!(Ok(json) = serde_json::to_string(&settings));
assert_eq!(json, r##"{"host":"example.com","port":80}"##.trim_margin().unwrap());
assert_let!(Ok(from_yaml) = serde_yaml::from_str::<HttpServerSettings>(yaml.as_str()));
assert_eq!(from_yaml, settings);
assert_let!(Ok(from_json) = serde_json::from_str::<HttpServerSettings>(json.as_str()));
assert_eq!(from_json, settings);
}
#[test]
fn test_url() {
let local = HttpServerSettings { host: "127.0.0.1".to_string(), port: 80 };
assert_let!(Ok(actual) = local.url("https"));
assert_let!(Ok(expected) = Url::parse("https://127.0.0.1:80"));
assert_eq!(actual, expected);
let example = HttpServerSettings { host: "example.com".to_string(), port: 8080 };
assert_let!(Ok(actual) = example.url("http"));
assert_let!(Ok(expected) = Url::parse("http://example.com:8080"));
assert_eq!(actual, expected);
let dns = HttpServerSettings { host: "job_manager".to_string(), port: 8888 };
assert_let!(Ok(actual) = dns.url("https"));
assert_let!(Ok(expected) = Url::parse("https://job_manager:8888"));
assert_eq!(actual, expected);
}
}