From b9dc17110e1ac54a6a89ee38dd6d55def63d122f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Meuter?= Date: Sun, 24 Sep 2023 15:13:38 +0200 Subject: [PATCH] test: added some negative test for download --- src/archive.rs | 1 + tests/download.rs | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/archive.rs b/src/archive.rs index 4047a9e..0f987fd 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -77,6 +77,7 @@ pub(crate) trait Archived { fn entries(&mut self) -> Result; } +#[derive(Debug)] pub(crate) enum Storage { FileOnDisk { path: PathBuf, diff --git a/tests/download.rs b/tests/download.rs index a6da2d0..6df90e0 100644 --- a/tests/download.rs +++ b/tests/download.rs @@ -1,7 +1,7 @@ #[cfg(feature = "download")] mod download { - use arkiv::Archive; + use arkiv::{Archive, Error as ArkivError}; use httptest::{matchers::request, responders::status_code, Expectation, Server}; use std::{ fs::File, @@ -12,6 +12,34 @@ mod download { type Error = Box; type Result = std::result::Result; + #[allow(unused)] + async fn test_404(path: impl AsRef) -> Result<()> { + // read archive contents into buffer + let archive_file = File::open(path.as_ref())?; + let mut reader = BufReader::new(archive_file); + let mut buffer = Vec::new(); + reader.read_to_end(&mut buffer)?; + + // prepare test server to return archive contents on request + let server = Server::run(); + server.expect( + Expectation::matching(request::method_path( + "GET", + format!("/{}", path.as_ref().display()), + )) + .respond_with(status_code(404)), + ); + + // download archive + let url = format!("/{}", path.as_ref().display()); + let url = server.url(&url); + + let res = Archive::download(url.to_string()); + assert!(matches!(res, Err(ArkivError::InvalidRequest(_)))); + + Ok(()) + } + #[allow(unused)] async fn test(path: impl AsRef) -> Result<()> { // read archive contents into buffer @@ -45,6 +73,11 @@ mod download { Ok(()) } + #[tokio::test] + async fn download_404() -> Result<()> { + test_404("tests/sample/sample.zip").await + } + #[tokio::test] #[cfg(feature = "zip")] async fn zip_archive() -> Result<()> {