From 2ffdcdf10666585e7864027e52ef73505f1ad176 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 2 Jul 2022 23:25:57 +0800 Subject: [PATCH] feat: allow search with --render-try-index (#88) --- src/server.rs | 15 ++++++++++++--- tests/render.rs | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/server.rs b/src/server.rs index 356c6ef..577cc67 100644 --- a/src/server.rs +++ b/src/server.rs @@ -155,9 +155,18 @@ impl Server { match method { Method::GET | Method::HEAD => { if is_dir { - if render_try_index && query == "zip" { - self.handle_zip_dir(path, head_only, &mut res).await?; - } else if render_index || render_spa || render_try_index { + if render_try_index { + if query == "zip" { + self.handle_zip_dir(path, head_only, &mut res).await?; + } else if allow_search && query.starts_with("q=") { + let q = decode_uri(&query[2..]).unwrap_or_default(); + self.handle_search_dir(path, &q, head_only, &mut res) + .await?; + } else { + self.handle_render_index(path, headers, head_only, &mut res) + .await?; + } + } else if render_index || render_spa { self.handle_render_index(path, headers, head_only, &mut res) .await?; } else if query == "zip" { diff --git a/tests/render.rs b/tests/render.rs index 9ecfd8e..29644c6 100644 --- a/tests/render.rs +++ b/tests/render.rs @@ -50,6 +50,18 @@ fn render_try_index3(#[with(&["--render-try-index"])] server: TestServer) -> Res Ok(()) } +#[rstest] +#[case(server(&["--render-try-index"] as &[&str]), false)] +#[case(server(&["--render-try-index", "--allow-search"] as &[&str]), true)] +fn render_try_index4(#[case] server: TestServer, #[case] searched: bool) -> Result<(), Error> { + let resp = reqwest::blocking::get(format!("{}{}?q={}", server.url(), DIR_NO_INDEX, "😀.bin"))?; + assert_eq!(resp.status(), 200); + let paths = utils::retrive_index_paths(&resp.text()?); + assert!(!paths.is_empty()); + assert_eq!(paths.iter().all(|v| v.contains("😀.bin")), searched); + Ok(()) +} + #[rstest] fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> { let resp = reqwest::blocking::get(server.url())?;