1 use std::borrow::Cow;
2 use url::Url;
3 
4 /// Validates whether the string given is a url
5 #[must_use]
validate_url<'a, T>(val: T) -> bool where T: Into<Cow<'a, str>>,6 pub fn validate_url<'a, T>(val: T) -> bool
7 where
8     T: Into<Cow<'a, str>>,
9 {
10     Url::parse(val.into().as_ref()).is_ok()
11 }
12 
13 #[cfg(test)]
14 mod tests {
15     use std::borrow::Cow;
16 
17     use super::validate_url;
18 
19     #[test]
test_validate_url()20     fn test_validate_url() {
21         let tests = vec![
22             ("http", false),
23             ("https://google.com", true),
24             ("http://localhost:80", true),
25             ("ftp://localhost:80", true),
26         ];
27 
28         for (url, expected) in tests {
29             assert_eq!(validate_url(url), expected);
30         }
31     }
32 
33     #[test]
test_validate_url_cow()34     fn test_validate_url_cow() {
35         let test: Cow<'static, str> = "http://localhost:80".into();
36         assert_eq!(validate_url(test), true);
37         let test: Cow<'static, str> = String::from("http://localhost:80").into();
38         assert_eq!(validate_url(test), true);
39         let test: Cow<'static, str> = "http".into();
40         assert_eq!(validate_url(test), false);
41         let test: Cow<'static, str> = String::from("http").into();
42         assert_eq!(validate_url(test), false);
43     }
44 }
45