Merge pull request #178 from snshn/tests-code-refactor

Group all tests into either passing or failing groups
pull/179/head
Sunshine 4 years ago committed by GitHub
commit 399f515eeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

File diff suppressed because it is too large Load Diff

@ -1,8 +1,3 @@
use reqwest::blocking::Client;
use std::collections::HashMap;
use crate::css;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -10,308 +5,315 @@ use crate::css;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_empty_input() { mod passing {
let cache = &mut HashMap::new(); use crate::css;
let client = Client::new(); use reqwest::blocking::Client;
use std::collections::HashMap;
assert_eq!(
css::embed_css(cache, &client, "", "", false, false, false,), #[test]
"" fn empty_input() {
); let cache = &mut HashMap::new();
} let client = Client::new();
#[test] assert_eq!(
fn passing_style_exclude_unquoted_images() { css::embed_css(cache, &client, "", "", false, false, false,),
let cache = &mut HashMap::new(); ""
let client = Client::new(); );
}
const STYLE: &str = "/* border: none;*/\
background-image: url(https://somewhere.com/bg.png); \ #[test]
list-style: url(/assets/images/bullet.svg);\ fn style_exclude_unquoted_images() {
width:99.998%; \ let cache = &mut HashMap::new();
margin-top: -20px; \ let client = Client::new();
line-height: -1; \
height: calc(100vh - 10pt)"; const STYLE: &str = "/* border: none;*/\
background-image: url(https://somewhere.com/bg.png); \
assert_eq!( list-style: url(/assets/images/bullet.svg);\
css::embed_css( width:99.998%; \
cache, margin-top: -20px; \
&client, line-height: -1; \
"https://doesntmatter.local/", height: calc(100vh - 10pt)";
&STYLE,
false, assert_eq!(
true, css::embed_css(
true, cache,
), &client,
format!( "https://doesntmatter.local/",
"/* border: none;*/\ &STYLE,
background-image: url('{empty_image}'); \ false,
list-style: url('{empty_image}');\ true,
width:99.998%; \ true,
margin-top: -20px; \ ),
line-height: -1; \ format!(
height: calc(100vh - 10pt)", "/* border: none;*/\
empty_image = empty_image!() background-image: url('{empty_image}'); \
) list-style: url('{empty_image}');\
); width:99.998%; \
} margin-top: -20px; \
line-height: -1; \
#[test] height: calc(100vh - 10pt)",
fn passing_style_exclude_single_quoted_images() { empty_image = empty_image!()
let cache = &mut HashMap::new(); )
let client = Client::new(); );
}
const STYLE: &str = "/* border: none;*/\
background-image: url('https://somewhere.com/bg.png'); \ #[test]
list-style: url('/assets/images/bullet.svg');\ fn style_exclude_single_quoted_images() {
width:99.998%; \ let cache = &mut HashMap::new();
margin-top: -20px; \ let client = Client::new();
line-height: -1; \
height: calc(100vh - 10pt)"; const STYLE: &str = "/* border: none;*/\
background-image: url('https://somewhere.com/bg.png'); \
assert_eq!( list-style: url('/assets/images/bullet.svg');\
css::embed_css(cache, &client, "", &STYLE, false, true, true,), width:99.998%; \
format!( margin-top: -20px; \
"/* border: none;*/\ line-height: -1; \
background-image: url('{empty_image}'); \ height: calc(100vh - 10pt)";
list-style: url('{empty_image}');\
width:99.998%; \ assert_eq!(
margin-top: -20px; \ css::embed_css(cache, &client, "", &STYLE, false, true, true,),
line-height: -1; \ format!(
height: calc(100vh - 10pt)", "/* border: none;*/\
empty_image = empty_image!() background-image: url('{empty_image}'); \
) list-style: url('{empty_image}');\
); width:99.998%; \
} margin-top: -20px; \
line-height: -1; \
#[test] height: calc(100vh - 10pt)",
fn passing_style_block() { empty_image = empty_image!()
let cache = &mut HashMap::new(); )
let client = Client::new(); );
}
const CSS: &str = "\
#id.class-name:not(:nth-child(3n+0)) {\n \ #[test]
// border: none;\n \ fn style_block() {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=');\n\ let cache = &mut HashMap::new();
}\n\ let client = Client::new();
\n\
html > body {}"; const CSS: &str = "\
#id.class-name:not(:nth-child(3n+0)) {\n \
assert_eq!( // border: none;\n \
css::embed_css(cache, &client, "file:///", &CSS, false, false, true,), background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=');\n\
CSS }\n\
); \n\
} html > body {}";
#[test] assert_eq!(
fn passing_attribute_selectors() { css::embed_css(cache, &client, "file:///", &CSS, false, false, true,),
let cache = &mut HashMap::new(); CSS
let client = Client::new(); );
}
const CSS: &str = "\
[data-value] { #[test]
/* Attribute exists */ fn attribute_selectors() {
} let cache = &mut HashMap::new();
let client = Client::new();
[data-value='foo'] {
/* Attribute has this exact value */ const CSS: &str = "\
} [data-value] {
/* Attribute exists */
[data-value*='foo'] { }
/* Attribute value contains this value somewhere in it */
} [data-value='foo'] {
/* Attribute has this exact value */
[data-value~='foo'] { }
/* Attribute has this value in a space-separated list somewhere */
} [data-value*='foo'] {
/* Attribute value contains this value somewhere in it */
[data-value^='foo'] { }
/* Attribute value starts with this */
} [data-value~='foo'] {
/* Attribute has this value in a space-separated list somewhere */
[data-value|='foo'] { }
/* Attribute value starts with this in a dash-separated list */
} [data-value^='foo'] {
/* Attribute value starts with this */
[data-value$='foo'] { }
/* Attribute value ends with this */
} [data-value|='foo'] {
"; /* Attribute value starts with this in a dash-separated list */
}
assert_eq!(
css::embed_css(cache, &client, "", &CSS, false, false, false,), [data-value$='foo'] {
CSS /* Attribute value ends with this */
); }
} ";
#[test] assert_eq!(
fn passing_import_string() { css::embed_css(cache, &client, "", &CSS, false, false, false,),
let cache = &mut HashMap::new(); CSS
let client = Client::new(); );
}
const CSS: &str = "\
@charset 'UTF-8';\n\ #[test]
\n\ fn import_string() {
@import 'data:text/css,html{background-color:%23000}';\n\ let cache = &mut HashMap::new();
\n\ let client = Client::new();
@import url('data:text/css,html{color:%23fff}')\n\
"; const CSS: &str = "\
@charset 'UTF-8';\n\
assert_eq!( \n\
css::embed_css( @import 'data:text/css,html{background-color:%23000}';\n\
cache, \n\
&client, @import url('data:text/css,html{color:%23fff}')\n\
"https://doesntmatter.local/", ";
&CSS,
false, assert_eq!(
false, css::embed_css(
true, cache,
), &client,
"\ "https://doesntmatter.local/",
@charset 'UTF-8';\n\ &CSS,
\n\ false,
@import 'data:text/css;base64,aHRtbHtiYWNrZ3JvdW5kLWNvbG9yOiMwMDB9';\n\ false,
\n\ true,
@import url('data:text/css;base64,aHRtbHtjb2xvcjojZmZmfQ==')\n\ ),
" "\
); @charset 'UTF-8';\n\
} \n\
@import 'data:text/css;base64,aHRtbHtiYWNrZ3JvdW5kLWNvbG9yOiMwMDB9';\n\
#[test] \n\
fn passing_hash_urls() { @import url('data:text/css;base64,aHRtbHtjb2xvcjojZmZmfQ==')\n\
let cache = &mut HashMap::new(); "
let client = Client::new(); );
}
const CSS: &str = "\
body {\n \ #[test]
behavior: url(#default#something);\n\ fn hash_urls() {
}\n\ let cache = &mut HashMap::new();
\n\ let client = Client::new();
.scissorHalf {\n \
offset-path: url(#somePath);\n\ const CSS: &str = "\
}\n\ body {\n \
"; behavior: url(#default#something);\n\
}\n\
assert_eq!( \n\
css::embed_css( .scissorHalf {\n \
cache, offset-path: url(#somePath);\n\
&client, }\n\
"https://doesntmatter.local/", ";
&CSS,
false, assert_eq!(
false, css::embed_css(
true, cache,
), &client,
CSS "https://doesntmatter.local/",
); &CSS,
} false,
false,
#[test] true,
fn passing_transform_percentages_and_degrees() { ),
let cache = &mut HashMap::new(); CSS
let client = Client::new(); );
}
const CSS: &str = "\
div {\n \ #[test]
transform: translate(-50%, -50%) rotate(-45deg);\n\ fn transform_percentages_and_degrees() {
transform: translate(50%, 50%) rotate(45deg);\n\ let cache = &mut HashMap::new();
transform: translate(+50%, +50%) rotate(+45deg);\n\ let client = Client::new();
}\n\
"; const CSS: &str = "\
div {\n \
assert_eq!( transform: translate(-50%, -50%) rotate(-45deg);\n\
css::embed_css( transform: translate(50%, 50%) rotate(45deg);\n\
cache, transform: translate(+50%, +50%) rotate(+45deg);\n\
&client, }\n\
"https://doesntmatter.local/", ";
&CSS,
false, assert_eq!(
false, css::embed_css(
true, cache,
), &client,
CSS "https://doesntmatter.local/",
); &CSS,
} false,
false,
#[test] true,
fn passing_unusual_indents() { ),
let cache = &mut HashMap::new(); CSS
let client = Client::new(); );
}
const CSS: &str = "\
.is\\:good:hover {\n \ #[test]
color: green\n\ fn unusual_indents() {
}\n\ let cache = &mut HashMap::new();
\n\ let client = Client::new();
#\\~\\!\\@\\$\\%\\^\\&\\*\\(\\)\\+\\=\\,\\.\\/\\\\\\'\\\"\\;\\:\\?\\>\\<\\[\\]\\{\\}\\|\\`\\# {\n \
color: black\n\ const CSS: &str = "\
}\n\ .is\\:good:hover {\n \
"; color: green\n\
}\n\
assert_eq!( \n\
css::embed_css( #\\~\\!\\@\\$\\%\\^\\&\\*\\(\\)\\+\\=\\,\\.\\/\\\\\\'\\\"\\;\\:\\?\\>\\<\\[\\]\\{\\}\\|\\`\\# {\n \
cache, color: black\n\
&client, }\n\
"https://doesntmatter.local/", ";
&CSS,
false, assert_eq!(
false, css::embed_css(
true, cache,
), &client,
CSS "https://doesntmatter.local/",
); &CSS,
} false,
false,
#[test] true,
fn passing_exclude_fonts() { ),
let cache = &mut HashMap::new(); CSS
let client = Client::new(); );
}
const CSS: &str = "\
@font-face {\n \ #[test]
font-family: 'My Font';\n \ fn exclude_fonts() {
src: url(my_font.woff);\n\ let cache = &mut HashMap::new();
}\n\ let client = Client::new();
\n\
#identifier {\n \ const CSS: &str = "\
font-family: 'My Font' Arial\n\ @font-face {\n \
}\n\ font-family: 'My Font';\n \
\n\ src: url(my_font.woff);\n\
@font-face {\n \ }\n\
font-family: 'My Font';\n \ \n\
src: url(my_font.woff);\n\ #identifier {\n \
}\n\ font-family: 'My Font' Arial\n\
\n\ }\n\
div {\n \ \n\
font-family: 'My Font' Verdana\n\ @font-face {\n \
}\n\ font-family: 'My Font';\n \
"; src: url(my_font.woff);\n\
}\n\
const CSS_OUT: &str = " \ \n\
\n\ div {\n \
\n\ font-family: 'My Font' Verdana\n\
#identifier {\n \ }\n\
font-family: 'My Font' Arial\n\ ";
}\n\
\n \ const CSS_OUT: &str = " \
\n\ \n\
\n\ \n\
div {\n \ #identifier {\n \
font-family: 'My Font' Verdana\n\ font-family: 'My Font' Arial\n\
}\n\ }\n\
"; \n \
\n\
assert_eq!( \n\
css::embed_css( div {\n \
cache, font-family: 'My Font' Verdana\n\
&client, }\n\
"https://doesntmatter.local/", ";
&CSS,
true, assert_eq!(
false, css::embed_css(
true, cache,
), &client,
CSS_OUT "https://doesntmatter.local/",
); &CSS,
true,
false,
true,
),
CSS_OUT
);
}
} }

@ -1,5 +1,3 @@
use crate::css;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,44 +5,49 @@ use crate::css;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_empty_input_single_quotes() { mod passing {
assert_eq!(css::enquote(str!(""), false), "''"); use crate::css;
}
#[test] #[test]
fn passing_empty_input_double_quotes() { fn empty_input_single_quotes() {
assert_eq!(css::enquote(str!(""), true), "\"\""); assert_eq!(css::enquote(str!(""), false), "''");
} }
#[test] #[test]
fn passing_apostrophes_single_quotes() { fn empty_input_double_quotes() {
assert_eq!( assert_eq!(css::enquote(str!(""), true), "\"\"");
css::enquote(str!("It's a lovely day, don't you think?"), false), }
"'It\\'s a lovely day, don\\'t you think?'"
);
}
#[test] #[test]
fn passing_apostrophes_double_quotes() { fn apostrophes_single_quotes() {
assert_eq!( assert_eq!(
css::enquote(str!("It's a lovely day, don't you think?"), true), css::enquote(str!("It's a lovely day, don't you think?"), false),
"\"It's a lovely day, don't you think?\"" "'It\\'s a lovely day, don\\'t you think?'"
); );
} }
#[test] #[test]
fn passing_feet_and_inches_single_quotes() { fn apostrophes_double_quotes() {
assert_eq!( assert_eq!(
css::enquote(str!("5'2\", 6'5\""), false), css::enquote(str!("It's a lovely day, don't you think?"), true),
"'5\\'2\", 6\\'5\"'" "\"It's a lovely day, don't you think?\""
); );
} }
#[test]
fn feet_and_inches_single_quotes() {
assert_eq!(
css::enquote(str!("5'2\", 6'5\""), false),
"'5\\'2\", 6\\'5\"'"
);
}
#[test] #[test]
fn passing_feet_and_inches_double_quotes() { fn feet_and_inches_double_quotes() {
assert_eq!( assert_eq!(
css::enquote(str!("5'2\", 6'5\""), true), css::enquote(str!("5'2\", 6'5\""), true),
"\"5'2\\\", 6'5\\\"\"" "\"5'2\\\", 6'5\\\"\""
); );
}
} }

@ -1,6 +1,3 @@
use crate::html;
use html5ever::rcdom::{Handle, NodeData};
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -8,42 +5,48 @@ use html5ever::rcdom::{Handle, NodeData};
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn get_node_name() { mod passing {
let html = "<!doctype html><html><HEAD></HEAD><body><div><P></P></div></body></html>"; use crate::html;
let dom = html::html_to_dom(&html); use html5ever::rcdom::{Handle, NodeData};
let mut count = 0;
fn test_walk(node: &Handle, i: &mut i8) { #[test]
*i += 1; fn get_node_name() {
let html = "<!doctype html><html><HEAD></HEAD><body><div><P></P></div></body></html>";
let dom = html::html_to_dom(&html);
let mut count = 0;
match &node.data { fn test_walk(node: &Handle, i: &mut i8) {
NodeData::Document => { *i += 1;
for child in node.children.borrow().iter() {
test_walk(child, &mut *i); match &node.data {
} NodeData::Document => {
} for child in node.children.borrow().iter() {
NodeData::Element { ref name, .. } => { test_walk(child, &mut *i);
let node_name = name.local.as_ref().to_string(); }
let parent = html::get_parent_node(node);
let parent_node_name = html::get_node_name(&parent);
if node_name == "head" || node_name == "body" {
assert_eq!(parent_node_name, Some("html"));
} else if node_name == "div" {
assert_eq!(parent_node_name, Some("body"));
} else if node_name == "p" {
assert_eq!(parent_node_name, Some("div"));
} }
NodeData::Element { ref name, .. } => {
let node_name = name.local.as_ref().to_string();
let parent = html::get_parent_node(node);
let parent_node_name = html::get_node_name(&parent);
if node_name == "head" || node_name == "body" {
assert_eq!(parent_node_name, Some("html"));
} else if node_name == "div" {
assert_eq!(parent_node_name, Some("body"));
} else if node_name == "p" {
assert_eq!(parent_node_name, Some("div"));
}
for child in node.children.borrow().iter() { for child in node.children.borrow().iter() {
test_walk(child, &mut *i); test_walk(child, &mut *i);
}
} }
} _ => (),
_ => (), };
}; }
}
test_walk(&dom.document, &mut count); test_walk(&dom.document, &mut count);
assert_eq!(count, 7); assert_eq!(count, 7);
}
} }

@ -1,5 +1,3 @@
use crate::html;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,29 +5,34 @@ use crate::html;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_icon() { mod passing {
assert!(html::is_icon("icon")); use crate::html;
}
#[test]
#[test] fn icon() {
fn passing_shortcut_icon_capitalized() { assert!(html::is_icon("icon"));
assert!(html::is_icon("Shortcut Icon")); }
}
#[test]
#[test] fn shortcut_icon_capitalized() {
fn passing_icon_uppercase() { assert!(html::is_icon("Shortcut Icon"));
assert!(html::is_icon("ICON")); }
}
#[test]
#[test] fn icon_uppercase() {
fn passing_mask_icon() { assert!(html::is_icon("ICON"));
assert!(html::is_icon("mask-icon")); }
}
#[test]
#[test] fn mask_icon() {
fn passing_fluid_icon() { assert!(html::is_icon("mask-icon"));
assert!(html::is_icon("fluid-icon")); }
#[test]
fn fluid_icon() {
assert!(html::is_icon("fluid-icon"));
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -39,12 +42,17 @@ fn passing_fluid_icon() {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_stylesheet() { mod failing {
assert!(!html::is_icon("stylesheet")); use crate::html;
}
#[test]
fn stylesheet() {
assert!(!html::is_icon("stylesheet"));
}
#[test] #[test]
fn failing_empty_string() { fn empty_string() {
assert!(!html::is_icon("")); assert!(!html::is_icon(""));
}
} }

@ -1,5 +1,3 @@
use crate::html;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,182 +5,187 @@ use crate::html;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_div_as_root_element() { mod passing {
let html = "<div><script src=\"some.js\"></script></div>"; use crate::html;
let dom = html::html_to_dom(&html);
#[test]
let opt_no_css: bool = false; fn div_as_root_element() {
let opt_no_frames: bool = false; let html = "<div><script src=\"some.js\"></script></div>";
let opt_no_js: bool = false; let dom = html::html_to_dom(&html);
let opt_no_images: bool = false;
let opt_isolate: bool = false; let opt_no_css: bool = false;
let opt_no_frames: bool = false;
assert_eq!( let opt_no_js: bool = false;
html::stringify_document( let opt_no_images: bool = false;
&dom.document, let opt_isolate: bool = false;
opt_no_css,
opt_no_frames, assert_eq!(
opt_no_js, html::stringify_document(
opt_no_images, &dom.document,
opt_isolate, opt_no_css,
), opt_no_frames,
"<html><head></head><body><div><script src=\"some.js\"></script></div></body></html>" opt_no_js,
); opt_no_images,
} opt_isolate,
),
#[test] "<html><head></head><body><div><script src=\"some.js\"></script></div></body></html>"
fn passing_full_page_with_no_html_head_or_body() { );
let html = "<title>Isolated document</title>\ }
<link rel=\"something\" href=\"some.css\" />\
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src https:\">\ #[test]
<div><script src=\"some.js\"></script></div>"; fn full_page_with_no_html_head_or_body() {
let dom = html::html_to_dom(&html); let html = "<title>Isolated document</title>\
<link rel=\"something\" href=\"some.css\" />\
let opt_no_css: bool = false; <meta http-equiv=\"Content-Security-Policy\" content=\"default-src https:\">\
let opt_no_frames: bool = false; <div><script src=\"some.js\"></script></div>";
let opt_no_js: bool = false; let dom = html::html_to_dom(&html);
let opt_no_images: bool = false;
let opt_isolate: bool = true; let opt_no_css: bool = false;
let opt_no_frames: bool = false;
assert_eq!( let opt_no_js: bool = false;
html::stringify_document( let opt_no_images: bool = false;
&dom.document, let opt_isolate: bool = true;
opt_no_css,
opt_no_frames, assert_eq!(
opt_no_js, html::stringify_document(
opt_no_images, &dom.document,
opt_isolate, opt_no_css,
), opt_no_frames,
"<html>\ opt_no_js,
<head>\ opt_no_images,
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'unsafe-inline' data:;\"></meta>\ opt_isolate,
<title>Isolated document</title>\ ),
<link rel=\"something\" href=\"some.css\">\ "<html>\
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src https:\">\
</head>\
<body>\
<div>\
<script src=\"some.js\"></script>\
</div>\
</body>\
</html>"
);
}
#[test]
fn passing_doctype_and_the_rest_no_html_head_or_body() {
let html = "<!doctype html>\
<title>Unstyled document</title>\
<link rel=\"stylesheet\" href=\"main.css\"/>\
<div style=\"display: none;\"></div>";
let dom = html::html_to_dom(&html);
let opt_no_css: bool = true;
let opt_no_frames: bool = false;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_isolate: bool = false;
assert_eq!(
html::stringify_document(
&dom.document,
opt_no_css,
opt_no_frames,
opt_no_js,
opt_no_images,
opt_isolate,
),
"<!DOCTYPE html>\
<html>\
<head>\
<meta http-equiv=\"Content-Security-Policy\" content=\"style-src 'none';\"></meta>\
<title>Unstyled document</title>\
<link rel=\"stylesheet\" href=\"main.css\">\
</head>\
<body><div style=\"display: none;\"></div></body>\
</html>"
);
}
#[test]
fn passing_doctype_and_the_rest_no_html_head_or_body_forbid_frames() {
let html = "<!doctype html>\
<title>Frameless document</title>\
<link rel=\"something\"/>\
<div><script src=\"some.js\"></script></div>";
let dom = html::html_to_dom(&html);
let opt_no_css: bool = false;
let opt_no_frames: bool = true;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_isolate: bool = false;
assert_eq!(
html::stringify_document(
&dom.document,
opt_no_css,
opt_no_frames,
opt_no_js,
opt_no_images,
opt_isolate,
),
"<!DOCTYPE html>\
<html>\
<head>\
<meta http-equiv=\"Content-Security-Policy\" content=\"frame-src 'none';child-src 'none';\"></meta>\
<title>Frameless document</title>\
<link rel=\"something\">\
</head>\
<body><div><script src=\"some.js\"></script></div></body>\
</html>"
);
}
#[test]
fn passing_doctype_and_the_rest_all_forbidden() {
let html = "<!doctype html>\
<title>no-frame no-css no-js no-image isolated document</title>\
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src https:\">\
<link rel=\"stylesheet\" href=\"some.css\">\
<div>\
<script src=\"some.js\"></script>\
<img style=\"width: 100%;\" src=\"some.png\" />\
<iframe src=\"some.html\"></iframe>\
</div>";
let dom = html::html_to_dom(&html);
let opt_isolate: bool = true;
let opt_no_css: bool = true;
let opt_no_frames: bool = true;
let opt_no_js: bool = true;
let opt_no_images: bool = true;
assert_eq!(
html::stringify_document(
&dom.document,
opt_no_css,
opt_no_frames,
opt_no_js,
opt_no_images,
opt_isolate,
),
"<!DOCTYPE html>\
<html>\
<head>\ <head>\
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'unsafe-inline' data:; style-src 'none'; frame-src 'none';child-src 'none'; script-src 'none'; img-src data:;\"></meta>\ <meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'unsafe-inline' data:;\"></meta>\
<title>no-frame no-css no-js no-image isolated document</title>\ <title>Isolated document</title>\
<link rel=\"something\" href=\"some.css\">\
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src https:\">\ <meta http-equiv=\"Content-Security-Policy\" content=\"default-src https:\">\
<link rel=\"stylesheet\" href=\"some.css\">\
</head>\ </head>\
<body>\ <body>\
<div>\ <div>\
<script src=\"some.js\"></script>\ <script src=\"some.js\"></script>\
<img style=\"width: 100%;\" src=\"some.png\">\
<iframe src=\"some.html\"></iframe>\
</div>\ </div>\
</body>\ </body>\
</html>" </html>"
); );
}
#[test]
fn doctype_and_the_rest_no_html_head_or_body() {
let html = "<!doctype html>\
<title>Unstyled document</title>\
<link rel=\"stylesheet\" href=\"main.css\"/>\
<div style=\"display: none;\"></div>";
let dom = html::html_to_dom(&html);
let opt_no_css: bool = true;
let opt_no_frames: bool = false;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_isolate: bool = false;
assert_eq!(
html::stringify_document(
&dom.document,
opt_no_css,
opt_no_frames,
opt_no_js,
opt_no_images,
opt_isolate,
),
"<!DOCTYPE html>\
<html>\
<head>\
<meta http-equiv=\"Content-Security-Policy\" content=\"style-src 'none';\"></meta>\
<title>Unstyled document</title>\
<link rel=\"stylesheet\" href=\"main.css\">\
</head>\
<body><div style=\"display: none;\"></div></body>\
</html>"
);
}
#[test]
fn doctype_and_the_rest_no_html_head_or_body_forbid_frames() {
let html = "<!doctype html>\
<title>Frameless document</title>\
<link rel=\"something\"/>\
<div><script src=\"some.js\"></script></div>";
let dom = html::html_to_dom(&html);
let opt_no_css: bool = false;
let opt_no_frames: bool = true;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_isolate: bool = false;
assert_eq!(
html::stringify_document(
&dom.document,
opt_no_css,
opt_no_frames,
opt_no_js,
opt_no_images,
opt_isolate,
),
"<!DOCTYPE html>\
<html>\
<head>\
<meta http-equiv=\"Content-Security-Policy\" content=\"frame-src 'none';child-src 'none';\"></meta>\
<title>Frameless document</title>\
<link rel=\"something\">\
</head>\
<body><div><script src=\"some.js\"></script></div></body>\
</html>"
);
}
#[test]
fn doctype_and_the_rest_all_forbidden() {
let html = "<!doctype html>\
<title>no-frame no-css no-js no-image isolated document</title>\
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src https:\">\
<link rel=\"stylesheet\" href=\"some.css\">\
<div>\
<script src=\"some.js\"></script>\
<img style=\"width: 100%;\" src=\"some.png\" />\
<iframe src=\"some.html\"></iframe>\
</div>";
let dom = html::html_to_dom(&html);
let opt_isolate: bool = true;
let opt_no_css: bool = true;
let opt_no_frames: bool = true;
let opt_no_js: bool = true;
let opt_no_images: bool = true;
assert_eq!(
html::stringify_document(
&dom.document,
opt_no_css,
opt_no_frames,
opt_no_js,
opt_no_images,
opt_isolate,
),
"<!DOCTYPE html>\
<html>\
<head>\
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'unsafe-inline' data:; style-src 'none'; frame-src 'none';child-src 'none'; script-src 'none'; img-src data:;\"></meta>\
<title>no-frame no-css no-js no-image isolated document</title>\
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src https:\">\
<link rel=\"stylesheet\" href=\"some.css\">\
</head>\
<body>\
<div>\
<script src=\"some.js\"></script>\
<img style=\"width: 100%;\" src=\"some.png\">\
<iframe src=\"some.html\"></iframe>\
</div>\
</body>\
</html>"
);
}
} }

@ -1,8 +1,3 @@
use crate::html;
use html5ever::serialize::{serialize, SerializeOpts};
use reqwest::blocking::Client;
use std::collections::HashMap;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -10,410 +5,419 @@ use std::collections::HashMap;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_basic() { mod passing {
let cache = &mut HashMap::new(); use crate::html;
use html5ever::serialize::{serialize, SerializeOpts};
let html = "<div><P></P></div>"; use reqwest::blocking::Client;
let dom = html::html_to_dom(&html); use std::collections::HashMap;
let url = "http://localhost";
#[test]
let opt_no_css: bool = false; fn basic() {
let opt_no_fonts: bool = false; let cache = &mut HashMap::new();
let opt_no_frames: bool = false;
let opt_no_js: bool = false; let html = "<div><P></P></div>";
let opt_no_images: bool = false; let dom = html::html_to_dom(&html);
let opt_silent = true; let url = "http://localhost";
let client = Client::new(); let opt_no_css: bool = false;
let opt_no_fonts: bool = false;
html::walk_and_embed_assets( let opt_no_frames: bool = false;
cache, let opt_no_js: bool = false;
&client, let opt_no_images: bool = false;
&url, let opt_silent = true;
&dom.document,
opt_no_css, let client = Client::new();
opt_no_fonts,
opt_no_frames, html::walk_and_embed_assets(
opt_no_js, cache,
opt_no_images, &client,
opt_silent, &url,
); &dom.document,
opt_no_css,
let mut buf: Vec<u8> = Vec::new(); opt_no_fonts,
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); opt_no_frames,
opt_no_js,
assert_eq!( opt_no_images,
buf.iter().map(|&c| c as char).collect::<String>(), opt_silent,
"<html><head></head><body><div><p></p></div></body></html>" );
);
} let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
#[test]
fn passing_ensure_no_recursive_iframe() { assert_eq!(
let html = "<div><P></P><iframe src=\"\"></iframe></div>"; buf.iter().map(|&c| c as char).collect::<String>(),
let dom = html::html_to_dom(&html); "<html><head></head><body><div><p></p></div></body></html>"
let url = "http://localhost"; );
let cache = &mut HashMap::new(); }
let opt_no_css: bool = false; #[test]
let opt_no_fonts: bool = false; fn ensure_no_recursive_iframe() {
let opt_no_frames: bool = false; let html = "<div><P></P><iframe src=\"\"></iframe></div>";
let opt_no_js: bool = false; let dom = html::html_to_dom(&html);
let opt_no_images: bool = false; let url = "http://localhost";
let opt_silent = true; let cache = &mut HashMap::new();
let client = Client::new(); let opt_no_css: bool = false;
let opt_no_fonts: bool = false;
html::walk_and_embed_assets( let opt_no_frames: bool = false;
cache, let opt_no_js: bool = false;
&client, let opt_no_images: bool = false;
&url, let opt_silent = true;
&dom.document,
opt_no_css, let client = Client::new();
opt_no_fonts,
opt_no_frames, html::walk_and_embed_assets(
opt_no_js, cache,
opt_no_images, &client,
opt_silent, &url,
); &dom.document,
opt_no_css,
let mut buf: Vec<u8> = Vec::new(); opt_no_fonts,
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); opt_no_frames,
opt_no_js,
assert_eq!( opt_no_images,
buf.iter().map(|&c| c as char).collect::<String>(), opt_silent,
"<html><head></head><body><div><p></p><iframe src=\"\"></iframe></div></body></html>" );
);
} let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
#[test]
fn passing_ensure_no_recursive_frame() { assert_eq!(
let html = "<frameset><frame src=\"\"></frameset>"; buf.iter().map(|&c| c as char).collect::<String>(),
let dom = html::html_to_dom(&html); "<html><head></head><body><div><p></p><iframe src=\"\"></iframe></div></body></html>"
let url = "http://localhost"; );
let cache = &mut HashMap::new(); }
let opt_no_css: bool = false; #[test]
let opt_no_fonts: bool = false; fn ensure_no_recursive_frame() {
let opt_no_frames: bool = false; let html = "<frameset><frame src=\"\"></frameset>";
let opt_no_js: bool = false; let dom = html::html_to_dom(&html);
let opt_no_images: bool = false; let url = "http://localhost";
let opt_silent = true; let cache = &mut HashMap::new();
let client = Client::new(); let opt_no_css: bool = false;
let opt_no_fonts: bool = false;
html::walk_and_embed_assets( let opt_no_frames: bool = false;
cache, let opt_no_js: bool = false;
&client, let opt_no_images: bool = false;
&url, let opt_silent = true;
&dom.document,
opt_no_css, let client = Client::new();
opt_no_fonts,
opt_no_frames, html::walk_and_embed_assets(
opt_no_js, cache,
opt_no_images, &client,
opt_silent, &url,
); &dom.document,
opt_no_css,
let mut buf: Vec<u8> = Vec::new(); opt_no_fonts,
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); opt_no_frames,
opt_no_js,
assert_eq!( opt_no_images,
buf.iter().map(|&c| c as char).collect::<String>(), opt_silent,
"<html><head></head><frameset><frame src=\"\"></frameset></html>" );
);
} let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
#[test]
fn passing_no_css() { assert_eq!(
let html = "<link rel=\"stylesheet\" href=\"main.css\">\ buf.iter().map(|&c| c as char).collect::<String>(),
<style>html{background-color: #000;}</style>\ "<html><head></head><frameset><frame src=\"\"></frameset></html>"
<div style=\"display: none;\"></div>"; );
let dom = html::html_to_dom(&html); }
let url = "http://localhost";
let cache = &mut HashMap::new(); #[test]
fn no_css() {
let opt_no_css: bool = true; let html = "<link rel=\"stylesheet\" href=\"main.css\">\
let opt_no_fonts: bool = false; <style>html{background-color: #000;}</style>\
let opt_no_frames: bool = false; <div style=\"display: none;\"></div>";
let opt_no_js: bool = false; let dom = html::html_to_dom(&html);
let opt_no_images: bool = false; let url = "http://localhost";
let opt_silent = true; let cache = &mut HashMap::new();
let client = Client::new();
let opt_no_css: bool = true;
html::walk_and_embed_assets( let opt_no_fonts: bool = false;
cache, let opt_no_frames: bool = false;
&client, let opt_no_js: bool = false;
&url, let opt_no_images: bool = false;
&dom.document, let opt_silent = true;
opt_no_css, let client = Client::new();
opt_no_fonts,
opt_no_frames, html::walk_and_embed_assets(
opt_no_js, cache,
opt_no_images, &client,
opt_silent, &url,
); &dom.document,
opt_no_css,
let mut buf: Vec<u8> = Vec::new(); opt_no_fonts,
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); opt_no_frames,
opt_no_js,
assert_eq!( opt_no_images,
buf.iter().map(|&c| c as char).collect::<String>(), opt_silent,
"<html>\ );
<head>\
<link rel=\"stylesheet\">\ let mut buf: Vec<u8> = Vec::new();
<style></style>\ serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
</head>\
<body>\ assert_eq!(
<div></div>\ buf.iter().map(|&c| c as char).collect::<String>(),
</body>\
</html>"
);
}
#[test]
fn passing_no_images() {
let html = "<link rel=\"icon\" href=\"favicon.ico\">\
<div><img src=\"http://localhost/assets/mono_lisa.png\" /></div>";
let dom = html::html_to_dom(&html);
let url = "http://localhost";
let cache = &mut HashMap::new();
let opt_no_css: bool = false;
let opt_no_fonts: bool = false;
let opt_no_frames: bool = false;
let opt_no_js: bool = false;
let opt_no_images: bool = true;
let opt_silent = true;
let client = Client::new();
html::walk_and_embed_assets(
cache,
&client,
&url,
&dom.document,
opt_no_css,
opt_no_fonts,
opt_no_frames,
opt_no_js,
opt_no_images,
opt_silent,
);
let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
assert_eq!(
buf.iter().map(|&c| c as char).collect::<String>(),
format!(
"<html>\ "<html>\
<head>\ <head>\
<link rel=\"icon\">\ <link rel=\"stylesheet\">\
</head>\ <style></style>\
<body>\ </head>\
<div>\ <body>\
<img src=\"{empty_image}\">\ <div></div>\
</div>\ </body>\
</body>\ </html>"
</html>", );
empty_image = empty_image!() }
)
); #[test]
} fn no_images() {
let html = "<link rel=\"icon\" href=\"favicon.ico\">\
#[test] <div><img src=\"http://localhost/assets/mono_lisa.png\" /></div>";
fn passing_no_body_background_images() { let dom = html::html_to_dom(&html);
let html = "<body background=\"no/such/image.png\" background=\"no/such/image2.png\"></body>"; let url = "http://localhost";
let dom = html::html_to_dom(&html); let cache = &mut HashMap::new();
let url = "http://localhost";
let cache = &mut HashMap::new(); let opt_no_css: bool = false;
let opt_no_fonts: bool = false;
let opt_no_css: bool = false; let opt_no_frames: bool = false;
let opt_no_fonts: bool = false; let opt_no_js: bool = false;
let opt_no_frames: bool = false; let opt_no_images: bool = true;
let opt_no_js: bool = false; let opt_silent = true;
let opt_no_images: bool = true;
let opt_silent = true; let client = Client::new();
let client = Client::new(); html::walk_and_embed_assets(
cache,
html::walk_and_embed_assets( &client,
cache, &url,
&client, &dom.document,
&url, opt_no_css,
&dom.document, opt_no_fonts,
opt_no_css, opt_no_frames,
opt_no_fonts, opt_no_js,
opt_no_frames, opt_no_images,
opt_no_js, opt_silent,
opt_no_images, );
opt_silent,
); let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!(
buf.iter().map(|&c| c as char).collect::<String>(),
assert_eq!( format!(
buf.iter().map(|&c| c as char).collect::<String>(), "<html>\
"<html><head></head><body></body></html>" <head>\
); <link rel=\"icon\">\
} </head>\
<body>\
#[test] <div>\
fn passing_no_frames() { <img src=\"{empty_image}\">\
let html = "<frameset><frame src=\"http://trackbook.com\"></frameset>"; </div>\
let dom = html::html_to_dom(&html); </body>\
let url = "http://localhost"; </html>",
let cache = &mut HashMap::new(); empty_image = empty_image!()
)
let opt_no_css: bool = false; );
let opt_no_fonts: bool = false; }
let opt_no_frames: bool = true;
let opt_no_js: bool = false; #[test]
let opt_no_images: bool = false; fn no_body_background_images() {
let opt_silent = true; let html =
let client = Client::new(); "<body background=\"no/such/image.png\" background=\"no/such/image2.png\"></body>";
let dom = html::html_to_dom(&html);
html::walk_and_embed_assets( let url = "http://localhost";
cache, let cache = &mut HashMap::new();
&client,
&url, let opt_no_css: bool = false;
&dom.document, let opt_no_fonts: bool = false;
opt_no_css, let opt_no_frames: bool = false;
opt_no_fonts, let opt_no_js: bool = false;
opt_no_frames, let opt_no_images: bool = true;
opt_no_js, let opt_silent = true;
opt_no_images,
opt_silent, let client = Client::new();
);
html::walk_and_embed_assets(
let mut buf: Vec<u8> = Vec::new(); cache,
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); &client,
&url,
assert_eq!( &dom.document,
buf.iter().map(|&c| c as char).collect::<String>(), opt_no_css,
"<html><head></head><frameset><frame src=\"\"></frameset></html>" opt_no_fonts,
); opt_no_frames,
} opt_no_js,
opt_no_images,
#[test] opt_silent,
fn passing_no_iframes() { );
let html = "<iframe src=\"http://trackbook.com\"></iframe>";
let dom = html::html_to_dom(&html); let mut buf: Vec<u8> = Vec::new();
let url = "http://localhost"; serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
let cache = &mut HashMap::new();
assert_eq!(
let opt_no_css: bool = false; buf.iter().map(|&c| c as char).collect::<String>(),
let opt_no_fonts: bool = false; "<html><head></head><body></body></html>"
let opt_no_frames: bool = true; );
let opt_no_js: bool = false; }
let opt_no_images: bool = false;
let opt_silent = true; #[test]
let client = Client::new(); fn no_frames() {
let html = "<frameset><frame src=\"http://trackbook.com\"></frameset>";
html::walk_and_embed_assets( let dom = html::html_to_dom(&html);
cache, let url = "http://localhost";
&client, let cache = &mut HashMap::new();
&url,
&dom.document, let opt_no_css: bool = false;
opt_no_css, let opt_no_fonts: bool = false;
opt_no_fonts, let opt_no_frames: bool = true;
opt_no_frames, let opt_no_js: bool = false;
opt_no_js, let opt_no_images: bool = false;
opt_no_images, let opt_silent = true;
opt_silent, let client = Client::new();
);
html::walk_and_embed_assets(
let mut buf: Vec<u8> = Vec::new(); cache,
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); &client,
&url,
assert_eq!( &dom.document,
buf.iter().map(|&c| c as char).collect::<String>(), opt_no_css,
"<html><head></head><body><iframe src=\"\"></iframe></body></html>" opt_no_fonts,
); opt_no_frames,
} opt_no_js,
opt_no_images,
#[test] opt_silent,
fn passing_no_js() { );
let html = "<div onClick=\"void(0)\">\
<script src=\"http://localhost/assets/some.js\"></script>\ let mut buf: Vec<u8> = Vec::new();
<script>alert(1)</script>\ serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
</div>";
let dom = html::html_to_dom(&html); assert_eq!(
let url = "http://localhost"; buf.iter().map(|&c| c as char).collect::<String>(),
let cache = &mut HashMap::new(); "<html><head></head><frameset><frame src=\"\"></frameset></html>"
);
let opt_no_css: bool = false; }
let opt_no_fonts: bool = false;
let opt_no_frames: bool = false; #[test]
let opt_no_js: bool = true; fn no_iframes() {
let opt_no_images: bool = false; let html = "<iframe src=\"http://trackbook.com\"></iframe>";
let opt_silent = true; let dom = html::html_to_dom(&html);
let url = "http://localhost";
let client = Client::new(); let cache = &mut HashMap::new();
html::walk_and_embed_assets( let opt_no_css: bool = false;
cache, let opt_no_fonts: bool = false;
&client, let opt_no_frames: bool = true;
&url, let opt_no_js: bool = false;
&dom.document, let opt_no_images: bool = false;
opt_no_css, let opt_silent = true;
opt_no_fonts, let client = Client::new();
opt_no_frames,
opt_no_js, html::walk_and_embed_assets(
opt_no_images, cache,
opt_silent, &client,
); &url,
&dom.document,
let mut buf: Vec<u8> = Vec::new(); opt_no_css,
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); opt_no_fonts,
opt_no_frames,
assert_eq!( opt_no_js,
buf.iter().map(|&c| c as char).collect::<String>(), opt_no_images,
"<html><head></head><body><div><script></script>\ opt_silent,
<script></script></div></body></html>" );
);
} let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
#[test]
fn passing_with_no_integrity() { assert_eq!(
let html = "<title>No integrity</title>\ buf.iter().map(|&c| c as char).collect::<String>(),
<link integrity=\"sha384-...\" rel=\"something\"/>\ "<html><head></head><body><iframe src=\"\"></iframe></body></html>"
<script integrity=\"sha384-...\" src=\"some.js\"></script>"; );
let dom = html::html_to_dom(&html); }
let url = "http://localhost";
let cache = &mut HashMap::new(); #[test]
let client = Client::new(); fn no_js() {
let opt_no_css: bool = true; let html = "<div onClick=\"void(0)\">\
let opt_no_fonts: bool = false; <script src=\"http://localhost/assets/some.js\"></script>\
let opt_no_frames: bool = true; <script>alert(1)</script>\
let opt_no_js: bool = true; </div>";
let opt_no_images: bool = true; let dom = html::html_to_dom(&html);
let opt_silent = true; let url = "http://localhost";
let cache = &mut HashMap::new();
html::walk_and_embed_assets(
cache, let opt_no_css: bool = false;
&client, let opt_no_fonts: bool = false;
&url, let opt_no_frames: bool = false;
&dom.document, let opt_no_js: bool = true;
opt_no_css, let opt_no_images: bool = false;
opt_no_fonts, let opt_silent = true;
opt_no_frames,
opt_no_js, let client = Client::new();
opt_no_images,
opt_silent, html::walk_and_embed_assets(
); cache,
&client,
let mut buf: Vec<u8> = Vec::new(); &url,
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); &dom.document,
opt_no_css,
assert_eq!( opt_no_fonts,
buf.iter().map(|&c| c as char).collect::<String>(), opt_no_frames,
"<html>\ opt_no_js,
<head><title>No integrity</title><link rel=\"something\"><script></script></head>\ opt_no_images,
<body></body>\ opt_silent,
</html>" );
);
let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
assert_eq!(
buf.iter().map(|&c| c as char).collect::<String>(),
"<html><head></head><body><div><script></script>\
<script></script></div></body></html>"
);
}
#[test]
fn with_no_integrity() {
let html = "<title>No integrity</title>\
<link integrity=\"sha384-...\" rel=\"something\"/>\
<script integrity=\"sha384-...\" src=\"some.js\"></script>";
let dom = html::html_to_dom(&html);
let url = "http://localhost";
let cache = &mut HashMap::new();
let client = Client::new();
let opt_no_css: bool = true;
let opt_no_fonts: bool = false;
let opt_no_frames: bool = true;
let opt_no_js: bool = true;
let opt_no_images: bool = true;
let opt_silent = true;
html::walk_and_embed_assets(
cache,
&client,
&url,
&dom.document,
opt_no_css,
opt_no_fonts,
opt_no_frames,
opt_no_js,
opt_no_images,
opt_silent,
);
let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
assert_eq!(
buf.iter().map(|&c| c as char).collect::<String>(),
"<html>\
<head><title>No integrity</title><link rel=\"something\"><script></script></head>\
<body></body>\
</html>"
);
}
} }

@ -1,5 +1,3 @@
use crate::js;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,19 +5,24 @@ use crate::js;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_onblur_camelcase() { mod passing {
assert!(js::attr_is_event_handler("onBlur")); use crate::js;
}
#[test] #[test]
fn passing_onclick_lowercase() { fn onblur_camelcase() {
assert!(js::attr_is_event_handler("onclick")); assert!(js::attr_is_event_handler("onBlur"));
} }
#[test]
fn onclick_lowercase() {
assert!(js::attr_is_event_handler("onclick"));
}
#[test] #[test]
fn passing_onclick_camelcase() { fn onclick_camelcase() {
assert!(js::attr_is_event_handler("onClick")); assert!(js::attr_is_event_handler("onClick"));
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -29,17 +32,22 @@ fn passing_onclick_camelcase() {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_href() { mod failing {
assert!(!js::attr_is_event_handler("href")); use crate::js;
}
#[test] #[test]
fn failing_empty_string() { fn href() {
assert!(!js::attr_is_event_handler("")); assert!(!js::attr_is_event_handler("href"));
} }
#[test]
fn empty_string() {
assert!(!js::attr_is_event_handler(""));
}
#[test] #[test]
fn failing_class() { fn class() {
assert!(!js::attr_is_event_handler("class")); assert!(!js::attr_is_event_handler("class"));
}
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,42 +5,47 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_removes_fragment() { mod passing {
assert_eq!( use crate::utils;
utils::clean_url("https://somewhere.com/font.eot#iefix"),
"https://somewhere.com/font.eot"
);
}
#[test] #[test]
fn passing_removes_empty_fragment() { fn removes_fragment() {
assert_eq!( assert_eq!(
utils::clean_url("https://somewhere.com/font.eot#"), utils::clean_url("https://somewhere.com/font.eot#iefix"),
"https://somewhere.com/font.eot" "https://somewhere.com/font.eot"
); );
} }
#[test] #[test]
fn passing_removes_empty_query_and_empty_fragment() { fn removes_empty_fragment() {
assert_eq!( assert_eq!(
utils::clean_url("https://somewhere.com/font.eot?#"), utils::clean_url("https://somewhere.com/font.eot#"),
"https://somewhere.com/font.eot" "https://somewhere.com/font.eot"
); );
} }
#[test] #[test]
fn passing_removes_empty_query_amp_and_empty_fragment() { fn removes_empty_query_and_empty_fragment() {
assert_eq!( assert_eq!(
utils::clean_url("https://somewhere.com/font.eot?a=b&#"), utils::clean_url("https://somewhere.com/font.eot?#"),
"https://somewhere.com/font.eot?a=b" "https://somewhere.com/font.eot"
); );
} }
#[test]
fn removes_empty_query_amp_and_empty_fragment() {
assert_eq!(
utils::clean_url("https://somewhere.com/font.eot?a=b&#"),
"https://somewhere.com/font.eot?a=b"
);
}
#[test] #[test]
fn passing_keeps_credentials() { fn keeps_credentials() {
assert_eq!( assert_eq!(
utils::clean_url("https://cookie:monster@gibson.internet/"), utils::clean_url("https://cookie:monster@gibson.internet/"),
"https://cookie:monster@gibson.internet/" "https://cookie:monster@gibson.internet/"
); );
}
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,22 +5,27 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_encode_string_with_specific_media_type() { mod passing {
let mime = "application/javascript"; use crate::utils;
let data = "var word = 'hello';\nalert(word);\n";
let data_url = utils::data_to_data_url(mime, data.as_bytes(), "", "");
assert_eq!( #[test]
&data_url, fn encode_string_with_specific_media_type() {
"data:application/javascript;base64,dmFyIHdvcmQgPSAnaGVsbG8nOwphbGVydCh3b3JkKTsK" let mime = "application/javascript";
); let data = "var word = 'hello';\nalert(word);\n";
} let data_url = utils::data_to_data_url(mime, data.as_bytes(), "", "");
assert_eq!(
&data_url,
"data:application/javascript;base64,dmFyIHdvcmQgPSAnaGVsbG8nOwphbGVydCh3b3JkKTsK"
);
}
#[test] #[test]
fn passing_encode_append_fragment() { fn encode_append_fragment() {
let data = "<svg></svg>\n"; let data = "<svg></svg>\n";
let data_url = utils::data_to_data_url("text/css", data.as_bytes(), "", "fragment"); let data_url = utils::data_to_data_url("text/css", data.as_bytes(), "", "fragment");
assert_eq!(&data_url, "data:text/css;base64,PHN2Zz48L3N2Zz4K#fragment"); assert_eq!(&data_url, "data:text/css;base64,PHN2Zz48L3N2Zz4K#fragment");
}
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,76 +5,82 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_parse_text_html_base64() { mod passing {
let (media_type, data) = utils::data_url_to_data("data:text/html;base64,V29yayBleHBhbmRzIHNvIGFzIHRvIGZpbGwgdGhlIHRpbWUgYXZhaWxhYmxlIGZvciBpdHMgY29tcGxldGlvbg=="); use crate::utils;
assert_eq!(media_type, "text/html"); #[test]
assert_eq!( fn parse_text_html_base64() {
String::from_utf8_lossy(&data), let (media_type, data) = utils::data_url_to_data("data:text/html;base64,V29yayBleHBhbmRzIHNvIGFzIHRvIGZpbGwgdGhlIHRpbWUgYXZhaWxhYmxlIGZvciBpdHMgY29tcGxldGlvbg==");
"Work expands so as to fill the time available for its completion"
); assert_eq!(media_type, "text/html");
} assert_eq!(
String::from_utf8_lossy(&data),
#[test] "Work expands so as to fill the time available for its completion"
fn passing_parse_text_html_utf8() { );
let (media_type, data) = utils::data_url_to_data( }
"data:text/html;utf8,Work expands so as to fill the time available for its completion",
); #[test]
fn parse_text_html_utf8() {
assert_eq!(media_type, "text/html"); let (media_type, data) = utils::data_url_to_data(
assert_eq!( "data:text/html;utf8,Work expands so as to fill the time available for its completion",
String::from_utf8_lossy(&data), );
"Work expands so as to fill the time available for its completion"
); assert_eq!(media_type, "text/html");
} assert_eq!(
String::from_utf8_lossy(&data),
#[test] "Work expands so as to fill the time available for its completion"
fn passing_parse_text_html_plaintext() { );
let (media_type, data) = utils::data_url_to_data( }
"data:text/html,Work expands so as to fill the time available for its completion",
); #[test]
fn parse_text_html_plaintext() {
assert_eq!(media_type, "text/html"); let (media_type, data) = utils::data_url_to_data(
assert_eq!( "data:text/html,Work expands so as to fill the time available for its completion",
String::from_utf8_lossy(&data), );
"Work expands so as to fill the time available for its completion"
); assert_eq!(media_type, "text/html");
} assert_eq!(
String::from_utf8_lossy(&data),
#[test] "Work expands so as to fill the time available for its completion"
fn passing_parse_text_html_charset_utf_8_between_two_whitespaces() { );
let (media_type, data) = utils::data_url_to_data(" data:text/html;charset=utf-8,Work expands so as to fill the time available for its completion "); }
assert_eq!(media_type, "text/html"); #[test]
assert_eq!( fn parse_text_html_charset_utf_8_between_two_whitespaces() {
String::from_utf8_lossy(&data), let (media_type, data) = utils::data_url_to_data(" data:text/html;charset=utf-8,Work expands so as to fill the time available for its completion ");
"Work expands so as to fill the time available for its completion"
); assert_eq!(media_type, "text/html");
} assert_eq!(
String::from_utf8_lossy(&data),
#[test] "Work expands so as to fill the time available for its completion"
fn passing_parse_text_css_url_encoded() { );
let (media_type, data) = utils::data_url_to_data("data:text/css,div{background-color:%23000}"); }
assert_eq!(media_type, "text/css"); #[test]
assert_eq!(String::from_utf8_lossy(&data), "div{background-color:#000}"); fn parse_text_css_url_encoded() {
} let (media_type, data) =
utils::data_url_to_data("data:text/css,div{background-color:%23000}");
#[test]
fn passing_parse_no_media_type_base64() { assert_eq!(media_type, "text/css");
let (media_type, data) = utils::data_url_to_data("data:;base64,dGVzdA=="); assert_eq!(String::from_utf8_lossy(&data), "div{background-color:#000}");
}
assert_eq!(media_type, "");
assert_eq!(String::from_utf8_lossy(&data), "test"); #[test]
} fn parse_no_media_type_base64() {
let (media_type, data) = utils::data_url_to_data("data:;base64,dGVzdA==");
#[test]
fn passing_parse_no_media_type_no_encoding() { assert_eq!(media_type, "");
let (media_type, data) = utils::data_url_to_data("data:;,test%20test"); assert_eq!(String::from_utf8_lossy(&data), "test");
}
assert_eq!(media_type, "");
assert_eq!(String::from_utf8_lossy(&data), "test test"); #[test]
fn parse_no_media_type_no_encoding() {
let (media_type, data) = utils::data_url_to_data("data:;,test%20test");
assert_eq!(media_type, "");
assert_eq!(String::from_utf8_lossy(&data), "test test");
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -86,10 +90,15 @@ fn passing_parse_no_media_type_no_encoding() {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_just_word_data() { mod failing {
let (media_type, data) = utils::data_url_to_data("data"); use crate::utils;
#[test]
fn just_word_data() {
let (media_type, data) = utils::data_url_to_data("data");
assert_eq!(media_type, ""); assert_eq!(media_type, "");
assert_eq!(String::from_utf8_lossy(&data), ""); assert_eq!(String::from_utf8_lossy(&data), "");
}
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,131 +5,136 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_image_gif87() { mod passing {
assert_eq!(utils::detect_media_type(b"GIF87a", ""), "image/gif"); use crate::utils;
}
#[test]
#[test] fn image_gif87() {
fn passing_image_gif89() { assert_eq!(utils::detect_media_type(b"GIF87a", ""), "image/gif");
assert_eq!(utils::detect_media_type(b"GIF89a", ""), "image/gif"); }
}
#[test]
#[test] fn image_gif89() {
fn passing_image_jpeg() { assert_eq!(utils::detect_media_type(b"GIF89a", ""), "image/gif");
assert_eq!(utils::detect_media_type(b"\xFF\xD8\xFF", ""), "image/jpeg"); }
}
#[test]
#[test] fn image_jpeg() {
fn passing_image_png() { assert_eq!(utils::detect_media_type(b"\xFF\xD8\xFF", ""), "image/jpeg");
assert_eq!( }
utils::detect_media_type(b"\x89PNG\x0D\x0A\x1A\x0A", ""),
"image/png" #[test]
); fn image_png() {
} assert_eq!(
utils::detect_media_type(b"\x89PNG\x0D\x0A\x1A\x0A", ""),
#[test] "image/png"
fn passing_image_svg() { );
assert_eq!(utils::detect_media_type(b"<svg ", ""), "image/svg+xml"); }
}
#[test]
#[test] fn image_svg() {
fn passing_image_webp() { assert_eq!(utils::detect_media_type(b"<svg ", ""), "image/svg+xml");
assert_eq!( }
utils::detect_media_type(b"RIFF....WEBPVP8 ", ""),
"image/webp" #[test]
); fn image_webp() {
} assert_eq!(
utils::detect_media_type(b"RIFF....WEBPVP8 ", ""),
#[test] "image/webp"
fn passing_image_icon() { );
assert_eq!( }
utils::detect_media_type(b"\x00\x00\x01\x00", ""),
"image/x-icon" #[test]
); fn image_icon() {
} assert_eq!(
utils::detect_media_type(b"\x00\x00\x01\x00", ""),
#[test] "image/x-icon"
fn passing_image_svg_filename() { );
assert_eq!( }
utils::detect_media_type(b"<?xml ", "local-file.svg"),
"image/svg+xml" #[test]
); fn image_svg_filename() {
} assert_eq!(
utils::detect_media_type(b"<?xml ", "local-file.svg"),
#[test] "image/svg+xml"
fn passing_image_svg_url_uppercase() { );
assert_eq!( }
utils::detect_media_type(b"", "https://some-site.com/images/local-file.SVG"),
"image/svg+xml" #[test]
); fn image_svg_url_uppercase() {
} assert_eq!(
utils::detect_media_type(b"", "https://some-site.com/images/local-file.SVG"),
#[test] "image/svg+xml"
fn passing_audio_mpeg() { );
assert_eq!(utils::detect_media_type(b"ID3", ""), "audio/mpeg"); }
}
#[test]
#[test] fn audio_mpeg() {
fn passing_audio_mpeg_2() { assert_eq!(utils::detect_media_type(b"ID3", ""), "audio/mpeg");
assert_eq!(utils::detect_media_type(b"\xFF\x0E", ""), "audio/mpeg"); }
}
#[test]
#[test] fn audio_mpeg_2() {
fn passing_audio_mpeg_3() { assert_eq!(utils::detect_media_type(b"\xFF\x0E", ""), "audio/mpeg");
assert_eq!(utils::detect_media_type(b"\xFF\x0F", ""), "audio/mpeg"); }
}
#[test]
#[test] fn audio_mpeg_3() {
fn passing_audio_ogg() { assert_eq!(utils::detect_media_type(b"\xFF\x0F", ""), "audio/mpeg");
assert_eq!(utils::detect_media_type(b"OggS", ""), "audio/ogg"); }
}
#[test]
#[test] fn audio_ogg() {
fn passing_audio_wav() { assert_eq!(utils::detect_media_type(b"OggS", ""), "audio/ogg");
assert_eq!( }
utils::detect_media_type(b"RIFF....WAVEfmt ", ""),
"audio/wav" #[test]
); fn audio_wav() {
} assert_eq!(
utils::detect_media_type(b"RIFF....WAVEfmt ", ""),
#[test] "audio/wav"
fn passing_audio_flac() { );
assert_eq!(utils::detect_media_type(b"fLaC", ""), "audio/x-flac"); }
}
#[test]
#[test] fn audio_flac() {
fn passing_video_avi() { assert_eq!(utils::detect_media_type(b"fLaC", ""), "audio/x-flac");
assert_eq!( }
utils::detect_media_type(b"RIFF....AVI LIST", ""),
"video/avi" #[test]
); fn video_avi() {
} assert_eq!(
utils::detect_media_type(b"RIFF....AVI LIST", ""),
#[test] "video/avi"
fn passing_video_mp4() { );
assert_eq!(utils::detect_media_type(b"....ftyp", ""), "video/mp4"); }
}
#[test]
#[test] fn video_mp4() {
fn passing_video_mpeg() { assert_eq!(utils::detect_media_type(b"....ftyp", ""), "video/mp4");
assert_eq!( }
utils::detect_media_type(b"\x00\x00\x01\x0B", ""),
"video/mpeg" #[test]
); fn video_mpeg() {
} assert_eq!(
utils::detect_media_type(b"\x00\x00\x01\x0B", ""),
#[test] "video/mpeg"
fn passing_video_quicktime() { );
assert_eq!(utils::detect_media_type(b"....moov", ""), "video/quicktime"); }
}
#[test]
#[test] fn video_quicktime() {
fn passing_video_webm() { assert_eq!(utils::detect_media_type(b"....moov", ""), "video/quicktime");
assert_eq!( }
utils::detect_media_type(b"\x1A\x45\xDF\xA3", ""),
"video/webm" #[test]
); fn video_webm() {
assert_eq!(
utils::detect_media_type(b"\x1A\x45\xDF\xA3", ""),
"video/webm"
);
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -141,7 +144,12 @@ fn passing_video_webm() {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_unknown_media_type() { mod failing {
assert_eq!(utils::detect_media_type(b"abcdef0123456789", ""), ""); use crate::utils;
#[test]
fn unknown_media_type() {
assert_eq!(utils::detect_media_type(b"abcdef0123456789", ""), "");
}
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,32 +5,37 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_remove_protocl_and_fragment() { mod passing {
if cfg!(windows) { use crate::utils;
assert_eq!(
utils::file_url_to_fs_path("file:///C:/documents/some-path/some-file.svg#fragment"), #[test]
"C:\\documents\\some-path\\some-file.svg" fn remove_protocl_and_fragment() {
); if cfg!(windows) {
} else { assert_eq!(
assert_eq!( utils::file_url_to_fs_path("file:///C:/documents/some-path/some-file.svg#fragment"),
utils::file_url_to_fs_path("file:///tmp/some-path/some-file.svg#fragment"), "C:\\documents\\some-path\\some-file.svg"
"/tmp/some-path/some-file.svg" );
); } else {
assert_eq!(
utils::file_url_to_fs_path("file:///tmp/some-path/some-file.svg#fragment"),
"/tmp/some-path/some-file.svg"
);
}
} }
}
#[test] #[test]
fn passing_decodes_urls() { fn decodes_urls() {
if cfg!(windows) { if cfg!(windows) {
assert_eq!( assert_eq!(
utils::file_url_to_fs_path("file:///C:/Documents%20and%20Settings/some-file.html"), utils::file_url_to_fs_path("file:///C:/Documents%20and%20Settings/some-file.html"),
"C:\\Documents and Settings\\some-file.html" "C:\\Documents and Settings\\some-file.html"
); );
} else { } else {
assert_eq!( assert_eq!(
utils::file_url_to_fs_path("file:///home/user/My%20Documents"), utils::file_url_to_fs_path("file:///home/user/My%20Documents"),
"/home/user/My Documents" "/home/user/My Documents"
); );
}
} }
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,17 +5,22 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_data_url() { mod passing {
assert_eq!( use crate::utils;
utils::get_url_fragment(
"data:image/svg+xml;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h#test" #[test]
), fn data_url() {
"test" assert_eq!(
); utils::get_url_fragment(
} "data:image/svg+xml;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h#test"
),
"test"
);
}
#[test] #[test]
fn passing_https_empty() { fn https_empty() {
assert_eq!(utils::get_url_fragment("https://kernel.org#"), ""); assert_eq!(utils::get_url_fragment("https://kernel.org#"), "");
}
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,18 +5,23 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_data_url_text_html() { mod passing {
assert!(utils::is_data_url( use crate::utils;
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h"
)); #[test]
} fn data_url_text_html() {
assert!(utils::is_data_url(
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h"
));
}
#[test] #[test]
fn passing_data_url_no_media_type() { fn data_url_no_media_type() {
assert!(utils::is_data_url( assert!(utils::is_data_url(
"data:;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h" "data:;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h"
)); ));
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -28,17 +31,22 @@ fn passing_data_url_no_media_type() {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_https_url() { mod failing {
assert!(!utils::is_data_url("https://kernel.org")); use crate::utils;
}
#[test] #[test]
fn failing_no_protocol_url() { fn https_url() {
assert!(!utils::is_data_url("//kernel.org")); assert!(!utils::is_data_url("https://kernel.org"));
} }
#[test]
fn no_protocol_url() {
assert!(!utils::is_data_url("//kernel.org"));
}
#[test] #[test]
fn failing_empty_string() { fn empty_string() {
assert!(!utils::is_data_url("")); assert!(!utils::is_data_url(""));
}
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,32 +5,37 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_unix_file_url() { mod passing {
assert!(utils::is_file_url( use crate::utils;
"file:///home/user/Websites/my-website/index.html"
));
}
#[test] #[test]
fn passing_windows_file_url() { fn unix_file_url() {
assert!(utils::is_file_url( assert!(utils::is_file_url(
"file:///C:/Documents%20and%20Settings/user/Websites/my-website/assets/images/logo.png" "file:///home/user/Websites/my-website/index.html"
)); ));
} }
#[test] #[test]
fn passing_unix_url_with_backslashes() { fn windows_file_url() {
assert!(utils::is_file_url( assert!(utils::is_file_url(
"file:\\\\\\home\\user\\Websites\\my-website\\index.html" "file:///C:/Documents%20and%20Settings/user/Websites/my-website/assets/images/logo.png"
)); ));
} }
#[test]
fn unix_url_with_backslashes() {
assert!(utils::is_file_url(
"file:\\\\\\home\\user\\Websites\\my-website\\index.html"
));
}
#[test] #[test]
fn passing_windows_file_url_with_backslashes() { fn windows_file_url_with_backslashes() {
assert!(utils::is_file_url( assert!(utils::is_file_url(
"file:\\\\\\C:\\Documents%20and%20Settings\\user\\Websites\\my-website\\assets\\images\\logo.png" "file:\\\\\\C:\\Documents%20and%20Settings\\user\\Websites\\my-website\\assets\\images\\logo.png"
)); ));
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -42,34 +45,39 @@ fn passing_windows_file_url_with_backslashes() {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_url_with_no_protocl() { mod failing {
assert!(!utils::is_file_url("//kernel.org")); use crate::utils;
}
#[test] #[test]
fn failing_dot_slash_filename() { fn url_with_no_protocl() {
assert!(!utils::is_file_url("./index.html")); assert!(!utils::is_file_url("//kernel.org"));
} }
#[test] #[test]
fn failing_just_filename() { fn dot_slash_filename() {
assert!(!utils::is_file_url("some-local-page.htm")); assert!(!utils::is_file_url("./index.html"));
} }
#[test] #[test]
fn failing_https_ip_port_url() { fn just_filename() {
assert!(!utils::is_file_url("https://1.2.3.4:80/www/index.html")); assert!(!utils::is_file_url("some-local-page.htm"));
} }
#[test] #[test]
fn failing_data_url() { fn https_ip_port_url() {
assert!(!utils::is_file_url( assert!(!utils::is_file_url("https://1.2.3.4:80/www/index.html"));
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h" }
));
} #[test]
fn data_url() {
assert!(!utils::is_file_url(
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h"
));
}
#[test] #[test]
fn failing_just_word_file() { fn just_word_file() {
assert!(!utils::is_file_url("file")); assert!(!utils::is_file_url("file"));
}
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,19 +5,24 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_http_url() { mod passing {
assert!(utils::is_http_url("http://kernel.org")); use crate::utils;
}
#[test] #[test]
fn passing_https_url() { fn http_url() {
assert!(utils::is_http_url("https://www.rust-lang.org/")); assert!(utils::is_http_url("http://kernel.org"));
} }
#[test] #[test]
fn passing_http_url_with_backslashes() { fn https_url() {
assert!(utils::is_http_url("http:\\\\freebsd.org\\")); assert!(utils::is_http_url("https://www.rust-lang.org/"));
}
#[test]
fn http_url_with_backslashes() {
assert!(utils::is_http_url("http:\\\\freebsd.org\\"));
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -29,29 +32,34 @@ fn passing_http_url_with_backslashes() {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_url_with_no_protocol() { mod failing {
assert!(!utils::is_http_url("//kernel.org")); use crate::utils;
}
#[test] #[test]
fn failing_dot_slash_filename() { fn url_with_no_protocol() {
assert!(!utils::is_http_url("./index.html")); assert!(!utils::is_http_url("//kernel.org"));
} }
#[test] #[test]
fn failing_just_filename() { fn dot_slash_filename() {
assert!(!utils::is_http_url("some-local-page.htm")); assert!(!utils::is_http_url("./index.html"));
} }
#[test] #[test]
fn failing_https_ip_port_url() { fn just_filename() {
assert!(!utils::is_http_url("ftp://1.2.3.4/www/index.html")); assert!(!utils::is_http_url("some-local-page.htm"));
} }
#[test]
fn https_ip_port_url() {
assert!(!utils::is_http_url("ftp://1.2.3.4/www/index.html"));
}
#[test] #[test]
fn failing_data_url() { fn data_url() {
assert!(!utils::is_http_url( assert!(!utils::is_http_url(
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h" "data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h"
)); ));
}
} }

@ -1,7 +1,3 @@
use url::ParseError;
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -9,203 +5,210 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_from_https_to_level_up_relative() -> Result<(), ParseError> { mod passing {
let resolved_url = utils::resolve_url("https://www.kernel.org", "../category/signatures.html")?; use crate::utils;
use url::ParseError;
assert_eq!(
resolved_url.as_str(), #[test]
"https://www.kernel.org/category/signatures.html" fn from_https_to_level_up_relative() -> Result<(), ParseError> {
); let resolved_url =
utils::resolve_url("https://www.kernel.org", "../category/signatures.html")?;
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "https://www.kernel.org/category/signatures.html"
fn passing_from_just_filename_to_full_https_url() -> Result<(), ParseError> { );
let resolved_url = utils::resolve_url(
"saved_page.htm", Ok(())
"https://www.kernel.org/category/signatures.html", }
)?;
#[test]
assert_eq!( fn from_just_filename_to_full_https_url() -> Result<(), ParseError> {
resolved_url.as_str(), let resolved_url = utils::resolve_url(
"https://www.kernel.org/category/signatures.html" "saved_page.htm",
); "https://www.kernel.org/category/signatures.html",
)?;
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "https://www.kernel.org/category/signatures.html"
fn passing_from_https_url_to_url_with_no_protocol() -> Result<(), ParseError> { );
let resolved_url = utils::resolve_url(
"https://www.kernel.org", Ok(())
"//www.kernel.org/theme/images/logos/tux.png", }
)?;
#[test]
assert_eq!( fn from_https_url_to_url_with_no_protocol() -> Result<(), ParseError> {
resolved_url.as_str(), let resolved_url = utils::resolve_url(
"https://www.kernel.org/theme/images/logos/tux.png" "https://www.kernel.org",
); "//www.kernel.org/theme/images/logos/tux.png",
)?;
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "https://www.kernel.org/theme/images/logos/tux.png"
fn passing_from_https_url_to_url_with_no_protocol_and_on_different_hostname( );
) -> Result<(), ParseError> {
let resolved_url = utils::resolve_url( Ok(())
"https://www.kernel.org", }
"//another-host.org/theme/images/logos/tux.png",
)?; #[test]
fn from_https_url_to_url_with_no_protocol_and_on_different_hostname() -> Result<(), ParseError>
assert_eq!( {
resolved_url.as_str(), let resolved_url = utils::resolve_url(
"https://another-host.org/theme/images/logos/tux.png" "https://www.kernel.org",
); "//another-host.org/theme/images/logos/tux.png",
)?;
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "https://another-host.org/theme/images/logos/tux.png"
fn passing_from_https_url_to_relative_root_path() -> Result<(), ParseError> { );
let resolved_url = utils::resolve_url(
"https://www.kernel.org/category/signatures.html", Ok(())
"/theme/images/logos/tux.png", }
)?;
#[test]
assert_eq!( fn from_https_url_to_relative_root_path() -> Result<(), ParseError> {
resolved_url.as_str(), let resolved_url = utils::resolve_url(
"https://www.kernel.org/theme/images/logos/tux.png" "https://www.kernel.org/category/signatures.html",
); "/theme/images/logos/tux.png",
)?;
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "https://www.kernel.org/theme/images/logos/tux.png"
fn passing_from_https_to_just_filename() -> Result<(), ParseError> { );
let resolved_url = utils::resolve_url(
"https://www.w3schools.com/html/html_iframe.asp", Ok(())
"default.asp", }
)?;
#[test]
assert_eq!( fn from_https_to_just_filename() -> Result<(), ParseError> {
resolved_url.as_str(), let resolved_url = utils::resolve_url(
"https://www.w3schools.com/html/default.asp" "https://www.w3schools.com/html/html_iframe.asp",
); "default.asp",
)?;
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "https://www.w3schools.com/html/default.asp"
fn passing_from_data_url_to_https() -> Result<(), ParseError> { );
let resolved_url = utils::resolve_url(
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h", Ok(())
"https://www.kernel.org/category/signatures.html", }
)?;
#[test]
assert_eq!( fn from_data_url_to_https() -> Result<(), ParseError> {
resolved_url.as_str(), let resolved_url = utils::resolve_url(
"https://www.kernel.org/category/signatures.html" "data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h",
); "https://www.kernel.org/category/signatures.html",
)?;
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "https://www.kernel.org/category/signatures.html"
fn passing_from_data_url_to_data_url() -> Result<(), ParseError> { );
let resolved_url = utils::resolve_url(
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h", Ok(())
"data:text/html;base64,PGEgaHJlZj0iaW5kZXguaHRtbCI+SG9tZTwvYT4K", }
)?;
#[test]
assert_eq!( fn from_data_url_to_data_url() -> Result<(), ParseError> {
resolved_url.as_str(), let resolved_url = utils::resolve_url(
"data:text/html;base64,PGEgaHJlZj0iaW5kZXguaHRtbCI+SG9tZTwvYT4K" "data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h",
); "data:text/html;base64,PGEgaHJlZj0iaW5kZXguaHRtbCI+SG9tZTwvYT4K",
)?;
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "data:text/html;base64,PGEgaHJlZj0iaW5kZXguaHRtbCI+SG9tZTwvYT4K"
fn passing_from_file_url_to_relative_path() -> Result<(), ParseError> { );
let resolved_url = utils::resolve_url(
"file:///home/user/Websites/my-website/index.html", Ok(())
"assets/images/logo.png", }
)
.unwrap_or(str!()); #[test]
fn from_file_url_to_relative_path() -> Result<(), ParseError> {
assert_eq!( let resolved_url = utils::resolve_url(
resolved_url.as_str(), "file:///home/user/Websites/my-website/index.html",
"file:///home/user/Websites/my-website/assets/images/logo.png" "assets/images/logo.png",
); )
.unwrap_or(str!());
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "file:///home/user/Websites/my-website/assets/images/logo.png"
fn passing_from_file_url_to_relative_path_with_backslashes() -> Result<(), ParseError> { );
let resolved_url = utils::resolve_url(
"file:\\\\\\home\\user\\Websites\\my-website\\index.html", Ok(())
"assets\\images\\logo.png", }
)
.unwrap_or(str!()); #[test]
fn from_file_url_to_relative_path_with_backslashes() -> Result<(), ParseError> {
assert_eq!( let resolved_url = utils::resolve_url(
resolved_url.as_str(), "file:\\\\\\home\\user\\Websites\\my-website\\index.html",
"file:///home/user/Websites/my-website/assets/images/logo.png" "assets\\images\\logo.png",
); )
.unwrap_or(str!());
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "file:///home/user/Websites/my-website/assets/images/logo.png"
fn passing_from_data_url_to_file_url() -> Result<(), ParseError> { );
let resolved_url = utils::resolve_url(
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h", Ok(())
"file:///etc/passwd", }
)
.unwrap_or(str!()); #[test]
fn from_data_url_to_file_url() -> Result<(), ParseError> {
assert_eq!(resolved_url.as_str(), "file:///etc/passwd"); let resolved_url = utils::resolve_url(
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h",
Ok(()) "file:///etc/passwd",
} )
.unwrap_or(str!());
#[test]
fn passing_preserve_fragment() -> Result<(), ParseError> { assert_eq!(resolved_url.as_str(), "file:///etc/passwd");
let resolved_url = utils::resolve_url(
"http://doesnt-matter.local/", Ok(())
"css/fonts/fontmarvelous.svg#fontmarvelous", }
)
.unwrap_or(str!()); #[test]
fn preserve_fragment() -> Result<(), ParseError> {
assert_eq!( let resolved_url = utils::resolve_url(
resolved_url.as_str(), "http://doesnt-matter.local/",
"http://doesnt-matter.local/css/fonts/fontmarvelous.svg#fontmarvelous" "css/fonts/fontmarvelous.svg#fontmarvelous",
); )
.unwrap_or(str!());
Ok(())
} assert_eq!(
resolved_url.as_str(),
#[test] "http://doesnt-matter.local/css/fonts/fontmarvelous.svg#fontmarvelous"
fn passing_resolve_from_file_url_to_file_url() -> Result<(), ParseError> { );
let resolved_url = if cfg!(windows) {
utils::resolve_url("file:///c:/index.html", "file:///c:/image.png").unwrap_or(str!()) Ok(())
} else { }
utils::resolve_url("file:///tmp/index.html", "file:///tmp/image.png").unwrap_or(str!())
}; #[test]
fn resolve_from_file_url_to_file_url() -> Result<(), ParseError> {
assert_eq!( let resolved_url = if cfg!(windows) {
resolved_url.as_str(), utils::resolve_url("file:///c:/index.html", "file:///c:/image.png").unwrap_or(str!())
if cfg!(windows) {
"file:///c:/image.png"
} else { } else {
"file:///tmp/image.png" utils::resolve_url("file:///tmp/index.html", "file:///tmp/image.png").unwrap_or(str!())
} };
);
assert_eq!(
Ok(()) resolved_url.as_str(),
if cfg!(windows) {
"file:///c:/image.png"
} else {
"file:///tmp/image.png"
}
);
Ok(())
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -215,15 +218,21 @@ fn passing_resolve_from_file_url_to_file_url() -> Result<(), ParseError> {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_from_data_url_to_url_with_no_protocol() -> Result<(), ParseError> { mod failing {
let resolved_url = utils::resolve_url( use crate::utils;
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h", use url::ParseError;
"//www.w3schools.com/html/html_iframe.asp",
) #[test]
.unwrap_or(str!()); fn from_data_url_to_url_with_no_protocol() -> Result<(), ParseError> {
let resolved_url = utils::resolve_url(
"data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h",
"//www.w3schools.com/html/html_iframe.asp",
)
.unwrap_or(str!());
assert_eq!(resolved_url.as_str(), ""); assert_eq!(resolved_url.as_str(), "");
Ok(()) Ok(())
}
} }

@ -1,8 +1,3 @@
use crate::utils;
use reqwest::blocking::Client;
use std::collections::HashMap;
use std::env;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -10,66 +5,74 @@ use std::env;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_read_data_url() { mod passing {
let cache = &mut HashMap::new(); use crate::utils;
let client = Client::new(); use reqwest::blocking::Client;
use std::collections::HashMap;
use std::env;
// If both source and target are data URLs, #[test]
// ensure the result contains target data URL fn read_data_url() {
let (data, final_url, media_type) = utils::retrieve_asset( let cache = &mut HashMap::new();
cache, let client = Client::new();
&client,
"data:text/html;base64,c291cmNl", // If both source and target are data URLs,
"data:text/html;base64,dGFyZ2V0", // ensure the result contains target data URL
false, let (data, final_url, media_type) = utils::retrieve_asset(
) cache,
.unwrap(); &client,
assert_eq!( "data:text/html;base64,c291cmNl",
utils::data_to_data_url(&media_type, &data, &final_url, ""), "data:text/html;base64,dGFyZ2V0",
utils::data_to_data_url("text/html", "target".as_bytes(), "", "") false,
); )
assert_eq!( .unwrap();
final_url, assert_eq!(
utils::data_to_data_url("text/html", "target".as_bytes(), "", "") utils::data_to_data_url(&media_type, &data, &final_url, ""),
); utils::data_to_data_url("text/html", "target".as_bytes(), "", "")
assert_eq!(&media_type, "text/html"); );
} assert_eq!(
final_url,
utils::data_to_data_url("text/html", "target".as_bytes(), "", "")
);
assert_eq!(&media_type, "text/html");
}
#[test] #[test]
fn passing_read_local_file_with_file_url_parent() { fn read_local_file_with_file_url_parent() {
let cache = &mut HashMap::new(); let cache = &mut HashMap::new();
let client = Client::new(); let client = Client::new();
let file_url_protocol: &str = if cfg!(windows) { "file:///" } else { "file://" }; let file_url_protocol: &str = if cfg!(windows) { "file:///" } else { "file://" };
// Inclusion of local assets from local sources should be allowed // Inclusion of local assets from local sources should be allowed
let cwd = env::current_dir().unwrap(); let cwd = env::current_dir().unwrap();
let (data, final_url, _media_type) = utils::retrieve_asset( let (data, final_url, _media_type) = utils::retrieve_asset(
cache, cache,
&client, &client,
&format!( &format!(
"{file}{cwd}/src/tests/data/basic/local-file.html", "{file}{cwd}/src/tests/data/basic/local-file.html",
file = file_url_protocol, file = file_url_protocol,
cwd = cwd.to_str().unwrap() cwd = cwd.to_str().unwrap()
), ),
&format!( &format!(
"{file}{cwd}/src/tests/data/basic/local-script.js", "{file}{cwd}/src/tests/data/basic/local-script.js",
file = file_url_protocol, file = file_url_protocol,
cwd = cwd.to_str().unwrap() cwd = cwd.to_str().unwrap()
), ),
false, false,
)
.unwrap();
assert_eq!(utils::data_to_data_url("application/javascript", &data, &final_url, ""), "data:application/javascript;base64,ZG9jdW1lbnQuYm9keS5zdHlsZS5iYWNrZ3JvdW5kQ29sb3IgPSAiZ3JlZW4iOwpkb2N1bWVudC5ib2R5LnN0eWxlLmNvbG9yID0gInJlZCI7Cg==");
assert_eq!(
&final_url,
&format!(
"{file}{cwd}/src/tests/data/basic/local-script.js",
file = file_url_protocol,
cwd = cwd.to_str().unwrap()
) )
); .unwrap();
assert_eq!(utils::data_to_data_url("application/javascript", &data, &final_url, ""), "data:application/javascript;base64,ZG9jdW1lbnQuYm9keS5zdHlsZS5iYWNrZ3JvdW5kQ29sb3IgPSAiZ3JlZW4iOwpkb2N1bWVudC5ib2R5LnN0eWxlLmNvbG9yID0gInJlZCI7Cg==");
assert_eq!(
&final_url,
&format!(
"{file}{cwd}/src/tests/data/basic/local-script.js",
file = file_url_protocol,
cwd = cwd.to_str().unwrap()
)
);
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -79,46 +82,53 @@ fn passing_read_local_file_with_file_url_parent() {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_read_local_file_with_data_url_parent() { mod failing {
let cache = &mut HashMap::new(); use crate::utils;
let client = Client::new(); use reqwest::blocking::Client;
use std::collections::HashMap;
// Inclusion of local assets from data URL sources should not be allowed #[test]
match utils::retrieve_asset( fn read_local_file_with_data_url_parent() {
cache, let cache = &mut HashMap::new();
&client, let client = Client::new();
"data:text/html;base64,SoUrCe",
"file:///etc/passwd", // Inclusion of local assets from data URL sources should not be allowed
false, match utils::retrieve_asset(
) { cache,
Ok((..)) => { &client,
assert!(false); "data:text/html;base64,SoUrCe",
} "file:///etc/passwd",
Err(_) => { false,
assert!(true); ) {
Ok((..)) => {
assert!(false);
}
Err(_) => {
assert!(true);
}
} }
} }
}
#[test] #[test]
fn failing_read_local_file_with_https_parent() { fn read_local_file_with_https_parent() {
let cache = &mut HashMap::new(); let cache = &mut HashMap::new();
let client = Client::new(); let client = Client::new();
// Inclusion of local assets from remote sources should not be allowed // Inclusion of local assets from remote sources should not be allowed
match utils::retrieve_asset( match utils::retrieve_asset(
cache, cache,
&client, &client,
"https://kernel.org/", "https://kernel.org/",
"file:///etc/passwd", "file:///etc/passwd",
false, false,
) { ) {
Ok((..)) => { Ok((..)) => {
assert!(false); assert!(false);
} }
Err(_) => { Err(_) => {
assert!(true); assert!(true);
}
} }
} }
} }

@ -1,5 +1,3 @@
use crate::utils;
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
@ -7,50 +5,55 @@ use crate::utils;
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn passing_mailto() { mod passing {
assert!(utils::url_has_protocol( use crate::utils;
"mailto:somebody@somewhere.com?subject=hello"
));
}
#[test] #[test]
fn passing_tel() { fn mailto() {
assert!(utils::url_has_protocol("tel:5551234567")); assert!(utils::url_has_protocol(
} "mailto:somebody@somewhere.com?subject=hello"
));
}
#[test] #[test]
fn passing_ftp_no_slashes() { fn tel() {
assert!(utils::url_has_protocol("ftp:some-ftp-server.com")); assert!(utils::url_has_protocol("tel:5551234567"));
} }
#[test] #[test]
fn passing_ftp_with_credentials() { fn ftp_no_slashes() {
assert!(utils::url_has_protocol( assert!(utils::url_has_protocol("ftp:some-ftp-server.com"));
"ftp://user:password@some-ftp-server.com" }
));
}
#[test] #[test]
fn passing_javascript() { fn ftp_with_credentials() {
assert!(utils::url_has_protocol("javascript:void(0)")); assert!(utils::url_has_protocol(
} "ftp://user:password@some-ftp-server.com"
));
}
#[test] #[test]
fn passing_http() { fn javascript() {
assert!(utils::url_has_protocol("http://news.ycombinator.com")); assert!(utils::url_has_protocol("javascript:void(0)"));
} }
#[test] #[test]
fn passing_https() { fn http() {
assert!(utils::url_has_protocol("https://github.com")); assert!(utils::url_has_protocol("http://news.ycombinator.com"));
} }
#[test]
fn https() {
assert!(utils::url_has_protocol("https://github.com"));
}
#[test] #[test]
fn passing_mailto_uppercase() { fn mailto_uppercase() {
assert!(utils::url_has_protocol( assert!(utils::url_has_protocol(
"MAILTO:somebody@somewhere.com?subject=hello" "MAILTO:somebody@somewhere.com?subject=hello"
)); ));
}
} }
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗ // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
@ -60,24 +63,29 @@ fn passing_mailto_uppercase() {
// ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝ // ██║ ██║ ██║██║███████╗██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ // ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[test] #[cfg(test)]
fn failing_url_with_no_protocol() { mod failing {
assert!(!utils::url_has_protocol( use crate::utils;
"//some-hostname.com/some-file.html"
));
}
#[test] #[test]
fn failing_relative_path() { fn url_with_no_protocol() {
assert!(!utils::url_has_protocol("some-hostname.com/some-file.html")); assert!(!utils::url_has_protocol(
} "//some-hostname.com/some-file.html"
));
}
#[test] #[test]
fn failing_relative_to_root_path() { fn relative_path() {
assert!(!utils::url_has_protocol("/some-file.html")); assert!(!utils::url_has_protocol("some-hostname.com/some-file.html"));
} }
#[test]
fn relative_to_root_path() {
assert!(!utils::url_has_protocol("/some-file.html"));
}
#[test] #[test]
fn failing_empty_string() { fn empty_string() {
assert!(!utils::url_has_protocol("")); assert!(!utils::url_has_protocol(""));
}
} }

Loading…
Cancel
Save