diff options
author | jikstra <jikstra@disroot.org> | 2021-04-25 15:48:09 +0000 |
---|---|---|
committer | jikstra <jikstra@disroot.org> | 2021-09-01 19:08:00 +0000 |
commit | 06db56e40820ec0067d18840648ee786163a3862 (patch) | |
tree | 638eb60a484ba0b17a8373703141e19812dd0859 /tests/raw.rs | |
parent | Add CHANGELOG entry for printing QR codes on terminal (diff) | |
download | miniserve-06db56e40820ec0067d18840648ee786163a3862.tar.gz miniserve-06db56e40820ec0067d18840648ee786163a3862.zip |
Implement a raw rendering mode for recursive folder download
- Raw mode only displays file/folders and is more focused on
computer processing
- Display a banner in footer to recursively download the
current folder with wget
Diffstat (limited to 'tests/raw.rs')
-rw-r--r-- | tests/raw.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/tests/raw.rs b/tests/raw.rs new file mode 100644 index 0000000..5c2227b --- /dev/null +++ b/tests/raw.rs @@ -0,0 +1,103 @@ +mod fixtures; +mod utils; + +use crate::fixtures::TestServer; +use assert_cmd::prelude::*; +use assert_fs::fixture::TempDir; +use fixtures::{port, server, tmpdir, Error}; +use pretty_assertions::assert_eq; +use reqwest::blocking::Client; +use rstest::rstest; +use select::document::Document; +use select::predicate::Class; +use select::predicate::Name; +use std::process::{Command, Stdio}; +use std::thread::sleep; +use std::time::Duration; + +#[rstest] +/// The ui displays the correct wget command to download the folder recursively +fn ui_displays_wget_element(server: TestServer) -> Result<(), Error> { + let client = Client::new(); + + let body = client.get(server.url()).send()?.error_for_status()?; + let parsed = Document::from_read(body)?; + let wget_url = parsed + .find(Class("downloadWget")) + .next() + .unwrap() + .find(Class("cmd")) + .next() + .unwrap() + .text(); + assert_eq!( + wget_url, + format!( + "wget -r -c -nH -np --cut-dirs=0 -R \"index.html*\" \"{}?raw=true\"", + server.url() + ) + ); + + let body = client + .get(format!("{}/very/deeply/nested/", server.url())) + .send()? + .error_for_status()?; + let parsed = Document::from_read(body)?; + let wget_url = parsed + .find(Class("downloadWget")) + .next() + .unwrap() + .find(Class("cmd")) + .next() + .unwrap() + .text(); + assert_eq!( + wget_url, + format!( + "wget -r -c -nH -np --cut-dirs=2 -R \"index.html*\" \"{}very/deeply/nested/?raw=true\"", + server.url() + ) + ); + + Ok(()) +} + +#[rstest] +/// All hrefs in raw mode are links to directories or files & directories end with ?raw=true +fn raw_mode_links_to_directories_end_with_raw_true(server: TestServer) -> Result<(), Error> { + fn verify_a_tags(parsed: Document) { + // Ensure all links end with ?raw=true or are files + for node in parsed.find(Name("a")) { + let class = node.attr("class").unwrap(); + + if class == "root" || class == "directory" { + assert!(node.attr("href").unwrap().ends_with("?raw=true")); + } else if class == "file" { + assert!(true); + } else { + println!( + "This node is a link and neither of class directory, root or file: {:?}", + node + ); + assert!(false); + } + } + } + + let urls = [ + format!("{}?raw=true", server.url()), + format!("{}very?raw=true", server.url()), + format!("{}very/deeply/?raw=true", server.url()), + format!("{}very/deeply/nested?raw=true", server.url()), + ]; + + let client = Client::new(); + // Ensure the links to the archives are not present + for url in urls.iter() { + let body = client.get(url).send()?.error_for_status()?; + let parsed = Document::from_read(body)?; + verify_a_tags(parsed); + } + + Ok(()) +} |