Skip to content

Commit

Permalink
make tests work again
Browse files Browse the repository at this point in the history
  • Loading branch information
snshn committed May 19, 2024
1 parent c85220b commit 40acdd5
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 129 deletions.
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,10 @@ fn main() {
// Initialize client
let mut cache = HashMap::new();
let mut header_map = HeaderMap::new();
if let Some(user_agent) = &options.user_agent {
header_map.insert(
USER_AGENT,
HeaderValue::from_str(&user_agent).expect("Invalid User-Agent header specified"),
);
}
header_map.insert(
USER_AGENT,
HeaderValue::from_str(&options.user_agent).expect("Invalid User-Agent header specified"),
);
let client = if options.timeout > 0 {
Client::builder().timeout(Duration::from_secs(options.timeout))
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/opts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::Parser;
use clap::{Args, Parser, Subcommand, ValueEnum};
use std::env;

// #[derive(Default)]
Expand Down Expand Up @@ -38,9 +38,9 @@ const ASCII: &'static str = " \
| | \\___/ | | \\ | | | | | | |
|___| |__________| \\_____________________| |___| |___| |___|
";
const DEFAULT_NETWORK_TIMEOUT: u64 = 120;
const DEFAULT_USER_AGENT: &'static str =
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0";
// const DEFAULT_NETWORK_TIMEOUT: u64 = 60;
// const DEFAULT_USER_AGENT: &'static str =
// "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0";

#[derive(Parser, Debug)]
#[command(
Expand Down Expand Up @@ -114,7 +114,7 @@ pub struct Options {
pub unwrap_noscript: bool,

/// Write output to <file>, use - for STDOUT
#[arg(short = 'o', long = "output")]
#[arg(short = 'o', long = "output", default_value = "-")]
pub output: String,

/// Suppress verbosity
Expand All @@ -127,8 +127,8 @@ pub struct Options {
pub timeout: u64,

/// Set custom User-Agent string
#[arg(short = 'u', long = "user-agent")]
pub user_agent: Option<String>,
#[arg(short = 'u', long = "user-agent", default_value = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0")]
pub user_agent: String,

/// Remove video sources
#[arg(short = 'v', long = "no-video")]
Expand Down
67 changes: 41 additions & 26 deletions tests/css/embed_css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

#[cfg(test)]
mod passing {
use clap::Parser;
use reqwest::blocking::Client;
use reqwest::Url;
use std::collections::HashMap;

use monolith::cookies::Cookie;
use monolith::css;
use monolith::opts::Options;
use monolith::url::EMPTY_IMAGE_DATA_URL;
Expand All @@ -19,11 +21,12 @@ mod passing {
fn empty_input() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("data:,").unwrap();
let options = Options::default();
let options = Options::parse();

assert_eq!(
css::embed_css(cache, &client, &document_url, "", &options),
css::embed_css(cache, &client, &document_url, "", &options, &cookies),
""
);
}
Expand All @@ -32,11 +35,12 @@ mod passing {
fn trim_if_empty() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
let options = Options::default();
let options = Options::parse();

assert_eq!(
css::embed_css(cache, &client, &document_url, "\t \t ", &options),
css::embed_css(cache, &client, &document_url, "\t \t ", &options, &cookies),
""
);
}
Expand All @@ -45,8 +49,9 @@ mod passing {
fn style_exclude_unquoted_images() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.no_images = true;
options.silent = true;

Expand All @@ -59,7 +64,7 @@ mod passing {
height: calc(100vh - 10pt)";

assert_eq!(
css::embed_css(cache, &client, &document_url, &STYLE, &options),
css::embed_css(cache, &client, &document_url, &STYLE, &options, &cookies),
format!(
"/* border: none;*/\
background-image: url(\"{empty_image}\"); \
Expand All @@ -77,8 +82,9 @@ mod passing {
fn style_exclude_single_quoted_images() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("data:,").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.no_images = true;
options.silent = true;

Expand All @@ -91,7 +97,7 @@ mod passing {
height: calc(100vh - 10pt)";

assert_eq!(
css::embed_css(cache, &client, &document_url, &STYLE, &options),
css::embed_css(cache, &client, &document_url, &STYLE, &options, &cookies),
format!(
"/* border: none;*/\
background-image: url(\"{empty_image}\"); \
Expand All @@ -109,8 +115,9 @@ mod passing {
fn style_block() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("file:///").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.silent = true;

const CSS: &str = "\
Expand All @@ -122,7 +129,7 @@ mod passing {
html > body {}";

assert_eq!(
css::embed_css(cache, &client, &document_url, &CSS, &options),
css::embed_css(cache, &client, &document_url, &CSS, &options, &cookies),
CSS
);
}
Expand All @@ -131,8 +138,9 @@ mod passing {
fn attribute_selectors() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.silent = true;

const CSS: &str = "\
Expand Down Expand Up @@ -166,7 +174,7 @@ mod passing {
";

assert_eq!(
css::embed_css(cache, &client, &document_url, &CSS, &options),
css::embed_css(cache, &client, &document_url, &CSS, &options, &cookies),
CSS
);
}
Expand All @@ -175,8 +183,9 @@ mod passing {
fn import_string() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.silent = true;

const CSS: &str = "\
Expand All @@ -188,7 +197,7 @@ mod passing {
";

assert_eq!(
css::embed_css(cache, &client, &document_url, &CSS, &options),
css::embed_css(cache, &client, &document_url, &CSS, &options, &cookies),
"\
@charset \"UTF-8\";\n\
\n\
Expand All @@ -203,8 +212,9 @@ mod passing {
fn hash_urls() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.silent = true;

const CSS: &str = "\
Expand All @@ -218,7 +228,7 @@ mod passing {
";

assert_eq!(
css::embed_css(cache, &client, &document_url, &CSS, &options),
css::embed_css(cache, &client, &document_url, &CSS, &options, &cookies),
CSS
);
}
Expand All @@ -227,8 +237,9 @@ mod passing {
fn transform_percentages_and_degrees() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.silent = true;

const CSS: &str = "\
Expand All @@ -240,7 +251,7 @@ mod passing {
";

assert_eq!(
css::embed_css(cache, &client, &document_url, &CSS, &options),
css::embed_css(cache, &client, &document_url, &CSS, &options, &cookies),
CSS
);
}
Expand All @@ -249,8 +260,9 @@ mod passing {
fn unusual_indents() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.silent = true;

const CSS: &str = "\
Expand All @@ -264,7 +276,7 @@ mod passing {
";

assert_eq!(
css::embed_css(cache, &client, &document_url, &CSS, &options),
css::embed_css(cache, &client, &document_url, &CSS, &options, &cookies),
CSS
);
}
Expand All @@ -273,8 +285,9 @@ mod passing {
fn exclude_fonts() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.no_fonts = true;
options.silent = true;

Expand Down Expand Up @@ -312,7 +325,7 @@ mod passing {
";

assert_eq!(
css::embed_css(cache, &client, &document_url, &CSS, &options),
css::embed_css(cache, &client, &document_url, &CSS, &options, &cookies),
CSS_OUT
);
}
Expand All @@ -321,8 +334,9 @@ mod passing {
fn content() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("data:,").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.silent = true;

const CSS: &str = "\
Expand All @@ -337,7 +351,7 @@ mod passing {
";

assert_eq!(
css::embed_css(cache, &client, &document_url, &CSS, &options),
css::embed_css(cache, &client, &document_url, &CSS, &options, &cookies),
CSS_OUT
);
}
Expand All @@ -346,8 +360,9 @@ mod passing {
fn ie_css_hack() {
let cache = &mut HashMap::new();
let client = Client::new();
let cookies: Vec<Cookie> = vec![];
let document_url: Url = Url::parse("data:,").unwrap();
let mut options = Options::default();
let mut options = Options::parse();
options.silent = true;

const CSS: &str = "\
Expand All @@ -364,7 +379,7 @@ mod passing {
";

assert_eq!(
css::embed_css(cache, &client, &document_url, &CSS, &options),
css::embed_css(cache, &client, &document_url, &CSS, &options, &cookies),
CSS_OUT
);
}
Expand Down
16 changes: 9 additions & 7 deletions tests/html/compose_csp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

#[cfg(test)]
mod passing {
use clap::Parser;

use monolith::html;
use monolith::opts::Options;

#[test]
fn isolated() {
let mut options = Options::default();
let mut options = Options::parse();
options.isolate = true;
let csp_content = html::compose_csp(&options);

Expand All @@ -24,7 +26,7 @@ mod passing {

#[test]
fn no_css() {
let mut options = Options::default();
let mut options = Options::parse();
options.no_css = true;
let csp_content = html::compose_csp(&options);

Expand All @@ -33,7 +35,7 @@ mod passing {

#[test]
fn no_fonts() {
let mut options = Options::default();
let mut options = Options::parse();
options.no_fonts = true;
let csp_content = html::compose_csp(&options);

Expand All @@ -42,7 +44,7 @@ mod passing {

#[test]
fn no_frames() {
let mut options = Options::default();
let mut options = Options::parse();
options.no_frames = true;
let csp_content = html::compose_csp(&options);

Expand All @@ -51,7 +53,7 @@ mod passing {

#[test]
fn no_js() {
let mut options = Options::default();
let mut options = Options::parse();
options.no_js = true;
let csp_content = html::compose_csp(&options);

Expand All @@ -60,7 +62,7 @@ mod passing {

#[test]
fn no_images() {
let mut options = Options::default();
let mut options = Options::parse();
options.no_images = true;
let csp_content = html::compose_csp(&options);

Expand All @@ -69,7 +71,7 @@ mod passing {

#[test]
fn all() {
let mut options = Options::default();
let mut options = Options::parse();
options.isolate = true;
options.no_css = true;
options.no_fonts = true;
Expand Down
Loading

0 comments on commit 40acdd5

Please sign in to comment.