forked from apollographql/rover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.rs
42 lines (35 loc) · 1.24 KB
/
url.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use url::Url;
pub fn sanitize_url(url: &str) -> Option<String> {
Url::parse(url).ok().and_then(|mut parsed_url| {
if (parsed_url.username() != "" && parsed_url.set_username("").is_err())
|| parsed_url.set_password(None).is_err()
{
None
} else {
Some(parsed_url.to_string())
}
})
}
#[cfg(test)]
mod tests {
use super::*;
const UNAUTHENTICATED_URL: &str = "https://rover.apollo.dev/nix/latest";
const AUTHENTICATED_URL: &str = "https://username:password@customer.proxy/nix/latest";
const SANITIZED_AUTHENTICATED_URL: &str = "https://customer.proxy/nix/latest";
const INVALID_URL: &str = "not-a-url";
#[test]
fn it_leaves_unauthenticated_url_alone() {
let sanitized_url = sanitize_url(UNAUTHENTICATED_URL);
assert_eq!(sanitized_url, Some(UNAUTHENTICATED_URL.to_string()));
}
#[test]
fn it_sanitizes_authenticated_url() {
let sanitized_url = sanitize_url(AUTHENTICATED_URL);
assert_eq!(sanitized_url, Some(SANITIZED_AUTHENTICATED_URL.to_string()));
}
#[test]
fn it_returns_none_for_invalid_url() {
let sanitized_url = sanitize_url(INVALID_URL);
assert_eq!(sanitized_url, None);
}
}