aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tests/create_directories.rs19
-rw-r--r--tests/fixtures/mod.rs32
-rw-r--r--tests/serve_request.rs31
-rw-r--r--tests/webdav.rs26
4 files changed, 48 insertions, 60 deletions
diff --git a/tests/create_directories.rs b/tests/create_directories.rs
index c15b40f..bd9259f 100644
--- a/tests/create_directories.rs
+++ b/tests/create_directories.rs
@@ -1,8 +1,3 @@
-#[cfg(unix)]
-use std::os::unix::fs::symlink as symlink_dir;
-#[cfg(windows)]
-use std::os::windows::fs::symlink_dir;
-
use reqwest::blocking::{multipart, Client};
use rstest::rstest;
use select::{
@@ -12,7 +7,7 @@ use select::{
mod fixtures;
-use crate::fixtures::{server, Error, TestServer, DIRECTORIES};
+use crate::fixtures::{server, Error, TestServer, DIRECTORY_SYMLINK};
/// This should work because the flags for uploading files and creating directories
/// are set, and the directory name and path are valid.
@@ -100,20 +95,14 @@ fn creating_directories_is_prevented(server: TestServer) -> Result<(), Error> {
fn creating_directories_through_symlinks_is_prevented(
#[with(&["--upload-files", "--mkdir", "--no-symlinks"])] server: TestServer,
) -> Result<(), Error> {
- // Make symlinks
- let symlink_directory_str = "symlink";
- let symlink_directory = server.path().join(symlink_directory_str);
- let symlinked_direcotry = server.path().join(DIRECTORIES[0]);
- symlink_dir(symlinked_direcotry, symlink_directory).unwrap();
-
// Before attempting to create, ensure the symlink does not exist.
let body = reqwest::blocking::get(server.url())?.error_for_status()?;
let parsed = Document::from_read(body)?;
- assert!(parsed.find(Text).all(|x| x.text() != symlink_directory_str));
+ assert!(parsed.find(Text).all(|x| x.text() != DIRECTORY_SYMLINK));
// Attempt to perform directory creation.
let form = multipart::Form::new();
- let part = multipart::Part::text(symlink_directory_str);
+ let part = multipart::Part::text(DIRECTORY_SYMLINK);
let form = form.part("mkdir", part);
// This should fail
@@ -121,7 +110,7 @@ fn creating_directories_through_symlinks_is_prevented(
.post(
server
.url()
- .join(format!("/upload?path=/{symlink_directory_str}").as_str())?
+ .join(format!("/upload?path=/{DIRECTORY_SYMLINK}").as_str())?
)
.multipart(form)
.send()?
diff --git a/tests/fixtures/mod.rs b/tests/fixtures/mod.rs
index f5231f7..a96a5b7 100644
--- a/tests/fixtures/mod.rs
+++ b/tests/fixtures/mod.rs
@@ -1,12 +1,13 @@
+use std::process::{Child, Command, Stdio};
+use std::thread::sleep;
+use std::time::{Duration, Instant};
+
use assert_cmd::prelude::*;
use assert_fs::fixture::TempDir;
use assert_fs::prelude::*;
use port_check::free_local_port;
use reqwest::Url;
use rstest::fixture;
-use std::process::{Child, Command, Stdio};
-use std::thread::sleep;
-use std::time::{Duration, Instant};
/// Error type used by tests
pub type Error = Box<dyn std::error::Error>;
@@ -41,6 +42,15 @@ pub static HIDDEN_DIRECTORIES: &[&str] = &[".hidden_dir1/", ".hidden_dir2/"];
/// Name of a deeply nested file
pub static DEEPLY_NESTED_FILE: &str = "very/deeply/nested/test.rs";
+/// Name of a symlink pointing to a directory
+pub static DIRECTORY_SYMLINK: &str = "dir_symlink/";
+
+/// Name of a symlink pointing to a file
+pub static FILE_SYMLINK: &str = "file_symlink";
+
+/// Name of a symlink pointing to a path that doesn't exist
+pub static BROKEN_SYMLINK: &str = "broken_symlink";
+
/// Test fixture which creates a temporary directory with a few files and directories inside.
/// The directories also contain files.
#[fixture]
@@ -70,6 +80,22 @@ pub fn tmpdir() -> TempDir {
.child(DEEPLY_NESTED_FILE)
.write_str("File in a deeply nested directory.")
.expect("Couldn't write to file");
+
+ tmpdir
+ .child(DIRECTORY_SYMLINK.strip_suffix("/").unwrap())
+ .symlink_to_dir(DIRECTORIES[0])
+ .expect("Couldn't create symlink to dir");
+
+ tmpdir
+ .child(FILE_SYMLINK)
+ .symlink_to_file(FILES[0])
+ .expect("Couldn't create symlink to file");
+
+ tmpdir
+ .child(BROKEN_SYMLINK)
+ .symlink_to_file("broken_symlink")
+ .expect("Couldn't create broken symlink");
+
tmpdir
}
diff --git a/tests/serve_request.rs b/tests/serve_request.rs
index f536200..3f8e27a 100644
--- a/tests/serve_request.rs
+++ b/tests/serve_request.rs
@@ -4,6 +4,7 @@ use std::time::Duration;
use assert_cmd::prelude::*;
use assert_fs::fixture::TempDir;
+use fixtures::BROKEN_SYMLINK;
use regex::Regex;
use reqwest::StatusCode;
use rstest::rstest;
@@ -12,15 +13,10 @@ use select::{document::Document, node::Node, predicate::Attr};
mod fixtures;
use crate::fixtures::{
- port, server, server_no_stderr, tmpdir, Error, TestServer, DIRECTORIES, FILES,
- HIDDEN_DIRECTORIES, HIDDEN_FILES,
+ port, server, server_no_stderr, tmpdir, Error, TestServer, DIRECTORIES, DIRECTORY_SYMLINK,
+ FILES, FILE_SYMLINK, HIDDEN_DIRECTORIES, HIDDEN_FILES,
};
-#[cfg(unix)]
-use std::os::unix::fs::{symlink as symlink_dir, symlink as symlink_file};
-#[cfg(windows)]
-use std::os::windows::fs::{symlink_dir, symlink_file};
-
#[rstest]
fn serves_requests_with_no_options(tmpdir: TempDir) -> Result<(), Error> {
let mut child = Command::cargo_bin("miniserve")?
@@ -133,23 +129,10 @@ fn serves_requests_symlinks(
#[case] show_symlink_info: bool,
#[case] server: TestServer,
) -> Result<(), Error> {
- let file = "symlink-file.html";
- let dir = "symlink-dir/";
- let broken = "symlink broken";
-
- // Set up some basic symlinks:
- // to dir, to file, to non-existent location
- let orig = DIRECTORIES[0].strip_suffix('/').unwrap();
- let link = server.path().join(dir.strip_suffix('/').unwrap());
- symlink_dir(orig, link).expect("Couldn't create symlink");
- symlink_file(FILES[0], server.path().join(file)).expect("Couldn't create symlink");
- symlink_file("should-not-exist.xxx", server.path().join(broken))
- .expect("Couldn't create symlink");
-
let body = reqwest::blocking::get(server.url())?.error_for_status()?;
let parsed = Document::from_read(body)?;
- for &entry in &[file, dir] {
+ for &entry in &[FILE_SYMLINK, DIRECTORY_SYMLINK] {
let status = reqwest::blocking::get(server.url().join(entry)?)?.status();
// We expect a 404 here for when `no_symlinks` is `true`.
if no_symlinks {
@@ -163,6 +146,7 @@ fn serves_requests_symlinks(
.next();
// If symlinks are deactivated, none should be shown in the listing.
+ dbg!(&node);
assert_eq!(node.is_none(), no_symlinks);
if node.is_some() && show_symlink_info {
assert_eq!(node.unwrap().attr("class").unwrap(), "symlink");
@@ -188,7 +172,10 @@ fn serves_requests_symlinks(
assert_eq!(node.unwrap().attr("class").unwrap(), "file");
}
}
- assert!(parsed.find(|x: &Node| x.text() == broken).next().is_none());
+ assert!(parsed
+ .find(|x: &Node| x.text() == BROKEN_SYMLINK)
+ .next()
+ .is_none());
Ok(())
}
diff --git a/tests/webdav.rs b/tests/webdav.rs
index 1bc7e12..eb97e3c 100644
--- a/tests/webdav.rs
+++ b/tests/webdav.rs
@@ -1,7 +1,3 @@
-#[cfg(unix)]
-use std::os::unix::fs::{symlink as symlink_dir, symlink as symlink_file};
-#[cfg(windows)]
-use std::os::windows::fs::{symlink_dir, symlink_file};
use std::process::Command;
use assert_cmd::prelude::*;
@@ -17,7 +13,8 @@ use rstest::rstest;
mod fixtures;
use crate::fixtures::{
- server, tmpdir, Error, TestServer, DIRECTORIES, FILES, HIDDEN_DIRECTORIES, HIDDEN_FILES,
+ server, server_no_stderr, tmpdir, Error, TestServer, DIRECTORIES, DIRECTORY_SYMLINK, FILES,
+ FILE_SYMLINK, HIDDEN_DIRECTORIES, HIDDEN_FILES,
};
#[rstest]
@@ -93,36 +90,25 @@ fn webdav_respects_hidden_flag(
#[rstest]
#[case(server(&["--enable-webdav"]), true)]
#[should_panic]
-#[case(server(&["--enable-webdav", "--no-symlinks"]), false)]
+#[case(server_no_stderr(&["--enable-webdav", "--no-symlinks"]), false)]
fn webdav_respects_no_symlink_flag(#[case] server: TestServer, #[case] symlinks_should_show: bool) {
- // Make symlinks
- let symlink_directory_str = "symlink_directory";
- let symlink_directory = server.path().join(symlink_directory_str);
- let symlinked_direcotry = server.path().join(DIRECTORIES[0]);
- symlink_dir(symlinked_direcotry, symlink_directory).unwrap();
-
- let symlink_filename_str = "symlink_file";
- let symlink_filename = server.path().join(symlink_filename_str);
- let symlinked_file = server.path().join(FILES[0]);
- symlink_file(symlinked_file, symlink_filename).unwrap();
-
let list = list_webdav(server.url(), "/").unwrap();
assert_eq!(
symlinks_should_show,
list.iter().any(|el|
- matches!(el, ListEntity::File(ListFile { href, .. }) if href.contains(symlink_filename_str))
+ matches!(el, ListEntity::File(ListFile { href, .. }) if href.contains(FILE_SYMLINK))
),
);
assert_eq!(
symlinks_should_show,
list.iter().any(|el|
- matches!(el, ListEntity::Folder(ListFolder { href, .. }) if href.contains(symlink_directory_str))
+ matches!(el, ListEntity::Folder(ListFolder { href, .. }) if href.contains(DIRECTORY_SYMLINK))
),
);
- let list_linked = list_webdav(server.url(), &format!("/{}", symlink_directory_str));
+ let list_linked = list_webdav(server.url(), &format!("/{}", DIRECTORY_SYMLINK));
assert_eq!(symlinks_should_show, list_linked.is_ok());
}