aboutsummaryrefslogtreecommitdiffstats
path: root/tests/upload_files.rs
diff options
context:
space:
mode:
authorSven-Hendrik Haase <svenstaro@gmail.com>2020-03-01 06:54:33 +0000
committerGitHub <noreply@github.com>2020-03-01 06:54:33 +0000
commit0d5c0fcdc60ba4cbfd0bfaeb366e9d25fcadb72e (patch)
treec6166396b10205b924fb2f4d6fd89bdd1b642467 /tests/upload_files.rs
parentMerge pull request #248 from svenstaro/dependabot/cargo/http-0.1.21 (diff)
parentAdded test for disabled file uploads (diff)
downloadminiserve-0d5c0fcdc60ba4cbfd0bfaeb366e9d25fcadb72e.tar.gz
miniserve-0d5c0fcdc60ba4cbfd0bfaeb366e9d25fcadb72e.zip
Merge pull request #236 from DamianX/test_upload_disabled
Added test for disabled file uploads
Diffstat (limited to 'tests/upload_files.rs')
-rw-r--r--tests/upload_files.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/upload_files.rs b/tests/upload_files.rs
index 6a24aee..53833a6 100644
--- a/tests/upload_files.rs
+++ b/tests/upload_files.rs
@@ -59,3 +59,49 @@ fn uploading_files_works(tmpdir: TempDir, port: u16) -> Result<(), Error> {
Ok(())
}
+
+#[rstest]
+fn uploading_files_is_prevented(tmpdir: TempDir, port: u16) -> Result<(), Error> {
+ let test_file_name = "uploaded test file.txt";
+
+ 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));
+
+ // Before uploading, check whether the uploaded file does not yet exist.
+ let body = reqwest::get(format!("http://localhost:{}", port).as_str())?.error_for_status()?;
+ let parsed = Document::from_read(body)?;
+ assert!(parsed.find(Text).all(|x| x.text() != test_file_name));
+
+ // Ensure the file upload form is not present
+ assert!(parsed.find(Attr("id", "file_submit")).next().is_none());
+
+ // Then try to upload anyway
+ let form = multipart::Form::new();
+ let part = multipart::Part::text("this should not be uploaded")
+ .file_name(test_file_name)
+ .mime_str("text/plain")?;
+ let form = form.part("file_to_upload", part);
+
+ let client = reqwest::Client::new();
+ // Ensure uploading fails and returns an error
+ assert!(client
+ .post(format!("http://localhost:{}{}", port, "/upload?path=/").as_str())
+ .multipart(form)
+ .send()?
+ .error_for_status().is_err());
+
+ // After uploading, check whether the uploaded file is now getting listed.
+ let body = reqwest::get(format!("http://localhost:{}", port).as_str())?;
+ let parsed = Document::from_read(body)?;
+ assert!(!parsed.find(Text).any(|x| x.text() == test_file_name));
+
+ child.kill()?;
+
+ Ok(())
+}