diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2019-04-15 23:43:04 +0000 |
---|---|---|
committer | Sven-Hendrik Haase <svenstaro@gmail.com> | 2019-04-15 23:43:04 +0000 |
commit | 06e6d9344f358f60651923bb827c069272c22955 (patch) | |
tree | 5430a8da6718f16f241230e0c2bfc4b501041324 /tests/cli.rs | |
parent | Fix tiny lint (diff) | |
download | miniserve-06e6d9344f358f60651923bb827c069272c22955.tar.gz miniserve-06e6d9344f358f60651923bb827c069272c22955.zip |
Set up testing infrastructure and add some integration tests (fixes #68)
Diffstat (limited to '')
-rw-r--r-- | tests/cli.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/cli.rs b/tests/cli.rs new file mode 100644 index 0000000..14b81f3 --- /dev/null +++ b/tests/cli.rs @@ -0,0 +1,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(()) +} |