-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move request_url to separate file and add tests
- Loading branch information
1 parent
4b40128
commit fbbadae
Showing
3 changed files
with
83 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod config; | ||
mod node_service; | ||
mod request_url; | ||
|
||
use hyper::server::conn::http1; | ||
use hyper_util::rt::TokioIo; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::{collections::HashMap, str::FromStr}; | ||
|
||
use hyper::Uri; | ||
|
||
use crate::config::Url; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct RequestUrl { | ||
pub uri: Uri, | ||
pub params: HashMap<String, String>, | ||
} | ||
|
||
impl RequestUrl { | ||
pub fn from_uri( | ||
url: Url, | ||
urls_override: HashMap<String, Url>, | ||
original_uri: &Uri, | ||
) -> RequestUrl { | ||
let uri = url.url + original_uri.to_string().as_str(); | ||
let uri = uri.parse::<hyper::Uri>().expect("invalid url"); | ||
|
||
// first order is url and the global | ||
let urls_override = vec![url.urls_override.unwrap_or_default(), urls_override]; | ||
|
||
for override_url in urls_override { | ||
for (path, endpoint) in override_url { | ||
if uri.path() == path { | ||
let uri = Uri::from_str(&endpoint.url.clone().as_str()).unwrap(); | ||
return RequestUrl { | ||
uri, | ||
params: endpoint.headers.unwrap_or_default(), | ||
}; | ||
} | ||
} | ||
} | ||
|
||
RequestUrl { | ||
uri, | ||
params: url.headers.unwrap_or_default(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::{collections::HashMap, str::FromStr}; | ||
|
||
#[test] | ||
fn test_from_uri() { | ||
let url = Url { | ||
url: "https://example.com".to_string(), | ||
headers: Some(HashMap::new()), | ||
urls_override: None, | ||
}; | ||
let original_uri = Uri::from_str("/path").unwrap(); | ||
let request_url = RequestUrl::from_uri(url.clone(), HashMap::new(), &original_uri); | ||
assert_eq!(request_url.uri.to_string(), "https://example.com/path"); | ||
assert!(request_url.params.is_empty()); | ||
|
||
let mut urls_override = HashMap::new(); | ||
urls_override.insert( | ||
"/path".to_string(), | ||
Url { | ||
url: "https://override.com/".to_string(), | ||
headers: Some({ | ||
let mut params = HashMap::new(); | ||
params.insert("key".to_string(), "value".to_string()); | ||
params | ||
}), | ||
urls_override: None, | ||
}, | ||
); | ||
let request_url = RequestUrl::from_uri(url, urls_override, &original_uri); | ||
assert_eq!(request_url.uri.to_string(), "https://override.com/"); | ||
assert_eq!(*request_url.params.get("key").unwrap(), "value".to_string()); | ||
} | ||
} |