diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/serve_request.rs | 28 | ||||
-rw-r--r-- | tests/upload_files.rs | 46 |
2 files changed, 73 insertions, 1 deletions
diff --git a/tests/serve_request.rs b/tests/serve_request.rs index 7419ab1..064e196 100644 --- a/tests/serve_request.rs +++ b/tests/serve_request.rs @@ -55,7 +55,10 @@ fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<( .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()); + assert!(dir_body_parsed + .find(|x: &Node| x.text() == file) + .next() + .is_some()); } } @@ -63,3 +66,26 @@ fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<( Ok(()) } + +#[rstest] +fn serves_requests_custom_index_notice(tmpdir: TempDir, port: u16) -> Result<(), Error> { + let mut child = Command::cargo_bin("miniserve")? + .arg("--index=not.html") + .arg("-p") + .arg(port.to_string()) + .arg(tmpdir.path()) + .stdout(Stdio::piped()) + .spawn()?; + + sleep(Duration::from_secs(1)); + + child.kill()?; + let output = child.wait_with_output().expect("Failed to read stdout"); + let all_text = String::from_utf8(output.stdout); + + assert!(all_text + .unwrap() + .contains("The provided index file could not be found")); + + Ok(()) +} 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(()) +} |