Skip to content

Commit

Permalink
Fix double slash in url.
Browse files Browse the repository at this point in the history
  • Loading branch information
mstyura committed Mar 14, 2024
1 parent ebae633 commit 8b9935f
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('/'))
}
}

Expand Down Expand Up @@ -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('/'))
}
}

Expand Down Expand Up @@ -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('/'))
}
}

Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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"
);
}
}

0 comments on commit 8b9935f

Please sign in to comment.