use apollo_configuration::configuration;
use apollo_configuration::parse_yaml;
use apollo_configuration::types::Url;
use serde::{Deserialize, Serialize};
#[configuration]
struct UrlConfig {
#[config(required)]
endpoint: Url,
}
#[test]
fn url_parses() {
let config: UrlConfig =
parse_yaml("endpoint: https://example.com/api", &Default::default()).unwrap();
assert_eq!(config.endpoint.host_str(), Some("example.com"));
assert_eq!(config.endpoint.path(), "/api");
}
#[test]
fn url_error_message() {
let result = parse_yaml::<UrlConfig>("endpoint: \"://badurl\"", &Default::default());
let err = result.expect_err("should fail to parse invalid URL");
assert_eq!(
format!("{err}"),
"failed to parse value: relative URL without a base"
);
}
#[derive(Debug, Deserialize, Serialize)]
struct SerializableUrlConfig {
url: Url,
}
#[test]
fn url_as_ref_str() {
let url: Url = "https://example.com/api".parse().unwrap();
assert_eq!(<Url as AsRef<str>>::as_ref(&url), "https://example.com/api");
}
#[test]
fn url_serializes() {
let config: SerializableUrlConfig =
serde_yaml::from_str("url: https://example.com/path").unwrap();
let yaml = serde_yaml::to_string(&config).unwrap();
insta::assert_snapshot!(yaml, @"url: https://example.com/path");
}