2020-03-23 03:08:41 +01:00
|
|
|
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
|
|
|
|
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
|
|
|
|
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
|
|
|
|
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
|
|
|
|
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
|
|
|
|
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod passing {
|
|
|
|
use reqwest::blocking::Client;
|
2021-03-11 23:44:02 +01:00
|
|
|
use reqwest::Url;
|
2020-05-23 09:49:04 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::env;
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2021-10-17 09:16:37 +02:00
|
|
|
use monolith::opts::Options;
|
|
|
|
use monolith::url;
|
|
|
|
use monolith::utils;
|
2020-06-24 09:16:40 +02:00
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[test]
|
|
|
|
fn read_data_url() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-11-23 03:49:26 +01:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
// If both source and target are data URLs,
|
2021-03-11 23:44:02 +01:00
|
|
|
// ensure the result contains target data URL
|
2021-06-08 14:30:15 +02:00
|
|
|
let (data, final_url, media_type, charset) = utils::retrieve_asset(
|
2020-05-23 09:49:04 +02:00
|
|
|
cache,
|
|
|
|
&client,
|
2021-03-11 23:44:02 +01:00
|
|
|
&Url::parse("data:text/html;base64,c291cmNl").unwrap(),
|
|
|
|
&Url::parse("data:text/html;base64,dGFyZ2V0").unwrap(),
|
2020-11-23 03:49:26 +01:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-05-23 09:49:04 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-06-08 14:30:15 +02:00
|
|
|
assert_eq!(&media_type, "text/html");
|
|
|
|
assert_eq!(&charset, "US-ASCII");
|
2020-05-23 09:49:04 +02:00
|
|
|
assert_eq!(
|
2021-06-09 00:54:16 +02:00
|
|
|
url::create_data_url(&media_type, &charset, &data, &final_url),
|
2021-06-08 14:30:15 +02:00
|
|
|
Url::parse("data:text/html;base64,dGFyZ2V0").unwrap(),
|
2020-05-23 09:49:04 +02:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
final_url,
|
2021-06-08 14:30:15 +02:00
|
|
|
Url::parse("data:text/html;base64,dGFyZ2V0").unwrap(),
|
2020-05-23 09:49:04 +02:00
|
|
|
);
|
|
|
|
}
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[test]
|
|
|
|
fn read_local_file_with_file_url_parent() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2020-11-23 03:49:26 +01:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
let file_url_protocol: &str = if cfg!(windows) { "file:///" } else { "file://" };
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
// Inclusion of local assets from local sources should be allowed
|
|
|
|
let cwd = env::current_dir().unwrap();
|
2021-06-08 14:30:15 +02:00
|
|
|
let (data, final_url, media_type, charset) = utils::retrieve_asset(
|
2020-05-23 09:49:04 +02:00
|
|
|
cache,
|
|
|
|
&client,
|
2021-03-11 23:44:02 +01:00
|
|
|
&Url::parse(&format!(
|
2021-10-18 10:46:06 +02:00
|
|
|
"{file}{cwd}/tests/_data_/basic/local-file.html",
|
2020-05-23 09:49:04 +02:00
|
|
|
file = file_url_protocol,
|
|
|
|
cwd = cwd.to_str().unwrap()
|
2021-03-11 23:44:02 +01:00
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
&Url::parse(&format!(
|
2021-10-18 10:46:06 +02:00
|
|
|
"{file}{cwd}/tests/_data_/basic/local-script.js",
|
2020-05-23 09:49:04 +02:00
|
|
|
file = file_url_protocol,
|
|
|
|
cwd = cwd.to_str().unwrap()
|
2021-03-11 23:44:02 +01:00
|
|
|
))
|
|
|
|
.unwrap(),
|
2020-11-23 03:49:26 +01:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-03-23 03:08:41 +01:00
|
|
|
)
|
2020-05-23 09:49:04 +02:00
|
|
|
.unwrap();
|
2021-06-08 14:30:15 +02:00
|
|
|
assert_eq!(&media_type, "application/javascript");
|
|
|
|
assert_eq!(&charset, "");
|
2021-06-09 00:54:16 +02:00
|
|
|
assert_eq!(url::create_data_url(&media_type, &charset, &data, &final_url), Url::parse("data:application/javascript;base64,ZG9jdW1lbnQuYm9keS5zdHlsZS5iYWNrZ3JvdW5kQ29sb3IgPSAiZ3JlZW4iOwpkb2N1bWVudC5ib2R5LnN0eWxlLmNvbG9yID0gInJlZCI7Cg==").unwrap());
|
2020-05-23 09:49:04 +02:00
|
|
|
assert_eq!(
|
2021-03-11 23:44:02 +01:00
|
|
|
final_url,
|
|
|
|
Url::parse(&format!(
|
2021-10-18 10:46:06 +02:00
|
|
|
"{file}{cwd}/tests/_data_/basic/local-script.js",
|
2020-05-23 09:49:04 +02:00
|
|
|
file = file_url_protocol,
|
|
|
|
cwd = cwd.to_str().unwrap()
|
2021-03-11 23:44:02 +01:00
|
|
|
))
|
|
|
|
.unwrap()
|
2020-05-23 09:49:04 +02:00
|
|
|
);
|
|
|
|
}
|
2020-03-23 03:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
|
|
|
|
// ██╔════╝██╔══██╗██║██║ ██║████╗ ██║██╔════╝
|
|
|
|
// █████╗ ███████║██║██║ ██║██╔██╗ ██║██║ ███╗
|
|
|
|
// ██╔══╝ ██╔══██║██║██║ ██║██║╚██╗██║██║ ██║
|
|
|
|
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
|
|
|
|
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod failing {
|
|
|
|
use reqwest::blocking::Client;
|
2021-03-11 23:44:02 +01:00
|
|
|
use reqwest::Url;
|
2020-05-23 09:49:04 +02:00
|
|
|
use std::collections::HashMap;
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2021-10-17 09:16:37 +02:00
|
|
|
use monolith::opts::Options;
|
|
|
|
use monolith::utils;
|
2020-06-28 07:36:41 +02:00
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[test]
|
|
|
|
fn read_local_file_with_data_url_parent() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-11-23 03:49:26 +01:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
// Inclusion of local assets from data URL sources should not be allowed
|
|
|
|
match utils::retrieve_asset(
|
|
|
|
cache,
|
|
|
|
&client,
|
2021-03-11 23:44:02 +01:00
|
|
|
&Url::parse("data:text/html;base64,SoUrCe").unwrap(),
|
|
|
|
&Url::parse("file:///etc/passwd").unwrap(),
|
2020-11-23 03:49:26 +01:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-05-23 09:49:04 +02:00
|
|
|
) {
|
|
|
|
Ok((..)) => {
|
|
|
|
assert!(false);
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
assert!(true);
|
|
|
|
}
|
2020-05-02 12:13:28 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[test]
|
|
|
|
fn read_local_file_with_https_parent() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2020-11-23 03:49:26 +01:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
// Inclusion of local assets from remote sources should not be allowed
|
|
|
|
match utils::retrieve_asset(
|
|
|
|
cache,
|
|
|
|
&client,
|
2021-03-11 23:44:02 +01:00
|
|
|
&Url::parse("https://kernel.org/").unwrap(),
|
|
|
|
&Url::parse("file:///etc/passwd").unwrap(),
|
2020-11-23 03:49:26 +01:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-05-23 09:49:04 +02:00
|
|
|
) {
|
|
|
|
Ok((..)) => {
|
|
|
|
assert!(false);
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
assert!(true);
|
|
|
|
}
|
2020-05-02 12:13:28 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 03:08:41 +01:00
|
|
|
}
|