diff options
author | Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com> | 2021-04-24 04:28:55 +0000 |
---|---|---|
committer | Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com> | 2021-08-26 11:21:47 +0000 |
commit | dfd0ecf931b68ea373be1e4e785421d48ca6fed5 (patch) | |
tree | 115986299dcdeccc0bf2fac00da32dc64759ec1f /tests/serve_request.rs | |
parent | Upgrade deps (diff) | |
download | miniserve-dfd0ecf931b68ea373be1e4e785421d48ca6fed5.tar.gz miniserve-dfd0ecf931b68ea373be1e4e785421d48ca6fed5.zip |
tests: Refactor!
Remove duplicate code responsible for the initial setup and teardown of
the test binary.
This introduces `TestServer` as a resource manager for a running
miniserve binary, which can be created with the fixtures `server()` and
`server_no_stderr()`
It also provides convenience function for handling server url.
Diffstat (limited to '')
-rw-r--r-- | tests/serve_request.rs | 110 |
1 files changed, 23 insertions, 87 deletions
diff --git a/tests/serve_request.rs b/tests/serve_request.rs index e259b9e..323b7b4 100644 --- a/tests/serve_request.rs +++ b/tests/serve_request.rs @@ -2,13 +2,14 @@ mod fixtures; use assert_cmd::prelude::*; use assert_fs::fixture::TempDir; -use fixtures::{port, tmpdir, Error, DIRECTORIES, FILES, HIDDEN_DIRECTORIES, HIDDEN_FILES}; +use fixtures::{ + port, server, tmpdir, Error, TestServer, DIRECTORIES, FILES, HIDDEN_DIRECTORIES, HIDDEN_FILES, +}; use http::StatusCode; use regex::Regex; use rstest::rstest; use select::document::Document; use select::node::Node; -use std::path::Path; use std::process::{Command, Stdio}; use std::thread::sleep; use std::time::Duration; @@ -39,28 +40,13 @@ fn serves_requests_with_no_options(tmpdir: TempDir) -> Result<(), Error> { } #[rstest] -fn serves_requests_with_non_default_port(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()?; +fn serves_requests_with_non_default_port(server: TestServer) -> Result<(), Error> { + let body = reqwest::blocking::get(server.url())?.error_for_status()?; let parsed = Document::from_read(body)?; for &file in FILES { let f = parsed.find(|x: &Node| x.text() == file).next().unwrap(); - reqwest::blocking::get(format!( - "http://localhost:{}/{}", - port, - f.attr("href").unwrap() - ))? - .error_for_status()?; + reqwest::blocking::get(server.url().join(f.attr("href").unwrap())?)?.error_for_status()?; assert_eq!( format!("/{}", file), percent_encoding::percent_decode_str(f.attr("href").unwrap()).decode_utf8_lossy(), @@ -73,8 +59,7 @@ fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<( .next() .is_some()); let dir_body = - reqwest::blocking::get(format!("http://localhost:{}/{}", port, directory).as_str())? - .error_for_status()?; + reqwest::blocking::get(server.url().join(&directory)?)?.error_for_status()?; let dir_body_parsed = Document::from_read(dir_body)?; for &file in FILES { assert!(dir_body_parsed @@ -84,25 +69,12 @@ fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<( } } - child.kill()?; - Ok(()) } -#[rstest] -fn serves_requests_hidden_files(tmpdir: TempDir, port: u16) -> Result<(), Error> { - let mut child = Command::cargo_bin("miniserve")? - .arg(tmpdir.path()) - .arg("-p") - .arg(port.to_string()) - .arg("--hidden") - .stdout(Stdio::null()) - .spawn()?; - - sleep(Duration::from_secs(1)); - - let body = reqwest::blocking::get(format!("http://localhost:{}", port).as_str())? - .error_for_status()?; +#[rstest(server(&["--hidden"]))] +fn serves_requests_hidden_files(server: TestServer) -> Result<(), Error> { + let body = reqwest::blocking::get(server.url())?.error_for_status()?; let parsed = Document::from_read(body)?; for &file in FILES.into_iter().chain(HIDDEN_FILES) { @@ -119,8 +91,7 @@ fn serves_requests_hidden_files(tmpdir: TempDir, port: u16) -> Result<(), Error> .next() .is_some()); let dir_body = - reqwest::blocking::get(format!("http://localhost:{}/{}", port, directory).as_str())? - .error_for_status()?; + reqwest::blocking::get(server.url().join(&directory)?)?.error_for_status()?; let dir_body_parsed = Document::from_read(dir_body)?; for &file in FILES.into_iter().chain(HIDDEN_FILES) { assert!(dir_body_parsed @@ -130,24 +101,12 @@ fn serves_requests_hidden_files(tmpdir: TempDir, port: u16) -> Result<(), Error> } } - child.kill()?; - Ok(()) } #[rstest] -fn serves_requests_no_hidden_files_without_flag(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()?; +fn serves_requests_no_hidden_files_without_flag(server: TestServer) -> Result<(), Error> { + let body = reqwest::blocking::get(server.url())?.error_for_status()?; let parsed = Document::from_read(body)?; for &hidden_item in HIDDEN_FILES.into_iter().chain(HIDDEN_DIRECTORIES) { @@ -155,54 +114,34 @@ fn serves_requests_no_hidden_files_without_flag(tmpdir: TempDir, port: u16) -> R .find(|x: &Node| x.text() == hidden_item) .next() .is_none()); - let resp = - reqwest::blocking::get(format!("http://localhost:{}/{}", port, hidden_item).as_str())?; + let resp = reqwest::blocking::get(server.url().join(&hidden_item)?)?; assert_eq!(resp.status(), StatusCode::BAD_REQUEST); } - child.kill()?; - Ok(()) } #[rstest(no_symlinks, case(true), case(false))] -fn serves_requests_symlinks(tmpdir: TempDir, port: u16, no_symlinks: bool) -> Result<(), Error> { - let mut comm = Command::cargo_bin("miniserve")?; - comm.arg(tmpdir.path()) - .arg("-p") - .arg(port.to_string()) - .stdout(Stdio::null()); - if no_symlinks { - comm.arg("--no-symlinks"); - } - - let mut child = comm.spawn()?; - sleep(Duration::from_secs(1)); - +fn serves_requests_symlinks(no_symlinks: bool) -> Result<(), Error> { + let server = server(["--no-symlinks"].iter().filter(|_| no_symlinks)); let files = &["symlink-file.html"]; let dirs = &["symlink-dir/"]; let broken = &["symlink broken"]; for &directory in dirs { - let orig = Path::new(DIRECTORIES[0].strip_suffix("/").unwrap()); - let link = tmpdir - .path() - .join(Path::new(directory.strip_suffix("/").unwrap())); + let orig = DIRECTORIES[0].strip_suffix("/").unwrap(); + let link = server.path().join(directory.strip_suffix("/").unwrap()); symlink_dir(orig, link).expect("Couldn't create symlink"); } for &file in files { - let orig = Path::new(FILES[0]); - let link = tmpdir.path().join(Path::new(file)); - symlink_file(orig, link).expect("Couldn't create symlink"); + symlink_file(FILES[0], server.path().join(file)).expect("Couldn't create symlink"); } for &file in broken { - let orig = Path::new("should-not-exist.xxx"); - let link = tmpdir.path().join(Path::new(file)); - symlink_file(orig, link).expect("Couldn't create symlink"); + symlink_file("souldnt-exist.xxx", server.path().join(file)) + .expect("Couldn't create symlink"); } - let body = reqwest::blocking::get(format!("http://localhost:{}", port).as_str())? - .error_for_status()?; + let body = reqwest::blocking::get(server.url())?.error_for_status()?; let parsed = Document::from_read(body)?; for &entry in files.into_iter().chain(dirs) { @@ -216,8 +155,7 @@ fn serves_requests_symlinks(tmpdir: TempDir, port: u16, no_symlinks: bool) -> Re let node = node.unwrap(); assert_eq!(node.attr("href").unwrap().strip_prefix("/").unwrap(), entry); - reqwest::blocking::get(format!("http://localhost:{}/{}", port, entry))? - .error_for_status()?; + reqwest::blocking::get(server.url().join(&entry)?)?.error_for_status()?; if entry.ends_with("/") { assert_eq!(node.attr("class").unwrap(), "directory"); } else { @@ -228,8 +166,6 @@ fn serves_requests_symlinks(tmpdir: TempDir, port: u16, no_symlinks: bool) -> Re assert!(parsed.find(|x: &Node| x.text() == entry).next().is_none()); } - child.kill()?; - Ok(()) } |