From 8b9935fe89c97be6de1733e75ea705119ab16f48 Mon Sep 17 00:00:00 2001 From: Yury Yarashevich Date: Wed, 13 Mar 2024 17:56:28 +0100 Subject: [PATCH] Fix double slash in url. --- src/api.rs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/api.rs b/src/api.rs index 8e065f7..8bc0ec1 100644 --- a/src/api.rs +++ b/src/api.rs @@ -15,7 +15,7 @@ pub struct Features { impl Features { pub fn endpoint(api_url: &str) -> String { - format!("{}/client/features", api_url) + format!("{}/client/features", api_url.trim_end_matches('/')) } } @@ -92,7 +92,7 @@ pub struct Registration { impl Registration { pub fn endpoint(api_url: &str) -> String { - format!("{}/client/register", api_url) + format!("{}/client/register", api_url.trim_end_matches('/')) } } @@ -120,7 +120,7 @@ pub struct Metrics { impl Metrics { pub fn endpoint(api_url: &str) -> String { - format!("{}/client/metrics", api_url) + format!("{}/client/metrics", api_url.trim_end_matches('/')) } } @@ -159,7 +159,8 @@ where #[cfg(test)] mod tests { - use super::Registration; + + use super::{Features, Metrics, Registration}; #[test] fn parse_reference_doc() -> Result<(), serde_json::Error> { @@ -289,4 +290,34 @@ mod tests { ..Default::default() }; } + + #[test] + fn test_endpoints_handle_trailing_slashes() { + assert_eq!( + Registration::endpoint("https://localhost:4242/api"), + "https://localhost:4242/api/client/register" + ); + assert_eq!( + Registration::endpoint("https://localhost:4242/api/"), + "https://localhost:4242/api/client/register" + ); + + assert_eq!( + Features::endpoint("https://localhost:4242/api"), + "https://localhost:4242/api/client/features" + ); + assert_eq!( + Features::endpoint("https://localhost:4242/api/"), + "https://localhost:4242/api/client/features" + ); + + assert_eq!( + Metrics::endpoint("https://localhost:4242/api"), + "https://localhost:4242/api/client/metrics" + ); + assert_eq!( + Metrics::endpoint("https://localhost:4242/api/"), + "https://localhost:4242/api/client/metrics" + ); + } }