pub fn url(path: &str) -> String {
join_url(&larust_core::config().app_url, path)
}
pub fn asset(path: &str) -> String {
url(path)
}
fn join_url(base: &str, path: &str) -> String {
format!(
"{}/{}",
base.trim_end_matches('/'),
path.trim_start_matches('/')
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn joins_a_leading_slash_path_with_exactly_one_slash() {
assert_eq!(
join_url("http://example.test", "/posts"),
"http://example.test/posts"
);
}
#[test]
fn joins_a_path_with_no_leading_slash_the_same_way() {
assert_eq!(
join_url("http://example.test", "posts"),
"http://example.test/posts"
);
}
#[test]
fn joins_a_base_with_a_trailing_slash_without_doubling_it() {
assert_eq!(
join_url("http://example.test/", "/posts"),
"http://example.test/posts"
);
}
#[test]
fn empty_path_still_produces_a_trailing_slash_on_the_base() {
assert_eq!(join_url("http://example.test", ""), "http://example.test/");
}
}