// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ // ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ // ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ // ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║ // ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ // ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ #[cfg(test)] mod passing { use html5ever::serialize::{serialize, SerializeOpts}; use markup5ever_rcdom::SerializableHandle; use reqwest::blocking::Client; use std::collections::HashMap; use url::Url; use monolith::html; use monolith::opts::Options; use monolith::url::EMPTY_IMAGE_DATA_URL; #[test] fn basic() { let cache = &mut HashMap::new(); let html: &str = "

"; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let mut options = Options::default(); options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "

" ); } #[test] fn ensure_no_recursive_iframe() { let html = "

"; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "

" ); } #[test] fn ensure_no_recursive_frame() { let html = ""; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "" ); } #[test] fn no_css() { let html = "\ \ \ \
\ "; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_css = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ \ \ \ \ \
\ \ \ " ); } #[test] fn no_images() { let html = "\
"; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_images = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), format!( "\ \ \ \ \
\ \
\ \ ", empty_image = EMPTY_IMAGE_DATA_URL ) ); } #[test] fn no_body_background_images() { let html = ""; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_images = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "" ); } #[test] fn no_frames() { let html = ""; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_frames = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ \ \ \ \ \ " ); } #[test] fn no_iframes() { let html = ""; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_frames = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ \ \ \ \ " ); } #[test] fn no_js() { let html = "\
\ \ \
\ "; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_js = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ \
\ \ \
\ \ \ " ); } #[test] fn keeps_integrity_for_unfamiliar_links() { let html = "Has integrity\ "; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ Has integrity\ \ \ \ \ " ); } #[test] fn discards_integrity_for_known_links_nojs_nocss() { let html = "\ No integrity\ \ \ "; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_css = true; options.no_js = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ No integrity\ \ \ \ \ \ " ); } #[test] fn discards_integrity_for_embedded_assets() { let html = "\ No integrity\ \ \ "; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_css = true; options.no_js = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ No integrity\ \ \ \ \ \ \ " ); } #[test] fn removes_unwanted_meta_tags() { let html = "\ \ \ \ \ \ \ \ \ "; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_css = true; options.no_frames = true; options.no_js = true; options.no_images = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ \ \ \ \ \ " ); } #[test] fn processes_noscript_tags() { let html = "\ \ \ \ \ "; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.no_images = true; options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), format!( "\ \ \ \ \ \ \ ", EMPTY_IMAGE_DATA_URL, ) ); } #[test] fn preserves_script_type_json() { let html = ""; let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string()); let url: Url = Url::parse("http://localhost").unwrap(); let cache = &mut HashMap::new(); let mut options = Options::default(); options.silent = true; let client = Client::new(); html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0); let mut buf: Vec = Vec::new(); serialize( &mut buf, &SerializableHandle::from(dom.document.clone()), SerializeOpts::default(), ) .unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ \ \ \ \ " ); } }