aboutsummaryrefslogtreecommitdiffstats
path: root/tests/qrcode.rs
blob: eb37abc071026381e121da5ecec158bec3b4fcf7 (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
mod fixtures;

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 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 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 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")?)?;

    assert_eq!(resp.status(), StatusCode::OK);
    let body = resp.text()?;
    assert!(body.contains("qr_code_page"));
    assert!(body.contains("<svg"));

    // Err
    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);

    Ok(())
}