aboutsummaryrefslogtreecommitdiffstats
path: root/tests/upload_files.rs
diff options
context:
space:
mode:
authorAli MJ Al-Nasrawy <alimjalnasrawy@gmail.com>2021-04-24 04:28:55 +0000
committerAli MJ Al-Nasrawy <alimjalnasrawy@gmail.com>2021-08-26 11:21:47 +0000
commitdfd0ecf931b68ea373be1e4e785421d48ca6fed5 (patch)
tree115986299dcdeccc0bf2fac00da32dc64759ec1f /tests/upload_files.rs
parentUpgrade deps (diff)
downloadminiserve-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/upload_files.rs50
1 files changed, 10 insertions, 40 deletions
diff --git a/tests/upload_files.rs b/tests/upload_files.rs
index 58707c5..97d513a 100644
--- a/tests/upload_files.rs
+++ b/tests/upload_files.rs
@@ -1,33 +1,17 @@
mod fixtures;
-use assert_cmd::prelude::*;
-use assert_fs::fixture::TempDir;
-use fixtures::{port, tmpdir, Error};
+use fixtures::{server, Error, TestServer};
use reqwest::blocking::{multipart, Client};
use rstest::rstest;
use select::document::Document;
use select::predicate::{Attr, Text};
-use std::process::{Command, Stdio};
-use std::thread::sleep;
-use std::time::Duration;
-#[rstest]
-fn uploading_files_works(tmpdir: TempDir, port: u16) -> Result<(), Error> {
+#[rstest(server(&["-u"]))]
+fn uploading_files_works(server: TestServer) -> 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())
- .arg("-u")
- .stdout(Stdio::null())
- .spawn()?;
-
- sleep(Duration::from_secs(1));
-
// Before uploading, check whether the uploaded file does not yet exist.
- 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)?;
assert!(parsed.find(Text).all(|x| x.text() != test_file_name));
@@ -46,37 +30,25 @@ fn uploading_files_works(tmpdir: TempDir, port: u16) -> Result<(), Error> {
let client = Client::new();
client
- .post(format!("http://localhost:{}{}", port, upload_action).as_str())
+ .post(server.url().join(upload_action)?)
.multipart(form)
.send()?
.error_for_status()?;
// After uploading, check whether the uploaded file is now getting listed.
- let body = reqwest::blocking::get(format!("http://localhost:{}", port).as_str())?;
+ let body = reqwest::blocking::get(server.url())?;
let parsed = Document::from_read(body)?;
assert!(parsed.find(Text).any(|x| x.text() == test_file_name));
- child.kill()?;
-
Ok(())
}
#[rstest]
-fn uploading_files_is_prevented(tmpdir: TempDir, port: u16) -> Result<(), Error> {
+fn uploading_files_is_prevented(server: TestServer) -> 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::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)?;
assert!(parsed.find(Text).all(|x| x.text() != test_file_name));
@@ -93,18 +65,16 @@ fn uploading_files_is_prevented(tmpdir: TempDir, port: u16) -> Result<(), Error>
let client = Client::new();
// Ensure uploading fails and returns an error
assert!(client
- .post(format!("http://localhost:{}{}", port, "/upload?path=/").as_str())
+ .post(server.url().join("/upload?path=/")?)
.multipart(form)
.send()?
.error_for_status()
.is_err());
// After uploading, check whether the uploaded file is now getting listed.
- let body = reqwest::blocking::get(format!("http://localhost:{}", port).as_str())?;
+ let body = reqwest::blocking::get(server.url())?;
let parsed = Document::from_read(body)?;
assert!(!parsed.find(Text).any(|x| x.text() == test_file_name));
- child.kill()?;
-
Ok(())
}