From b933ed445611c752401342a3a020bbf185d2031d Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 3 Feb 2023 21:04:07 -0500 Subject: [PATCH] add tests for files_listing_renderer --- actix-files/src/files.rs | 43 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/actix-files/src/files.rs b/actix-files/src/files.rs index a30ce6fd3..be2a450d2 100644 --- a/actix-files/src/files.rs +++ b/actix-files/src/files.rs @@ -142,7 +142,7 @@ impl Files { self } - /// Set custom directory renderer + /// Set custom directory renderer. pub fn files_listing_renderer(mut self, f: F) -> Self where for<'r, 's> F: @@ -152,7 +152,7 @@ impl Files { self } - /// Specifies mime override callback + /// Specifies MIME override callback. pub fn mime_override(mut self, f: F) -> Self where F: Fn(&mime::Name<'_>) -> DispositionType + 'static, @@ -390,3 +390,42 @@ impl ServiceFactory for Files { } } } + +#[cfg(test)] +mod tests { + use actix_web::{ + http::StatusCode, + test::{self, TestRequest}, + App, HttpResponse, + }; + + use super::*; + + #[actix_web::test] + async fn custom_files_listing_renderer() { + let srv = test::init_service( + App::new().service( + Files::new("/", "./tests") + .show_files_listing() + .files_listing_renderer(|dir, req| { + Ok(ServiceResponse::new( + req.clone(), + HttpResponse::Ok().body(dir.path.to_str().unwrap().to_owned()), + )) + }), + ), + ) + .await; + + let req = TestRequest::with_uri("/").to_request(); + let res = test::call_service(&srv, req).await; + + assert_eq!(res.status(), StatusCode::OK); + let body = test::read_body(res).await; + assert!( + body.ends_with(b"actix-files/tests/"), + "body {:?} does not end with `actix-files/tests/`", + body + ); + } +}