aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorIsaac Parker <parrotmac@gmail.com>2020-06-28 20:03:31 +0000
committerIsaac Parker <parrotmac@gmail.com>2020-06-28 20:03:31 +0000
commitaa503c46c384c13fb02bf0cc98438336a161fcd4 (patch)
treeac6f8b3376946d1801273f125967004d0cb62d2a /tests
parentMake clippy and cargo fmt happy (diff)
downloadminiserve-aa503c46c384c13fb02bf0cc98438336a161fcd4.tar.gz
miniserve-aa503c46c384c13fb02bf0cc98438336a161fcd4.zip
Bind to random port when port 0 specified
Diffstat (limited to 'tests')
-rw-r--r--tests/serve_request.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/serve_request.rs b/tests/serve_request.rs
index 5761a65..918bb98 100644
--- a/tests/serve_request.rs
+++ b/tests/serve_request.rs
@@ -9,6 +9,7 @@ use select::node::Node;
use std::process::{Command, Stdio};
use std::thread::sleep;
use std::time::Duration;
+use regex::{Regex};
#[rstest]
fn serves_requests_with_no_options(tmpdir: TempDir) -> Result<(), Error> {
@@ -70,6 +71,31 @@ fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<(
}
#[rstest]
+fn serves_requests_with_randomly_assigned_port(tmpdir: TempDir) -> Result<(), Error> {
+ let mut child = Command::cargo_bin("miniserve")?
+ .arg(tmpdir.path())
+ .arg("-p")
+ .arg("0".to_string())
+ .stdout(Stdio::piped())
+ .spawn()?;
+
+ sleep(Duration::from_secs(1));
+ child.kill()?;
+
+ let output = child.wait_with_output().expect("Failed to read stdout");
+ let all_text = String::from_utf8(output.stdout)?;
+
+ let re = Regex::new(r"http://127.0.0.1:(\d+)").unwrap();
+ let caps = re.captures(all_text.as_str()).unwrap();
+ let port_num = caps.get(1).unwrap().as_str().parse::<u32>().unwrap();
+
+ assert!(port_num > 0);
+ assert!(port_num <= 65535);
+
+ Ok(())
+}
+
+#[rstest]
fn serves_requests_custom_index_notice(tmpdir: TempDir, port: u16) -> Result<(), Error> {
let mut child = Command::cargo_bin("miniserve")?
.arg("--index=not.html")