2020-03-23 03:08:41 +01:00
|
|
|
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
|
|
|
|
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
|
|
|
|
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
|
|
|
|
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
|
|
|
|
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
|
|
|
|
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod passing {
|
|
|
|
use url::ParseError;
|
|
|
|
|
2020-06-28 07:36:41 +02:00
|
|
|
use crate::url;
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[test]
|
|
|
|
fn from_https_to_level_up_relative() -> Result<(), ParseError> {
|
|
|
|
let resolved_url =
|
2020-06-24 09:16:40 +02:00
|
|
|
url::resolve_url("https://www.kernel.org", "../category/signatures.html")?;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"https://www.kernel.org/category/signatures.html"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_just_filename_to_full_https_url() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"saved_page.htm",
|
|
|
|
"https://www.kernel.org/category/signatures.html",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"https://www.kernel.org/category/signatures.html"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_https_url_to_url_with_no_protocol() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"https://www.kernel.org",
|
|
|
|
"//www.kernel.org/theme/images/logos/tux.png",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"https://www.kernel.org/theme/images/logos/tux.png"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_https_url_to_url_with_no_protocol_and_on_different_hostname() -> Result<(), ParseError>
|
|
|
|
{
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"https://www.kernel.org",
|
|
|
|
"//another-host.org/theme/images/logos/tux.png",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"https://another-host.org/theme/images/logos/tux.png"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_https_url_to_relative_root_path() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"https://www.kernel.org/category/signatures.html",
|
|
|
|
"/theme/images/logos/tux.png",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"https://www.kernel.org/theme/images/logos/tux.png"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_https_to_just_filename() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"https://www.w3schools.com/html/html_iframe.asp",
|
|
|
|
"default.asp",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"https://www.w3schools.com/html/default.asp"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_data_url_to_https() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h",
|
|
|
|
"https://www.kernel.org/category/signatures.html",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"https://www.kernel.org/category/signatures.html"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_data_url_to_data_url() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h",
|
|
|
|
"data:text/html;base64,PGEgaHJlZj0iaW5kZXguaHRtbCI+SG9tZTwvYT4K",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"data:text/html;base64,PGEgaHJlZj0iaW5kZXguaHRtbCI+SG9tZTwvYT4K"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_file_url_to_relative_path() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"file:///home/user/Websites/my-website/index.html",
|
|
|
|
"assets/images/logo.png",
|
|
|
|
)
|
|
|
|
.unwrap_or(str!());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"file:///home/user/Websites/my-website/assets/images/logo.png"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_file_url_to_relative_path_with_backslashes() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"file:\\\\\\home\\user\\Websites\\my-website\\index.html",
|
|
|
|
"assets\\images\\logo.png",
|
|
|
|
)
|
|
|
|
.unwrap_or(str!());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"file:///home/user/Websites/my-website/assets/images/logo.png"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_data_url_to_file_url() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h",
|
|
|
|
"file:///etc/passwd",
|
|
|
|
)
|
|
|
|
.unwrap_or(str!());
|
|
|
|
|
|
|
|
assert_eq!(resolved_url.as_str(), "file:///etc/passwd");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn preserve_fragment() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"http://doesnt-matter.local/",
|
|
|
|
"css/fonts/fontmarvelous.svg#fontmarvelous",
|
|
|
|
)
|
|
|
|
.unwrap_or(str!());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
"http://doesnt-matter.local/css/fonts/fontmarvelous.svg#fontmarvelous"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_file_url_to_file_url() -> Result<(), ParseError> {
|
|
|
|
let resolved_url = if cfg!(windows) {
|
2020-06-24 09:16:40 +02:00
|
|
|
url::resolve_url("file:///c:/index.html", "file:///c:/image.png").unwrap_or(str!())
|
2020-03-29 09:54:20 +02:00
|
|
|
} else {
|
2020-06-24 09:16:40 +02:00
|
|
|
url::resolve_url("file:///tmp/index.html", "file:///tmp/image.png").unwrap_or(str!())
|
2020-05-23 09:49:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
resolved_url.as_str(),
|
|
|
|
if cfg!(windows) {
|
|
|
|
"file:///c:/image.png"
|
|
|
|
} else {
|
|
|
|
"file:///tmp/image.png"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-29 09:54:20 +02:00
|
|
|
}
|
|
|
|
|
2020-03-23 03:08:41 +01:00
|
|
|
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
|
|
|
|
// ██╔════╝██╔══██╗██║██║ ██║████╗ ██║██╔════╝
|
|
|
|
// █████╗ ███████║██║██║ ██║██╔██╗ ██║██║ ███╗
|
|
|
|
// ██╔══╝ ██╔══██║██║██║ ██║██║╚██╗██║██║ ██║
|
|
|
|
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
|
|
|
|
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod failing {
|
2020-06-24 09:16:40 +02:00
|
|
|
use crate::url;
|
2020-05-23 09:49:04 +02:00
|
|
|
use url::ParseError;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_data_url_to_url_with_no_protocol() -> Result<(), ParseError> {
|
2020-06-24 09:16:40 +02:00
|
|
|
let resolved_url = url::resolve_url(
|
2020-05-23 09:49:04 +02:00
|
|
|
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h",
|
|
|
|
"//www.w3schools.com/html/html_iframe.asp",
|
|
|
|
)
|
|
|
|
.unwrap_or(str!());
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
assert_eq!(resolved_url.as_str(), "");
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-23 03:08:41 +01:00
|
|
|
}
|