Skip to content

Commit

Permalink
test: added some negative test for download
Browse files Browse the repository at this point in the history
  • Loading branch information
meuter committed Sep 24, 2023
1 parent 42819a8 commit b9dc171
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub(crate) trait Archived {
fn entries(&mut self) -> Result<Entries>;
}

#[derive(Debug)]
pub(crate) enum Storage {
FileOnDisk {
path: PathBuf,
Expand Down
35 changes: 34 additions & 1 deletion tests/download.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,6 +12,34 @@ mod download {
type Error = Box<dyn std::error::Error>;
type Result<T> = std::result::Result<T, Error>;

#[allow(unused)]
async fn test_404(path: impl AsRef<Path>) -> 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<Path>) -> Result<()> {
// read archive contents into buffer
Expand Down Expand Up @@ -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<()> {
Expand Down

0 comments on commit b9dc171

Please sign in to comment.