Skip to content

Commit

Permalink
Merge pull request #80 from mstyura/api-url-double-slash
Browse files Browse the repository at this point in the history
Handle trailing slashes in provided URL.

This PR makes it so that the SDK handles both urls ending in a trailing slash (e.g. localhost:4242/api/) the same way as urls without a trailing slash.
  • Loading branch information
thomasheartman authored Mar 15, 2024
2 parents ebae633 + 8b9935f commit d5e236c
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 d5e236c

Please sign in to comment.