2021-03-12 05:18:39 +01:00
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[ cfg(test) ]
mod passing {
use assert_cmd ::prelude ::* ;
use std ::env ;
use std ::fs ;
use std ::path ::Path ;
use std ::process ::Command ;
use url ::Url ;
#[ test ]
2021-03-16 07:10:50 +01:00
fn parse_noscript_contents ( ) {
let mut cmd = Command ::cargo_bin ( env! ( " CARGO_PKG_NAME " ) ) . unwrap ( ) ;
2021-10-18 10:46:06 +02:00
let path_html : & Path = Path ::new ( " tests/_data_/noscript/index.html " ) ;
let path_svg : & Path = Path ::new ( " tests/_data_/noscript/image.svg " ) ;
2021-03-12 05:18:39 +01:00
let out = cmd . arg ( " -M " ) . arg ( path_html . as_os_str ( ) ) . output ( ) . unwrap ( ) ;
// STDERR should contain target HTML and embedded SVG files
assert_eq! (
2021-06-08 14:30:15 +02:00
String ::from_utf8_lossy ( & out . stderr ) ,
2021-03-12 05:18:39 +01:00
format! (
" \
{ file_url_html } \ n \
{ file_url_svg } \ n \
" ,
2021-06-02 15:58:28 +02:00
file_url_html = Url ::from_file_path ( fs ::canonicalize ( & path_html ) . unwrap ( ) ) . unwrap ( ) ,
file_url_svg = Url ::from_file_path ( fs ::canonicalize ( & path_svg ) . unwrap ( ) ) . unwrap ( ) ,
2021-03-12 05:18:39 +01:00
)
) ;
2021-06-08 14:30:15 +02:00
// STDOUT should contain HTML with no CSS
assert_eq! (
String ::from_utf8_lossy ( & out . stdout ) ,
" <html><head></head><body><noscript><img src= \" data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGJhc2VQcm9maWxlPSJmdWxsIiB3aWR0aD0iMzAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InJlZCIgLz4KICAgIDxjaXJjbGUgY3g9IjE1MCIgY3k9IjEwMCIgcj0iODAiIGZpbGw9ImdyZWVuIiAvPgogICAgPHRleHQgeD0iMTUwIiB5PSIxMjUiIGZvbnQtc2l6ZT0iNjAiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj5TVkc8L3RleHQ+Cjwvc3ZnPgo= \" ></noscript> \n </body></html> \n "
) ;
// Exit code should be 0
2021-03-12 05:18:39 +01:00
out . assert ( ) . code ( 0 ) ;
}
#[ test ]
2021-03-16 07:10:50 +01:00
fn unwrap_noscript_contents ( ) {
let mut cmd = Command ::cargo_bin ( env! ( " CARGO_PKG_NAME " ) ) . unwrap ( ) ;
2021-10-18 10:46:06 +02:00
let path_html : & Path = Path ::new ( " tests/_data_/noscript/index.html " ) ;
let path_svg : & Path = Path ::new ( " tests/_data_/noscript/image.svg " ) ;
2021-03-12 05:18:39 +01:00
let out = cmd . arg ( " -Mn " ) . arg ( path_html . as_os_str ( ) ) . output ( ) . unwrap ( ) ;
// STDERR should contain target HTML and embedded SVG files
assert_eq! (
2021-06-08 14:30:15 +02:00
String ::from_utf8_lossy ( & out . stderr ) ,
2021-03-12 05:18:39 +01:00
format! (
" \
{ file_url_html } \ n \
{ file_url_svg } \ n \
" ,
2021-06-02 15:58:28 +02:00
file_url_html = Url ::from_file_path ( fs ::canonicalize ( & path_html ) . unwrap ( ) ) . unwrap ( ) ,
file_url_svg = Url ::from_file_path ( fs ::canonicalize ( & path_svg ) . unwrap ( ) ) . unwrap ( ) ,
2021-03-12 05:18:39 +01:00
)
) ;
2021-06-08 14:30:15 +02:00
// STDOUT should contain HTML with no CSS
assert_eq! (
String ::from_utf8_lossy ( & out . stdout ) ,
" <html><head></head><body><!--noscript--><img src= \" data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGJhc2VQcm9maWxlPSJmdWxsIiB3aWR0aD0iMzAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InJlZCIgLz4KICAgIDxjaXJjbGUgY3g9IjE1MCIgY3k9IjEwMCIgcj0iODAiIGZpbGw9ImdyZWVuIiAvPgogICAgPHRleHQgeD0iMTUwIiB5PSIxMjUiIGZvbnQtc2l6ZT0iNjAiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj5TVkc8L3RleHQ+Cjwvc3ZnPgo= \" ><!--/noscript--> \n </body></html> \n "
) ;
// Exit code should be 0
2021-03-12 05:18:39 +01:00
out . assert ( ) . code ( 0 ) ;
}
#[ test ]
2021-03-16 07:10:50 +01:00
fn unwrap_noscript_contents_nested ( ) {
let mut cmd = Command ::cargo_bin ( env! ( " CARGO_PKG_NAME " ) ) . unwrap ( ) ;
2021-10-18 10:46:06 +02:00
let path_html : & Path = Path ::new ( " tests/_data_/noscript/nested.html " ) ;
let path_svg : & Path = Path ::new ( " tests/_data_/noscript/image.svg " ) ;
2021-03-12 05:18:39 +01:00
let out = cmd . arg ( " -Mn " ) . arg ( path_html . as_os_str ( ) ) . output ( ) . unwrap ( ) ;
// STDERR should contain target HTML and embedded SVG files
assert_eq! (
2021-06-08 14:30:15 +02:00
String ::from_utf8_lossy ( & out . stderr ) ,
2021-03-12 05:18:39 +01:00
format! (
" \
{ file_url_html } \ n \
{ file_url_svg } \ n \
" ,
2021-06-02 15:58:28 +02:00
file_url_html = Url ::from_file_path ( fs ::canonicalize ( & path_html ) . unwrap ( ) ) . unwrap ( ) ,
file_url_svg = Url ::from_file_path ( fs ::canonicalize ( & path_svg ) . unwrap ( ) ) . unwrap ( ) ,
2021-03-12 05:18:39 +01:00
)
) ;
2021-06-08 14:30:15 +02:00
// STDOUT should contain HTML with no CSS
assert_eq! (
String ::from_utf8_lossy ( & out . stdout ) ,
" <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 "
) ;
// Exit code should be 0
2021-03-12 05:18:39 +01:00
out . assert ( ) . code ( 0 ) ;
}
#[ test ]
2021-03-16 07:10:50 +01:00
fn unwrap_noscript_contents_with_script ( ) {
let mut cmd = Command ::cargo_bin ( env! ( " CARGO_PKG_NAME " ) ) . unwrap ( ) ;
2021-10-18 10:46:06 +02:00
let path_html : & Path = Path ::new ( " tests/_data_/noscript/script.html " ) ;
let path_svg : & Path = Path ::new ( " tests/_data_/noscript/image.svg " ) ;
2021-03-12 05:18:39 +01:00
let out = cmd . arg ( " -Mn " ) . arg ( path_html . as_os_str ( ) ) . output ( ) . unwrap ( ) ;
// STDERR should contain target HTML and embedded SVG files
assert_eq! (
2021-06-08 14:30:15 +02:00
String ::from_utf8_lossy ( & out . stderr ) ,
2021-03-12 05:18:39 +01:00
format! (
" \
{ file_url_html } \ n \
{ file_url_svg } \ n \
" ,
2021-06-02 15:58:28 +02:00
file_url_html = Url ::from_file_path ( fs ::canonicalize ( & path_html ) . unwrap ( ) ) . unwrap ( ) ,
file_url_svg = Url ::from_file_path ( fs ::canonicalize ( & path_svg ) . unwrap ( ) ) . unwrap ( ) ,
2021-03-12 05:18:39 +01:00
)
) ;
2021-06-08 14:30:15 +02:00
// STDOUT should contain HTML with no CSS
assert_eq! (
String ::from_utf8_lossy ( & out . stdout ) ,
" <html> \
< head > < / head > \
< body > \
< ! - - noscript - ->\
< img src = \ " data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGJhc2VQcm9maWxlPSJmdWxsIiB3aWR0aD0iMzAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InJlZCIgLz4KICAgIDxjaXJjbGUgY3g9IjE1MCIgY3k9IjEwMCIgcj0iODAiIGZpbGw9ImdyZWVuIiAvPgogICAgPHRleHQgeD0iMTUwIiB5PSIxMjUiIGZvbnQtc2l6ZT0iNjAiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj5TVkc8L3RleHQ+Cjwvc3ZnPgo= \" > \
< ! - - / noscript - ->\ n \
< / body > \
< / html > \ n "
) ;
// Exit code should be 0
2021-03-12 05:18:39 +01:00
out . assert ( ) . code ( 0 ) ;
}
2021-06-02 15:41:41 +02:00
#[ test ]
fn unwrap_noscript_contents_attr_data_url ( ) {
let mut cmd = Command ::cargo_bin ( env! ( " CARGO_PKG_NAME " ) ) . unwrap ( ) ;
let out = cmd
. arg ( " -M " )
. arg ( " -n " )
. arg ( " data:text/html,<noscript class= \" \" >test</noscript> " )
. output ( )
. unwrap ( ) ;
2021-06-08 14:30:15 +02:00
// STDERR should be empty
assert_eq! ( String ::from_utf8_lossy ( & out . stderr ) , " " ) ;
2021-06-02 15:41:41 +02:00
// STDOUT should contain unwrapped contents of NOSCRIPT element
assert_eq! (
2021-06-08 14:30:15 +02:00
String ::from_utf8_lossy ( & out . stdout ) ,
2021-06-02 15:41:41 +02:00
" <html><head><!--noscript class= \" \" -->test<!--/noscript--></head><body></body></html> \n "
) ;
2021-06-08 14:30:15 +02:00
// Exit code should be 0
2021-06-02 15:41:41 +02:00
out . assert ( ) . code ( 0 ) ;
}
2021-03-12 05:18:39 +01:00
}