monolith/src/tests/html/walk_and_embed_assets.rs

420 lines
11 KiB
Rust
Raw Normal View History

use crate::html;
2019-09-29 23:15:49 +02:00
use html5ever::serialize::{serialize, SerializeOpts};
use reqwest::blocking::Client;
use std::collections::HashMap;
2019-09-29 23:15:49 +02:00
2020-03-23 03:08:41 +01:00
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
2019-09-29 23:15:49 +02:00
#[test]
2020-03-23 03:08:41 +01:00
fn passing_basic() {
let cache = &mut HashMap::new();
2019-09-29 23:15:49 +02:00
let html = "<div><P></P></div>";
let dom = html::html_to_dom(&html);
2019-09-29 23:15:49 +02:00
let url = "http://localhost";
let opt_no_css: bool = false;
2020-04-22 09:37:02 +02:00
let opt_no_fonts: bool = false;
2019-09-29 23:15:49 +02:00
let opt_no_frames: bool = false;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_silent = true;
2019-12-10 05:48:46 +01:00
let client = Client::new();
2019-09-29 23:15:49 +02:00
html::walk_and_embed_assets(
cache,
2019-12-10 05:48:46 +01:00
&client,
2019-09-29 23:15:49 +02:00
&url,
&dom.document,
opt_no_css,
2020-04-22 09:37:02 +02:00
opt_no_fonts,
opt_no_frames,
2019-09-29 23:15:49 +02:00
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></head><body><div><p></p></div></body></html>"
);
}
#[test]
2020-03-23 03:08:41 +01:00
fn passing_ensure_no_recursive_iframe() {
2019-09-29 23:15:49 +02:00
let html = "<div><P></P><iframe src=\"\"></iframe></div>";
let dom = html::html_to_dom(&html);
2019-09-29 23:15:49 +02:00
let url = "http://localhost";
let cache = &mut HashMap::new();
2019-09-29 23:15:49 +02:00
let opt_no_css: bool = false;
2020-04-22 09:37:02 +02:00
let opt_no_fonts: bool = false;
2019-09-29 23:15:49 +02:00
let opt_no_frames: bool = false;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_silent = true;
2019-12-10 05:48:46 +01:00
let client = Client::new();
2019-09-29 23:15:49 +02:00
html::walk_and_embed_assets(
cache,
2019-12-10 05:48:46 +01:00
&client,
2019-09-29 23:15:49 +02:00
&url,
&dom.document,
opt_no_css,
2020-04-22 09:37:02 +02:00
opt_no_fonts,
opt_no_frames,
2019-09-29 23:15:49 +02:00
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></head><body><div><p></p><iframe src=\"\"></iframe></div></body></html>"
);
}
2020-02-24 06:06:31 +01:00
#[test]
2020-03-23 03:08:41 +01:00
fn passing_ensure_no_recursive_frame() {
2020-02-24 06:06:31 +01:00
let html = "<frameset><frame src=\"\"></frameset>";
let dom = html::html_to_dom(&html);
2020-02-24 06:06:31 +01:00
let url = "http://localhost";
let cache = &mut HashMap::new();
let opt_no_css: bool = false;
2020-04-22 09:37:02 +02:00
let opt_no_fonts: bool = false;
2020-02-24 06:06:31 +01:00
let opt_no_frames: bool = false;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_silent = true;
let client = Client::new();
html::walk_and_embed_assets(
2020-02-24 06:06:31 +01:00
cache,
&client,
&url,
&dom.document,
opt_no_css,
2020-04-22 09:37:02 +02:00
opt_no_fonts,
opt_no_frames,
2020-02-24 06:06:31 +01:00
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></head><frameset><frame src=\"\"></frameset></html>"
);
}
2019-09-29 23:15:49 +02:00
#[test]
2020-03-23 03:08:41 +01:00
fn passing_no_css() {
2019-09-29 23:15:49 +02:00
let html = "<link rel=\"stylesheet\" href=\"main.css\">\
<style>html{background-color: #000;}</style>\
<div style=\"display: none;\"></div>";
let dom = html::html_to_dom(&html);
2019-09-29 23:15:49 +02:00
let url = "http://localhost";
let cache = &mut HashMap::new();
2019-09-29 23:15:49 +02:00
let opt_no_css: bool = true;
2020-04-22 09:37:02 +02:00
let opt_no_fonts: bool = false;
2019-09-29 23:15:49 +02:00
let opt_no_frames: bool = false;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_silent = true;
let client = Client::new();
2019-09-29 23:15:49 +02:00
html::walk_and_embed_assets(
cache,
2019-12-10 05:48:46 +01:00
&client,
2019-09-29 23:15:49 +02:00
&url,
&dom.document,
opt_no_css,
2020-04-22 09:37:02 +02:00
opt_no_fonts,
opt_no_frames,
2019-09-29 23:15:49 +02:00
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>\
<link rel=\"stylesheet\" href=\"\">\
<style></style>\
</head>\
<body>\
<div></div>\
</body>\
</html>"
);
}
#[test]
2020-03-23 03:08:41 +01:00
fn passing_no_images() {
2019-09-29 23:15:49 +02:00
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);
2019-09-29 23:15:49 +02:00
let url = "http://localhost";
let cache = &mut HashMap::new();
2019-09-29 23:15:49 +02:00
let opt_no_css: bool = false;
2020-04-22 09:37:02 +02:00
let opt_no_fonts: bool = false;
2019-09-29 23:15:49 +02:00
let opt_no_frames: bool = false;
let opt_no_js: bool = false;
let opt_no_images: bool = true;
let opt_silent = true;
2019-12-10 05:48:46 +01:00
let client = Client::new();
2019-09-29 23:15:49 +02:00
html::walk_and_embed_assets(
cache,
2019-12-10 05:48:46 +01:00
&client,
2019-09-29 23:15:49 +02:00
&url,
&dom.document,
opt_no_css,
2020-04-22 09:37:02 +02:00
opt_no_fonts,
opt_no_frames,
2019-09-29 23:15:49 +02:00
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>(),
2020-04-03 09:30:52 +02:00
format!(
"<html>\
2019-09-29 23:15:49 +02:00
<head>\
<link rel=\"icon\" href=\"\">\
</head>\
<body>\
<div>\
2020-04-03 09:30:52 +02:00
<img src=\"{empty_image}\">\
2019-09-29 23:15:49 +02:00
</div>\
</body>\
2020-04-03 09:30:52 +02:00
</html>",
empty_image = empty_image!()
)
2019-09-29 23:15:49 +02:00
);
}
#[test]
2020-03-23 03:08:41 +01:00
fn passing_no_body_background_images() {
let html = "<body background=\"no/such/image.png\" background=\"no/such/image2.png\"></body>";
let dom = html::html_to_dom(&html);
let url = "http://localhost";
let cache = &mut HashMap::new();
let opt_no_css: bool = false;
2020-04-22 09:37:02 +02:00
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,
2020-04-22 09:37:02 +02:00
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></head><body></body></html>"
);
}
2019-09-29 23:15:49 +02:00
#[test]
2020-03-23 03:08:41 +01:00
fn passing_no_frames() {
2020-02-24 06:06:31 +01:00
let html = "<frameset><frame src=\"http://trackbook.com\"></frameset>";
let dom = html::html_to_dom(&html);
2020-02-24 06:06:31 +01:00
let url = "http://localhost";
let cache = &mut HashMap::new();
let opt_no_css: bool = false;
2020-04-22 09:37:02 +02:00
let opt_no_fonts: bool = false;
2020-02-24 06:06:31 +01:00
let opt_no_frames: bool = true;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_silent = true;
let client = Client::new();
html::walk_and_embed_assets(
2020-02-24 06:06:31 +01:00
cache,
&client,
&url,
&dom.document,
opt_no_css,
2020-04-22 09:37:02 +02:00
opt_no_fonts,
opt_no_frames,
2020-02-24 06:06:31 +01:00
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></head><frameset><frame src=\"\"></frameset></html>"
);
}
#[test]
2020-03-23 03:08:41 +01:00
fn passing_no_iframes() {
2019-09-29 23:15:49 +02:00
let html = "<iframe src=\"http://trackbook.com\"></iframe>";
let dom = html::html_to_dom(&html);
2019-09-29 23:15:49 +02:00
let url = "http://localhost";
let cache = &mut HashMap::new();
2019-09-29 23:15:49 +02:00
let opt_no_css: bool = false;
2020-04-22 09:37:02 +02:00
let opt_no_fonts: bool = false;
2019-09-29 23:15:49 +02:00
let opt_no_frames: bool = true;
let opt_no_js: bool = false;
let opt_no_images: bool = false;
let opt_silent = true;
let client = Client::new();
2019-09-29 23:15:49 +02:00
html::walk_and_embed_assets(
cache,
2019-12-10 05:48:46 +01:00
&client,
2019-09-29 23:15:49 +02:00
&url,
&dom.document,
opt_no_css,
2020-04-22 09:37:02 +02:00
opt_no_fonts,
opt_no_frames,
2019-09-29 23:15:49 +02:00
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></head><body><iframe src=\"\"></iframe></body></html>"
);
}
#[test]
2020-03-23 03:08:41 +01:00
fn passing_no_js() {
2019-09-29 23:15:49 +02:00
let html = "<div onClick=\"void(0)\">\
<script src=\"http://localhost/assets/some.js\"></script>\
<script>alert(1)</script>\
</div>";
let dom = html::html_to_dom(&html);
2019-09-29 23:15:49 +02:00
let url = "http://localhost";
let cache = &mut HashMap::new();
2019-09-29 23:15:49 +02:00
let opt_no_css: bool = false;
2020-04-22 09:37:02 +02:00
let opt_no_fonts: bool = false;
2019-09-29 23:15:49 +02:00
let opt_no_frames: bool = false;
let opt_no_js: bool = true;
let opt_no_images: bool = false;
let opt_silent = true;
2019-12-10 05:48:46 +01:00
let client = Client::new();
2019-09-29 23:15:49 +02:00
html::walk_and_embed_assets(
cache,
2019-12-10 05:48:46 +01:00
&client,
2019-09-29 23:15:49 +02:00
&url,
&dom.document,
opt_no_css,
2020-04-22 09:37:02 +02:00
opt_no_fonts,
opt_no_frames,
2019-09-29 23:15:49 +02:00
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></head><body><div><script src=\"\"></script>\
<script></script></div></body></html>"
);
}
2019-12-26 15:44:01 +01:00
#[test]
2020-03-23 03:08:41 +01:00
fn passing_with_no_integrity() {
2019-12-26 15:44:01 +01:00
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);
2019-12-26 15:44:01 +01:00
let url = "http://localhost";
let cache = &mut HashMap::new();
let client = Client::new();
2019-12-26 15:44:01 +01:00
let opt_no_css: bool = true;
2020-04-22 09:37:02 +02:00
let opt_no_fonts: bool = false;
2019-12-26 15:44:01 +01:00
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(
2019-12-26 15:44:01 +01:00
cache,
&client,
&url,
&dom.document,
opt_no_css,
2020-04-22 09:37:02 +02:00
opt_no_fonts,
opt_no_frames,
2019-12-26 15:44:01 +01:00
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 src=\"\"></script></head>\
<body></body>\
</html>"
);
}