2020-03-23 03:08:41 +01:00
|
|
|
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
|
|
|
|
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
|
|
|
|
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
|
|
|
|
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
|
|
|
|
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
|
|
|
|
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod passing {
|
2021-03-11 23:44:02 +01:00
|
|
|
use reqwest::Url;
|
|
|
|
|
2021-10-17 09:16:37 +02:00
|
|
|
use monolith::url;
|
2020-03-23 03:08:41 +01:00
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[test]
|
|
|
|
fn encode_string_with_specific_media_type() {
|
2021-06-09 00:54:16 +02:00
|
|
|
let media_type = "application/javascript";
|
2020-05-23 09:49:04 +02:00
|
|
|
let data = "var word = 'hello';\nalert(word);\n";
|
2021-06-09 00:54:16 +02:00
|
|
|
let data_url = url::create_data_url(
|
|
|
|
media_type,
|
|
|
|
"",
|
|
|
|
data.as_bytes(),
|
|
|
|
&Url::parse("data:,").unwrap(),
|
|
|
|
);
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2021-03-11 23:44:02 +01:00
|
|
|
data_url.as_str(),
|
2020-05-23 09:49:04 +02:00
|
|
|
"data:application/javascript;base64,dmFyIHdvcmQgPSAnaGVsbG8nOwphbGVydCh3b3JkKTsK"
|
|
|
|
);
|
|
|
|
}
|
2020-03-29 09:54:20 +02:00
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[test]
|
|
|
|
fn encode_append_fragment() {
|
|
|
|
let data = "<svg></svg>\n";
|
2021-03-15 06:42:36 +01:00
|
|
|
let data_url = url::create_data_url(
|
2021-03-11 23:44:02 +01:00
|
|
|
"image/svg+xml",
|
2021-06-09 00:54:16 +02:00
|
|
|
"",
|
2021-03-11 23:44:02 +01:00
|
|
|
data.as_bytes(),
|
|
|
|
&Url::parse("data:,").unwrap(),
|
|
|
|
);
|
2020-03-29 09:54:20 +02:00
|
|
|
|
2021-03-11 23:44:02 +01:00
|
|
|
assert_eq!(
|
|
|
|
data_url.as_str(),
|
|
|
|
"data:image/svg+xml;base64,PHN2Zz48L3N2Zz4K"
|
|
|
|
);
|
2020-05-23 09:49:04 +02:00
|
|
|
}
|
2021-06-09 00:54:16 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_string_with_specific_media_type_and_charset() {
|
|
|
|
let media_type = "application/javascript";
|
|
|
|
let charset = "utf8";
|
|
|
|
let data = "var word = 'hello';\nalert(word);\n";
|
|
|
|
let data_url = url::create_data_url(
|
|
|
|
media_type,
|
|
|
|
charset,
|
|
|
|
data.as_bytes(),
|
|
|
|
&Url::parse("data:,").unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
data_url.as_str(),
|
|
|
|
"data:application/javascript;charset=utf8;base64,dmFyIHdvcmQgPSAnaGVsbG8nOwphbGVydCh3b3JkKTsK"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_data_url_with_us_ascii_charset() {
|
|
|
|
let media_type = "";
|
|
|
|
let charset = "us-ascii";
|
|
|
|
let data = "";
|
|
|
|
let data_url = url::create_data_url(
|
|
|
|
media_type,
|
|
|
|
charset,
|
|
|
|
data.as_bytes(),
|
|
|
|
&Url::parse("data:,").unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(data_url.as_str(), "data:;base64,");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_data_url_with_utf8_charset() {
|
|
|
|
let media_type = "";
|
|
|
|
let charset = "utf8";
|
|
|
|
let data = "";
|
|
|
|
let data_url = url::create_data_url(
|
|
|
|
media_type,
|
|
|
|
charset,
|
|
|
|
data.as_bytes(),
|
|
|
|
&Url::parse("data:,").unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(data_url.as_str(), "data:;charset=utf8;base64,");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_data_url_with_media_type_text_plain_and_utf8_charset() {
|
|
|
|
let media_type = "text/plain";
|
|
|
|
let charset = "utf8";
|
|
|
|
let data = "";
|
|
|
|
let data_url = url::create_data_url(
|
|
|
|
media_type,
|
|
|
|
charset,
|
|
|
|
data.as_bytes(),
|
|
|
|
&Url::parse("data:,").unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(data_url.as_str(), "data:text/plain;charset=utf8;base64,");
|
|
|
|
}
|
2020-03-29 09:54:20 +02:00
|
|
|
}
|