aboutsummaryrefslogtreecommitdiffstats
path: root/tests/qrcode.rs
blob: 48fa8c8e2e64d622cd871d20ef45958c5aecb1d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
mod fixtures;

use assert_cmd::prelude::CommandCargoExt;
use assert_fs::TempDir;
use fixtures::{port, server, tmpdir, Error, TestServer};
use rstest::rstest;
use select::document::Document;
use select::predicate::Attr;
use std::process::{Command, Stdio};
use std::thread::sleep;
use std::time::Duration;

#[rstest]
fn webpage_hides_qrcode_when_disabled(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());

    Ok(())
}

#[rstest]
fn webpage_shows_qrcode_when_enabled(#[with(&["-q"])] server: TestServer) -> Result<(), Error> {
    let body = reqwest::blocking::get(server.url())?.error_for_status()?;
    let parsed = Document::from_read(body)?;
    let qr_container = parsed
        .find(Attr("id", "qrcode"))
        .next()
        .ok_or("QR container not found")?;
    let tooltip = qr_container
        .attr("title")
        .ok_or("QR container has no title")?;
    assert_eq!(tooltip, server.url().as_str());

    Ok(())
}

#[cfg(not(windows))]
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)?.stdin(Stdio::null()).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]
// Disabled for Windows because `fake_tty` does not currently support it.
#[cfg(not(windows))]
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]
// Disabled for Windows because `fake_tty` does not currently support it.
#[cfg(not(windows))]
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())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .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(())
}