use url::Url;
pub type Error = url::ParseError;
pub fn normalize(input: &str) -> Result<String, Error> {
Url::parse(input)
.or_else(|_| Url::parse(format!("https://{input}").as_ref()))
.map(Into::into)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalize_identity() {
let tests = [
"https://google.com/",
"mailto:me@example.com",
"http://localhost/",
];
for url in tests {
assert_eq!(String::from(url), normalize(url).unwrap());
}
}
#[test]
fn normalize_host() {
let tests = [
("https://google.com/", "google.com"),
("https://iphone.local/", "iphone.local"),
("https://localhost/", "localhost"),
("https://google.com/", "https://GOOGLE.COM/"),
("http://www.google.com/", "http://WWW.GOogle.COM"),
("https://xn--4db.ws/", "https://א.ws"),
(
"https://test%40email.example@google.com/",
"test%40email.example@google.com",
),
];
for (want, inp) in tests {
assert_eq!(String::from(want), normalize(inp).unwrap());
}
}
}