Fix incorrect url formatting, add more tests

This commit is contained in:
bitfl0wer 2023-04-15 13:27:34 +02:00
parent 13a2b2daab
commit 57a1df35e6
1 changed files with 12 additions and 8 deletions

View File

@ -5,7 +5,7 @@ mod limit;
mod voice; mod voice;
use url::{ParseError, Url}; use url::{ParseError, Url};
#[derive(Clone, Default)] #[derive(Clone, Default, Debug)]
/// A URLBundle is a struct which bundles together the API-, Gateway- and CDN-URLs of a Spacebar /// A URLBundle is a struct which bundles together the API-, Gateway- and CDN-URLs of a Spacebar
/// instance. /// instance.
@ -36,13 +36,13 @@ impl URLBundle {
let url = match Url::parse(&url) { let url = match Url::parse(&url) {
Ok(url) => { Ok(url) => {
if url.scheme() == "localhost" { if url.scheme() == "localhost" {
return format!("http://{}", url); return URLBundle::parse_url(format!("http://{}", url));
} }
url url
} }
Err(ParseError::RelativeUrlWithoutBase) => { Err(ParseError::RelativeUrlWithoutBase) => {
let url = format!("http://{}", url); let url_fmt = format!("http://{}", url);
Url::parse(&url).unwrap() return URLBundle::parse_url(url_fmt);
} }
Err(_) => panic!("Invalid URL"), Err(_) => panic!("Invalid URL"),
}; };
@ -68,14 +68,18 @@ impl URLBundle {
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod lib {
use super::*; use super::*;
#[test] #[test]
fn test_parse_url() { fn test_parse_url() {
let mut result = URLBundle::parse_url(String::from("localhost:3000")); let mut result = URLBundle::parse_url(String::from("localhost:3000/"));
assert_eq!(result, String::from("http://localhost:3000")); assert_eq!(result, String::from("http://localhost:3000"));
result = URLBundle::parse_url(String::from("some.url.com/")); result = URLBundle::parse_url(String::from("https://some.url.com/"));
assert_eq!(result, String::from("http://some.url.com")) assert_eq!(result, String::from("https://some.url.com"));
result = URLBundle::parse_url(String::from("https://some.url.com/"));
assert_eq!(result, String::from("https://some.url.com"));
result = URLBundle::parse_url(String::from("https://some.url.com"));
assert_eq!(result, String::from("https://some.url.com"));
} }
} }