From 205a5a4cc49e33b86366969a76b3303d71287761 Mon Sep 17 00:00:00 2001 From: wyhaya Date: Sun, 5 Jul 2020 22:34:43 +0800 Subject: Add generate QR code (#330) * Add generate QR code * Add --qrcode option --- tests/qrcode.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests/qrcode.rs (limited to 'tests') diff --git a/tests/qrcode.rs b/tests/qrcode.rs new file mode 100644 index 0000000..b032267 --- /dev/null +++ b/tests/qrcode.rs @@ -0,0 +1,88 @@ +mod fixtures; + +use assert_cmd::prelude::*; +use assert_fs::fixture::TempDir; +use fixtures::{port, tmpdir, Error}; +use reqwest::StatusCode; +use rstest::rstest; +use select::document::Document; +use select::predicate::Attr; +use std::iter::repeat_with; +use std::process::{Command, Stdio}; +use std::thread::sleep; +use std::time::Duration; + +#[rstest] +fn hide_qrcode_element(tmpdir: TempDir, port: u16) -> Result<(), Error> { + let mut child = Command::cargo_bin("miniserve")? + .arg(tmpdir.path()) + .arg("-p") + .arg(port.to_string()) + .stdout(Stdio::null()) + .spawn()?; + + sleep(Duration::from_secs(1)); + + let body = reqwest::blocking::get(format!("http://localhost:{}", port).as_str())? + .error_for_status()?; + let parsed = Document::from_read(body)?; + assert!(parsed.find(Attr("id", "qrcode")).next().is_none()); + + child.kill()?; + + Ok(()) +} + +#[rstest] +fn show_qrcode_element(tmpdir: TempDir, port: u16) -> Result<(), Error> { + let mut child = Command::cargo_bin("miniserve")? + .arg(tmpdir.path()) + .arg("-p") + .arg(port.to_string()) + .arg("-q") + .stdout(Stdio::null()) + .spawn()?; + + sleep(Duration::from_secs(1)); + + let body = reqwest::blocking::get(format!("http://localhost:{}", port).as_str())? + .error_for_status()?; + let parsed = Document::from_read(body)?; + assert!(parsed.find(Attr("id", "qrcode")).next().is_some()); + + child.kill()?; + + Ok(()) +} + +#[rstest] +fn get_svg_qrcode(tmpdir: TempDir, port: u16) -> Result<(), Error> { + + let mut child = Command::cargo_bin("miniserve")? + .arg(tmpdir.path()) + .arg("-p") + .arg(port.to_string()) + .stdout(Stdio::null()) + .spawn()?; + + sleep(Duration::from_secs(1)); + + // Ok + let resp = reqwest::blocking::get(format!("http://localhost:{}/?qrcode=test", port).as_str())?; + + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.headers()["Content-Type"], "image/svg+xml"); + let body = resp.text()?; + assert!(body.starts_with("