pub fn ensure_no_trailing_slash<S>(url: S) -> String
where
S: Into<String>,
{
let url = url.into();
if url.ends_with('/') {
url[..url.len() - 1].to_string()
} else {
url
}
}
pub fn ensure_trailing_slash<S>(url: S) -> String
where
S: Into<String>,
{
let url = url.into();
if url.ends_with('/') {
ensure_no_trailing_slash(url) + "/"
} else {
url + "/"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ensure_no_trailing_slash() {
let test_cases = vec![
("http://example.com/", "http://example.com"),
("http://example.com", "http://example.com"),
];
for (input, expected) in test_cases {
assert_eq!(ensure_no_trailing_slash(input), expected);
assert_eq!(ensure_no_trailing_slash(input.to_string()), expected);
}
}
#[test]
fn test_ensure_trailing_slash() {
let test_cases = vec![
("http://example.com", "http://example.com/"),
("http://example.com/", "http://example.com/"),
];
for (input, expected) in test_cases {
assert_eq!(ensure_trailing_slash(input), expected);
assert_eq!(ensure_trailing_slash(input.to_string()), expected);
}
}
}