Skip to content

Commit

Permalink
api: add build_endpoint_url
Browse files Browse the repository at this point in the history
created new function to build endpoints urls,
to avoid errors with trailing and duplicate slashes
  • Loading branch information
Bernd Kaiser committed May 5, 2021
1 parent 88f6563 commit ec57e5a
Showing 1 changed file with 79 additions and 5 deletions.
84 changes: 79 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ pub enum Endpoint {
impl Endpoint {
pub fn as_str(&self) -> &str {
match self {
Endpoint::ApiKeyAccessSecret => "/api-key-access/secret/",
Endpoint::ApiKeyInspect => "/api-key-access/inspect/",
Endpoint::ApiKeyAccessSecret => "api-key-access/secret/",
Endpoint::ApiKeyInspect => "api-key-access/inspect/",
}
}
}
Expand Down Expand Up @@ -274,16 +274,32 @@ pub fn make_request(
Ok(vec)
}

fn build_endpoint_url(server_url: &Url, endpoint: &Endpoint) -> Result<Url> {
let mut endpoint_url = server_url.clone();

endpoint_url.set_path(&server_url.path().trim_end_matches('/'));

for segment in endpoint.as_str().split("/") {
endpoint_url
.path_segments_mut()
.map_err(|_| anyhow!("cannot create endpoint url from server_url and endpoint path"))?
.pop_if_empty()
.push(segment.trim_start_matches("/"));
}

Ok(endpoint_url)
}

fn call_route<T>(server_url: &Url, http_options: &HttpOptions, route: Route<T>) -> Result<Vec<u8>>
where
T: Serialize,
{
let url = format!("{}/{}", server_url, route.endpoint.as_str());
let url_parsed = Url::parse(&url).context("url parsing error")?;
let endpoint_url =
build_endpoint_url(server_url, &route.endpoint).context("building endpoint url failed")?;

let body = serde_json::to_string(&route.body)?;

let response_raw: Vec<u8> = make_request(http_options, url_parsed, route.method, Some(body))
let response_raw: Vec<u8> = make_request(http_options, endpoint_url, route.method, Some(body))
.context("make request failed")?;

Ok(response_raw)
Expand Down Expand Up @@ -964,6 +980,8 @@ pub fn api_key_get_secrets(config: &Config) -> Result<HashMap<Uuid, Secret>> {

#[cfg(test)]
mod tests {
use std::str::FromStr;

use lazy_static::lazy_static;

use super::*;
Expand Down Expand Up @@ -1057,6 +1075,62 @@ mod tests {
);
}

#[test]
#[allow(non_snake_case)]
fn build_url_simple_server_build_valid() {
let expected = Url::from_str("https://psono.pw/api-key-access/secret/").unwrap();

let server_url = Url::from_str("https://psono.pw").unwrap();

let endpoint_url = build_endpoint_url(&server_url, &Endpoint::ApiKeyAccessSecret);

assert!(endpoint_url.is_ok());

assert_eq!(endpoint_url.unwrap(), expected);
}

#[test]
#[allow(non_snake_case)]
fn build_url_no_trailing_slash_build_valid() {
let expected = Url::from_str("https://psono.pw/backend/api-key-access/secret/").unwrap();

let server_url = Url::from_str("https://psono.pw/backend").unwrap();

let endpoint_url = build_endpoint_url(&server_url, &Endpoint::ApiKeyAccessSecret);

assert!(endpoint_url.is_ok());

assert_eq!(endpoint_url.unwrap(), expected);
}

#[test]
#[allow(non_snake_case)]
fn build_url_one_trailing_slash_build_valid() {
let expected = Url::from_str("https://psono.pw/backend/api-key-access/secret/").unwrap();

let server_url = Url::from_str("https://psono.pw/backend/").unwrap();

let endpoint_url = build_endpoint_url(&server_url, &Endpoint::ApiKeyAccessSecret);

assert!(endpoint_url.is_ok());

assert_eq!(endpoint_url.unwrap(), expected);
}

#[test]
#[allow(non_snake_case)]
fn build_url_many_trailing_slashes_build_valid() {
let expected = Url::from_str("https://psono.pw/backend/api-key-access/secret/").unwrap();

let server_url = Url::from_str("https://psono.pw/backend//").unwrap();

let endpoint_url = build_endpoint_url(&server_url, &Endpoint::ApiKeyAccessSecret);

assert!(endpoint_url.is_ok());

assert_eq!(endpoint_url.unwrap(), expected);
}

#[test]
#[allow(non_snake_case)]
fn load_root_certificate__pem_success() {
Expand Down

0 comments on commit ec57e5a

Please sign in to comment.