blob: 14b81f3632b92aaa8c7008f6f7d7ad6f76ff8662 (
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
|
use assert_cmd::prelude::*;
use assert_fs::fixture::TempDir;
use assert_fs::prelude::*;
use clap::{crate_name, crate_version};
use portpicker::pick_unused_port;
use reqwest;
use select::document::Document;
use select::predicate::Text;
use std::process::{Command, Stdio};
use std::thread::sleep;
use std::time::Duration;
type Error = Box<std::error::Error>;
static FILES: &[&str] = &["test.txt", "test.html", "test.mkv"];
// Test fixture which creates a temporary directory with a few files inside.
pub fn tmpdir() -> Result<TempDir, Error> {
let tmpdir = assert_fs::TempDir::new()?;
for &file in FILES {
tmpdir.child(file).touch()?;
}
Ok(tmpdir)
}
#[test]
/// Starts and serves requests without any options.
fn starts_ok_with_no_option() -> Result<(), Error> {
let tmpdir = tmpdir()?;
let mut child = Command::cargo_bin("miniserve")?
.arg(tmpdir.path())
.stdout(Stdio::null())
.spawn()?;
sleep(Duration::from_secs(1));
let body = reqwest::get("http://localhost:8080")?;
let parsed = Document::from_read(body)?;
for &file in FILES {
assert!(parsed.find(Text).any(|x| x.text() == file));
}
child.kill()?;
Ok(())
}
#[test]
/// Starts and serves requests on a non-default port.
fn starts_ok_with_non_default_port() -> Result<(), Error> {
let tmpdir = tmpdir()?;
let port = pick_unused_port().unwrap();
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::get("http://localhost:8080")?;
let parsed = Document::from_read(body)?;
for &file in FILES {
assert!(parsed.find(Text).any(|x| x.text() == file));
}
child.kill()?;
Ok(())
}
#[test]
/// Show help and exit.
fn help_shows() -> Result<(), Error> {
Command::cargo_bin("miniserve")?
.arg("-h")
.assert()
.success();
Ok(())
}
#[test]
/// Show version and exit.
fn version_shows() -> Result<(), Error> {
Command::cargo_bin("miniserve")?
.arg("-V")
.assert()
.success()
.stdout(format!("{} {}\n", crate_name!(), crate_version!()));
Ok(())
}
|