diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/fixtures/mod.rs | 25 | ||||
-rw-r--r-- | tests/navigation.rs | 90 | ||||
-rw-r--r-- | tests/serve_request.rs | 20 | ||||
-rw-r--r-- | tests/utils/mod.rs | 12 |
4 files changed, 141 insertions, 6 deletions
diff --git a/tests/fixtures/mod.rs b/tests/fixtures/mod.rs index 9cfd2cf..cec912a 100644 --- a/tests/fixtures/mod.rs +++ b/tests/fixtures/mod.rs @@ -4,13 +4,22 @@ use port_check::free_local_port; use rstest::fixture; /// Error type used by tests -pub type Error = Box<std::error::Error>; +pub type Error = Box<dyn std::error::Error>; /// File names for testing purpose #[allow(dead_code)] pub static FILES: &[&str] = &["test.txt", "test.html", "test.mkv"]; -/// Test fixture which creates a temporary directory with a few files inside. +/// Directory names for testing purpose +#[allow(dead_code)] +pub static DIRECTORIES: &[&str] = &["dira/", "dirb/", "dirc/"]; + +/// Name of a deeply nested file +#[allow(dead_code)] +pub static DEEPLY_NESTED_FILE: &str = "very/deeply/nested/test.rs"; + +/// Test fixture which creates a temporary directory with a few files and directories inside. +/// The directories also contain files. #[fixture] #[allow(dead_code)] pub fn tmpdir() -> TempDir { @@ -21,6 +30,18 @@ pub fn tmpdir() -> TempDir { .write_str("Test Hello Yes") .expect("Couldn't write to file"); } + for &directory in DIRECTORIES { + for &file in FILES { + tmpdir + .child(format!("{}{}", directory, file)) + .write_str(&format!("This is {}{}", directory, file)) + .expect("Couldn't write to file"); + } + } + tmpdir + .child(&DEEPLY_NESTED_FILE) + .write_str("File in a deeply nested directory.") + .expect("Couldn't write to file"); tmpdir } diff --git a/tests/navigation.rs b/tests/navigation.rs new file mode 100644 index 0000000..d847f3e --- /dev/null +++ b/tests/navigation.rs @@ -0,0 +1,90 @@ +mod fixtures; +mod utils; + +use assert_cmd::prelude::*; +use assert_fs::fixture::TempDir; +use fixtures::{port, tmpdir, Error, DIRECTORIES, DEEPLY_NESTED_FILE}; +use rstest::rstest; +use select::document::Document; +use std::process::{Command, Stdio}; +use std::thread::sleep; +use std::time::Duration; +use utils::get_link_from_text; + +#[rstest] +/// We can navigate into directories and back using shown links. +fn can_navigate_into_dirs_and_back(tmpdir: TempDir, port: u16) -> Result<(), Error> { + let mut child = Command::cargo_bin("miniserve")? + .arg("-p") + .arg(port.to_string()) + .arg(tmpdir.path()) + .stdout(Stdio::null()) + .spawn()?; + + sleep(Duration::from_secs(1)); + + let original_location = format!("http://localhost:{}/", port); + let body = reqwest::get(&original_location)?.error_for_status()?; + let parsed = Document::from_read(body)?; + for &directory in DIRECTORIES { + let dir_elem = get_link_from_text(&parsed, &directory).expect("Dir should have been here."); + let dir_body = + reqwest::get(format!("http://localhost:{}/{}", port, dir_elem).as_str())?.error_for_status()?; + let dir_parsed = Document::from_read(dir_body)?; + let back_link = get_link_from_text(&dir_parsed, "Parent directory").expect("Back link should be there."); + let back_location = format!("http://localhost:{}{}", port, back_link); + + // Now check that we can actually get back to the original location we came from using the + // link. + assert_eq!(back_location, original_location); + } + + child.kill()?; + + Ok(()) +} + +#[rstest] +/// We can navigate deep into the file tree and back using shown links. +fn can_navigate_deep_into_dirs_and_back(tmpdir: TempDir, port: u16) -> Result<(), Error> { + let mut child = Command::cargo_bin("miniserve")? + .arg("-p") + .arg(port.to_string()) + .arg(tmpdir.path()) + .stdout(Stdio::null()) + .spawn()?; + + sleep(Duration::from_secs(1)); + + // Create a vector of directory names. We don't need to fetch the file and so we'll + // remove that part. + let dir_names = { + let mut comps = DEEPLY_NESTED_FILE.split("/").map(|d| format!("{}/", d)).collect::<Vec<String>>(); + comps.pop(); + comps + }; + let base_url = format!("http://localhost:{}", port); + + // First we'll go forwards through the directory tree and then we'll go backwards. + // In the end, we'll have to end up where we came from. + let mut next_url = base_url.clone(); + for dir_name in dir_names.iter() { + let body = reqwest::get(&next_url)?.error_for_status()?; + let parsed = Document::from_read(body)?; + let dir_elem = get_link_from_text(&parsed, &dir_name).expect("Dir should have been here."); + next_url = format!("{}{}", base_url, dir_elem); + } + + // Now try to get out the tree again using links only. + let start_url = format!("{}/", base_url); + while next_url != start_url { + let body = reqwest::get(&next_url)?.error_for_status()?; + let parsed = Document::from_read(body)?; + let dir_elem = get_link_from_text(&parsed, "Parent directory").expect("Back link not found."); + next_url = format!("{}{}", base_url, dir_elem); + } + + child.kill()?; + + Ok(()) +} diff --git a/tests/serve_request.rs b/tests/serve_request.rs index ef346b2..7419ab1 100644 --- a/tests/serve_request.rs +++ b/tests/serve_request.rs @@ -2,10 +2,10 @@ mod fixtures; use assert_cmd::prelude::*; use assert_fs::fixture::TempDir; -use fixtures::{port, tmpdir, Error, FILES}; +use fixtures::{port, tmpdir, Error, DIRECTORIES, FILES}; use rstest::rstest; use select::document::Document; -use select::predicate::Text; +use select::node::Node; use std::process::{Command, Stdio}; use std::thread::sleep; use std::time::Duration; @@ -22,7 +22,7 @@ fn serves_requests_with_no_options(tmpdir: TempDir) -> Result<(), Error> { let body = reqwest::get("http://localhost:8080")?.error_for_status()?; let parsed = Document::from_read(body)?; for &file in FILES { - assert!(parsed.find(Text).any(|x| x.text() == file)); + assert!(parsed.find(|x: &Node| x.text() == file).next().is_some()); } child.kill()?; @@ -44,7 +44,19 @@ fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<( let body = reqwest::get(format!("http://localhost:{}", port).as_str())?.error_for_status()?; let parsed = Document::from_read(body)?; for &file in FILES { - assert!(parsed.find(Text).any(|x| x.text() == file)); + assert!(parsed.find(|x: &Node| x.text() == file).next().is_some()); + } + for &directory in DIRECTORIES { + assert!(parsed + .find(|x: &Node| x.text() == directory) + .next() + .is_some()); + let dir_body = reqwest::get(format!("http://localhost:{}/{}", port, directory).as_str())? + .error_for_status()?; + let dir_body_parsed = Document::from_read(dir_body)?; + for &file in FILES { + assert!(dir_body_parsed.find(|x: &Node| x.text() == file).next().is_some()); + } } child.kill()?; diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs new file mode 100644 index 0000000..b724945 --- /dev/null +++ b/tests/utils/mod.rs @@ -0,0 +1,12 @@ +use select::document::Document; +use select::node::Node; +use select::predicate::Name; +use select::predicate::Predicate; + +/// Return the href attribute content for the closest anchor found by `text`. +pub fn get_link_from_text(document: &Document, text: &str) -> Option<String> { + let a_elem = document + .find(Name("a").and(|x: &Node| x.children().any(|x| x.text() == text))) + .next()?; + Some(a_elem.attr("href")?.to_string()) +} |