diff options
Diffstat (limited to '')
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/renderer.rs | 2 | ||||
-rw-r--r-- | tests/qrcode.rs | 85 |
4 files changed, 77 insertions, 18 deletions
@@ -818,6 +818,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5320ae4c3782150d900b79807611a59a99fc9a1d61d686faafc24b93fc8d7ca" [[package]] +name = "fake-tty" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1caac348c61d1f9a7d43c3629abc75540754a4971944953f84b98cb8280e3971" + +[[package]] name = "fancy-regex" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1563,6 +1569,7 @@ dependencies = [ "clap_complete", "clap_mangen", "comrak", + "fake-tty", "futures", "get_if_addrs", "grass", @@ -69,6 +69,7 @@ tls = ["rustls", "rustls-pemfile", "actix-web/rustls"] [dev-dependencies] assert_cmd = "2" assert_fs = "1" +fake-tty = "0.2.0" predicates = "2" pretty_assertions = "1.2" regex = "1" diff --git a/src/renderer.rs b/src/renderer.rs index 48b74b6..95aeb5f 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -232,7 +232,7 @@ pub fn qr_code_page(qr: &QrCode) -> Markup { html! { (DOCTYPE) html { - body { + body.qr_code_page { // make QR code expand and fill page style { (PreEscaped("\ diff --git a/tests/qrcode.rs b/tests/qrcode.rs index a9c27fe..eb37abc 100644 --- a/tests/qrcode.rs +++ b/tests/qrcode.rs @@ -1,43 +1,94 @@ mod fixtures; -use fixtures::{server, server_no_stderr, Error, TestServer}; +use assert_cmd::prelude::CommandCargoExt; +use assert_fs::TempDir; +use fixtures::{port, server_no_stderr, tmpdir, Error, TestServer}; use reqwest::StatusCode; use rstest::rstest; -use select::document::Document; -use select::predicate::Attr; -use std::iter::repeat_with; +use std::process::Command; +use std::thread::sleep; +use std::time::Duration; + +fn run_in_faketty_kill_and_get_stdout(template: &Command) -> Result<String, Error> { + use fake_tty::{bash_command, get_stdout}; + + let cmd = { + let bin = template.get_program().to_str().expect("not UTF8"); + let args = template + .get_args() + .map(|s| s.to_str().expect("not UTF8")) + .collect::<Vec<_>>() + .join(" "); + format!("{} {}", bin, args) + }; + let mut child = bash_command(&cmd).spawn()?; + + sleep(Duration::from_secs(1)); + + child.kill()?; + let output = child.wait_with_output().expect("Failed to read stdout"); + let all_text = get_stdout(output.stdout)?; + + Ok(all_text) +} #[rstest] -fn hide_qrcode_element(server: TestServer) -> Result<(), Error> { - let body = reqwest::blocking::get(server.url())?.error_for_status()?; - let parsed = Document::from_read(body)?; - assert!(parsed.find(Attr("id", "qrcode")).next().is_none()); +fn qrcode_hidden_in_tty_when_disabled(tmpdir: TempDir, port: u16) -> Result<(), Error> { + let mut template = Command::cargo_bin("miniserve")?; + template.arg("-p").arg(port.to_string()).arg(tmpdir.path()); + let output = run_in_faketty_kill_and_get_stdout(&template)?; + + assert!(!output.contains("QR code for ")); Ok(()) } #[rstest] -fn show_qrcode_element(#[with(&["-q"])] server: TestServer) -> Result<(), Error> { - let body = reqwest::blocking::get(server.url())?.error_for_status()?; - let parsed = Document::from_read(body)?; - assert!(parsed.find(Attr("id", "qrcode")).next().is_some()); +fn qrcode_shown_in_tty_when_enabled(tmpdir: TempDir, port: u16) -> Result<(), Error> { + let mut template = Command::cargo_bin("miniserve")?; + template + .arg("-p") + .arg(port.to_string()) + .arg("-q") + .arg(tmpdir.path()); + + let output = run_in_faketty_kill_and_get_stdout(&template)?; + + assert!(output.contains("QR code for ")); + Ok(()) +} + +#[rstest] +fn qrcode_hidden_in_non_tty_when_enabled(tmpdir: TempDir, port: u16) -> Result<(), Error> { + let mut child = Command::cargo_bin("miniserve")? + .arg("-p") + .arg(port.to_string()) + .arg("-q") + .arg(tmpdir.path()) + .spawn()?; + + sleep(Duration::from_secs(1)); + + child.kill()?; + let output = child.wait_with_output().expect("Failed to read stdout"); + let stdout = String::from_utf8(output.stdout)?; + assert!(!stdout.contains("QR code for ")); Ok(()) } #[rstest] fn get_svg_qrcode(#[from(server_no_stderr)] server: TestServer) -> Result<(), Error> { // Ok - let resp = reqwest::blocking::get(server.url().join("/?qrcode=test")?)?; + let resp = reqwest::blocking::get(server.url().join("?qrcode=test")?)?; assert_eq!(resp.status(), StatusCode::OK); - assert_eq!(resp.headers()["Content-Type"], "image/svg+xml"); let body = resp.text()?; - assert!(body.starts_with("<?xml")); - assert_eq!(body.len(), 3530); + assert!(body.contains("qr_code_page")); + assert!(body.contains("<svg")); // Err - let content: String = repeat_with(|| '0').take(8 * 1024).collect(); + let content: String = "0".repeat(8192); let resp = reqwest::blocking::get(server.url().join(&format!("?qrcode={}", content))?)?; assert_eq!(resp.status(), StatusCode::URI_TOO_LONG); |