1 //  ██████╗  █████╗ ███████╗███████╗██╗███╗   ██╗ ██████╗
2 //  ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗  ██║██╔════╝
3 //  ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║  ███╗
4 //  ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║   ██║
5 //  ██║     ██║  ██║███████║███████║██║██║ ╚████║╚██████╔╝
6 //  ╚═╝     ╚═╝  ╚═╝╚══════╝╚══════╝╚═╝╚═╝  ╚═══╝ ╚═════╝
7 
8 #[cfg(test)]
9 mod passing {
10     use assert_cmd::prelude::*;
11     use std::env;
12     use std::fs;
13     use std::path::Path;
14     use std::process::Command;
15     use url::Url;
16 
17     #[test]
parse_noscript_contents()18     fn parse_noscript_contents() {
19         let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
20         let path_html: &Path = Path::new("src/tests/data/noscript/index.html");
21         let path_svg: &Path = Path::new("src/tests/data/noscript/image.svg");
22 
23         let out = cmd.arg("-M").arg(path_html.as_os_str()).output().unwrap();
24 
25         // STDERR should contain target HTML and embedded SVG files
26         assert_eq!(
27             String::from_utf8_lossy(&out.stderr),
28             format!(
29                 "\
30                 {file_url_html}\n \
31                 {file_url_svg}\n\
32                 ",
33                 file_url_html = Url::from_file_path(fs::canonicalize(&path_html).unwrap()).unwrap(),
34                 file_url_svg = Url::from_file_path(fs::canonicalize(&path_svg).unwrap()).unwrap(),
35             )
36         );
37 
38         // STDOUT should contain HTML with no CSS
39         assert_eq!(
40             String::from_utf8_lossy(&out.stdout),
41             "<html><head></head><body><noscript><img src=\"data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGJhc2VQcm9maWxlPSJmdWxsIiB3aWR0aD0iMzAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InJlZCIgLz4KICAgIDxjaXJjbGUgY3g9IjE1MCIgY3k9IjEwMCIgcj0iODAiIGZpbGw9ImdyZWVuIiAvPgogICAgPHRleHQgeD0iMTUwIiB5PSIxMjUiIGZvbnQtc2l6ZT0iNjAiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj5TVkc8L3RleHQ+Cjwvc3ZnPgo=\"></noscript>\n</body></html>\n"
42         );
43 
44         // Exit code should be 0
45         out.assert().code(0);
46     }
47 
48     #[test]
unwrap_noscript_contents()49     fn unwrap_noscript_contents() {
50         let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
51         let path_html: &Path = Path::new("src/tests/data/noscript/index.html");
52         let path_svg: &Path = Path::new("src/tests/data/noscript/image.svg");
53 
54         let out = cmd.arg("-Mn").arg(path_html.as_os_str()).output().unwrap();
55 
56         // STDERR should contain target HTML and embedded SVG files
57         assert_eq!(
58             String::from_utf8_lossy(&out.stderr),
59             format!(
60                 "\
61                 {file_url_html}\n \
62                 {file_url_svg}\n\
63                 ",
64                 file_url_html = Url::from_file_path(fs::canonicalize(&path_html).unwrap()).unwrap(),
65                 file_url_svg = Url::from_file_path(fs::canonicalize(&path_svg).unwrap()).unwrap(),
66             )
67         );
68 
69         // STDOUT should contain HTML with no CSS
70         assert_eq!(
71             String::from_utf8_lossy(&out.stdout),
72             "<html><head></head><body><!--noscript--><img src=\"data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGJhc2VQcm9maWxlPSJmdWxsIiB3aWR0aD0iMzAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InJlZCIgLz4KICAgIDxjaXJjbGUgY3g9IjE1MCIgY3k9IjEwMCIgcj0iODAiIGZpbGw9ImdyZWVuIiAvPgogICAgPHRleHQgeD0iMTUwIiB5PSIxMjUiIGZvbnQtc2l6ZT0iNjAiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj5TVkc8L3RleHQ+Cjwvc3ZnPgo=\"><!--/noscript-->\n</body></html>\n"
73         );
74 
75         // Exit code should be 0
76         out.assert().code(0);
77     }
78 
79     #[test]
unwrap_noscript_contents_nested()80     fn unwrap_noscript_contents_nested() {
81         let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
82         let path_html: &Path = Path::new("src/tests/data/noscript/nested.html");
83         let path_svg: &Path = Path::new("src/tests/data/noscript/image.svg");
84 
85         let out = cmd.arg("-Mn").arg(path_html.as_os_str()).output().unwrap();
86 
87         // STDERR should contain target HTML and embedded SVG files
88         assert_eq!(
89             String::from_utf8_lossy(&out.stderr),
90             format!(
91                 "\
92                 {file_url_html}\n \
93                 {file_url_svg}\n\
94                 ",
95                 file_url_html = Url::from_file_path(fs::canonicalize(&path_html).unwrap()).unwrap(),
96                 file_url_svg = Url::from_file_path(fs::canonicalize(&path_svg).unwrap()).unwrap(),
97             )
98         );
99 
100         // STDOUT should contain HTML with no CSS
101         assert_eq!(
102             String::from_utf8_lossy(&out.stdout),
103             "<html><head></head><body><!--noscript--><h1>JS is not active</h1><!--noscript--><img src=\"data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGJhc2VQcm9maWxlPSJmdWxsIiB3aWR0aD0iMzAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InJlZCIgLz4KICAgIDxjaXJjbGUgY3g9IjE1MCIgY3k9IjEwMCIgcj0iODAiIGZpbGw9ImdyZWVuIiAvPgogICAgPHRleHQgeD0iMTUwIiB5PSIxMjUiIGZvbnQtc2l6ZT0iNjAiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj5TVkc8L3RleHQ+Cjwvc3ZnPgo=\"><!--/noscript--><!--/noscript-->\n</body></html>\n"
104         );
105 
106         // Exit code should be 0
107         out.assert().code(0);
108     }
109 
110     #[test]
unwrap_noscript_contents_with_script()111     fn unwrap_noscript_contents_with_script() {
112         let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
113         let path_html: &Path = Path::new("src/tests/data/noscript/script.html");
114         let path_svg: &Path = Path::new("src/tests/data/noscript/image.svg");
115 
116         let out = cmd.arg("-Mn").arg(path_html.as_os_str()).output().unwrap();
117 
118         // STDERR should contain target HTML and embedded SVG files
119         assert_eq!(
120             String::from_utf8_lossy(&out.stderr),
121             format!(
122                 "\
123                 {file_url_html}\n \
124                 {file_url_svg}\n\
125                 ",
126                 file_url_html = Url::from_file_path(fs::canonicalize(&path_html).unwrap()).unwrap(),
127                 file_url_svg = Url::from_file_path(fs::canonicalize(&path_svg).unwrap()).unwrap(),
128             )
129         );
130 
131         // STDOUT should contain HTML with no CSS
132         assert_eq!(
133             String::from_utf8_lossy(&out.stdout),
134             "<html>\
135                 <head></head>\
136                 <body>\
137                     <!--noscript-->\
138                     <img src=\"data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGJhc2VQcm9maWxlPSJmdWxsIiB3aWR0aD0iMzAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InJlZCIgLz4KICAgIDxjaXJjbGUgY3g9IjE1MCIgY3k9IjEwMCIgcj0iODAiIGZpbGw9ImdyZWVuIiAvPgogICAgPHRleHQgeD0iMTUwIiB5PSIxMjUiIGZvbnQtc2l6ZT0iNjAiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj5TVkc8L3RleHQ+Cjwvc3ZnPgo=\">\
139                     <!--/noscript-->\n\
140                 </body>\
141             </html>\n"
142         );
143 
144         // Exit code should be 0
145         out.assert().code(0);
146     }
147 
148     #[test]
unwrap_noscript_contents_attr_data_url()149     fn unwrap_noscript_contents_attr_data_url() {
150         let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
151         let out = cmd
152             .arg("-M")
153             .arg("-n")
154             .arg("data:text/html,<noscript class=\"\">test</noscript>")
155             .output()
156             .unwrap();
157 
158         // STDERR should be empty
159         assert_eq!(String::from_utf8_lossy(&out.stderr), "");
160 
161         // STDOUT should contain unwrapped contents of NOSCRIPT element
162         assert_eq!(
163             String::from_utf8_lossy(&out.stdout),
164             "<html><head><!--noscript class=\"\"-->test<!--/noscript--></head><body></body></html>\n"
165         );
166 
167         // Exit code should be 0
168         out.assert().code(0);
169     }
170 }
171