aboutsummaryrefslogtreecommitdiffstats
path: root/tests/readme.rs
blob: 0dabf87915fbac09fb3566320c268e213c0b5974 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
mod fixtures;

use assert_cmd::prelude::*;
use assert_fs::fixture::{FileWriteStr, TempDir};
use assert_fs::prelude::PathChild;
use fixtures::{server, tmpdir, Error, TestServer, DIRECTORIES};
use reqwest::Url;
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]
/// Do not show readme contents by default
fn no_readme_contents(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", "readme")).next().is_none());
    assert!(parsed.find(Attr("id", "readme-filename")).next().is_none());
    assert!(parsed.find(Attr("id", "readme-contents")).next().is_none());

    Ok(())
}

#[rstest]
/// Show readme contents when told to if there is no readme.md file
fn show_readme_contents(tmpdir: TempDir) -> Result<(), Error> {
    tmpdir
        .child("readme.md")
        .write_str("Readme Contents.")
        .expect("Couldn't write to readme.md");
    let mut child = Command::cargo_bin("miniserve")?
        .arg("--readme")
        .arg("--port")
        .arg("8090")
        .arg(tmpdir.path())
        .stdout(Stdio::null())
        .spawn()?;

    sleep(Duration::from_secs(1));
    let body = reqwest::blocking::get("http://localhost:8090")?.error_for_status()?;
    let parsed = Document::from_read(body)?;
    assert!(parsed.find(Attr("id", "readme")).next().is_some());
    assert!(parsed.find(Attr("id", "readme-filename")).next().is_some());
    assert!(
        parsed
            .find(Attr("id", "readme-filename"))
            .next()
            .unwrap()
            .text()
            == "readme.md"
    );
    assert!(parsed.find(Attr("id", "readme-contents")).next().is_some());
    assert!(
        parsed
            .find(Attr("id", "readme-contents"))
            .next()
            .unwrap()
            .text()
            .trim()
            == "Readme Contents."
    );

    child.kill()?;
    Ok(())
}

#[rstest]
/// Show readme contents when told to if there is no readme.md file
fn show_readme_contents_directories(tmpdir: TempDir) -> Result<(), Error> {
    let directories = DIRECTORIES.to_vec();
    for directory in directories.iter() {
        tmpdir
            .child(format!("{}{}", directory, "readme.md"))
            .write_str("Readme Contents.")
            .expect("Couldn't write to file");
    }

    let mut child = Command::cargo_bin("miniserve")?
        .arg("--readme")
        .arg(tmpdir.path())
        .stdout(Stdio::null())
        .spawn()?;

    sleep(Duration::from_secs(1));

    for directory in directories {
        let dir_body =
            reqwest::blocking::get(Url::parse("http://localhost:8080")?.join(&directory)?)?
                .error_for_status()?;
        let dir_body_parsed = Document::from_read(dir_body)?;
        assert!(dir_body_parsed.find(Attr("id", "readme")).next().is_some());
        assert!(dir_body_parsed
            .find(Attr("id", "readme-filename"))
            .next()
            .is_some());
        assert!(
            dir_body_parsed
                .find(Attr("id", "readme-filename"))
                .next()
                .unwrap()
                .text()
                == "readme.md"
        );
        assert!(dir_body_parsed
            .find(Attr("id", "readme-contents"))
            .next()
            .is_some());
        assert!(
            dir_body_parsed
                .find(Attr("id", "readme-contents"))
                .next()
                .unwrap()
                .text()
                .trim()
                == "Readme Contents."
        );
    }

    child.kill()?;
    Ok(())
}