aboutsummaryrefslogtreecommitdiffstats
path: root/tests/qrcode.rs
blob: b0322676a25092150581d9ba9f1b942fba8aa7f0 (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
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("<?xml"));
    assert_eq!(body.len(), 3530);

    // Err
    let content: String = repeat_with(|| '0').take(8 * 1024).collect();
    let resp = reqwest::blocking::get(format!("http://localhost:{}/?qrcode={}", port, content).as_str())?;
  
    assert_eq!(resp.status(), StatusCode::URI_TOO_LONG);

    child.kill()?;

    Ok(())
}