HttpServer::new(|| {
App::new()
.service(hello)
+ .service(actix_files::Files::new("/dir", "../static"))
.wrap(Logger::default())
})
.bind(("127.0.0.1", 3030))?
#[cfg(test)]
mod tests {
use actix_web::{App, body, test, web};
- use actix_web::http::header;
+ use actix_web::http::{header, StatusCode};
use super::{hello};
#[actix_web::test]
async fn test_hello() {
+ // FIXME: duplicating the .service() call from main() here is super ugly, but it's hard to move that into a fn
let app = test::init_service(App::new().service(hello)).await;
// no user-agent
assert_eq!(body::to_bytes(res.into_body()).await.unwrap(),
web::Bytes::from_static(b"Hello rust from TestBrowser 0.1!"));
}
+
+ #[actix_web::test]
+ async fn test_static_dir() {
+ // FIXME: duplicating the .service() call from main() here is super ugly, but it's hard to move that into a fn
+ let app = test::init_service(App::new().service(actix_files::Files::new("/dir", "../static"))).await;
+
+ let req = test::TestRequest::get().uri("/dir/plain.txt").to_request();
+ let res = test::call_service(&app, req).await;
+ assert!(res.status().is_success());
+ assert_eq!(body::to_bytes(res.into_body()).await.unwrap(),
+ web::Bytes::from_static(b"Hello world! This is uncompressed text.\n"));
+
+ // subdir
+ let req = test::TestRequest::get().uri("/dir/dir1/optzip.txt").to_request();
+ let res = test::call_service(&app, req).await;
+ assert!(res.status().is_success());
+ assert_eq!(body::to_bytes(res.into_body()).await.unwrap(),
+ web::Bytes::from_static(b"This file is available uncompressed or compressed\n\
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"));
+
+ // does not support transparent decompression
+ let req = test::TestRequest::get().uri("/dir/onlycompressed.txt").to_request();
+ let res = test::call_service(&app, req).await;
+ assert_eq!(res.status(), StatusCode::NOT_FOUND);
+ }
}