tez 0.0.0

A lightweight, ergonomic, and fast high-level HTTP client for Rust
Documentation
use std::fmt;

use url::Url;

pub(crate) struct SanitizedUrl<'a>(pub(crate) &'a Url);

impl fmt::Debug for SanitizedUrl<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(sanitize_url(self.0).as_str(), formatter)
    }
}

pub(crate) fn sanitize_url(url: &Url) -> String {
    let mut url = url.clone();
    let _ = url.set_username("");
    let _ = url.set_password(None);
    url.set_query(None);
    url.set_fragment(None);
    url.to_string()
}

#[cfg(test)]
mod describe_sanitized_url {
    use super::*;

    #[test]
    fn it_removes_credentials_query_and_fragment() {
        let url = Url::parse("https://user:pass@example.com/path?token=secret#part").unwrap();

        assert_eq!(sanitize_url(&url), "https://example.com/path");
    }

    #[test]
    fn it_formats_the_sanitized_value() {
        let url = Url::parse("https://example.com/path?secret=value").unwrap();
        let debug = format!("{:?}", SanitizedUrl(&url));

        assert_eq!(debug, r#""https://example.com/path""#);
    }
}