From 5decff9154c48998e667e18e3f21bb9f0775f62a Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 3 Dec 2017 18:15:09 -0800 Subject: [PATCH] added fs tests --- guide/src/qs_12.md | 40 ++++++++++++++++++++++++++++++++++++++++ src/error.rs | 2 +- src/fs.rs | 35 +++++++++++++++++++++++++++++++++++ src/httpresponse.rs | 4 +++- 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/guide/src/qs_12.md b/guide/src/qs_12.md index 8220368dd..dd7f6acff 100644 --- a/guide/src/qs_12.md +++ b/guide/src/qs_12.md @@ -1 +1,41 @@ # Static file handling + +## Individual file + +It is possible to serve static files with tail path pattern and `NamedFile`. + +```rust +extern crate actix_web; +use actix_web::*; +use std::path::PathBuf; + +fn index(req: HttpRequest) -> Result { + let path: PathBuf = req.match_info().query("tail")?; + Ok(fs::NamedFile::open(path)?) +} + +fn main() { + Application::default("/") + .resource(r"/a/{tail:*}", |r| r.get(index)) + .finish(); +} +``` + +## Directory + +To serve all files from specific directory `StaticFiles` type could be used. +`StaticFiles` could be registered with `Application::route` method. + +```rust +extern crate actix_web; + +fn main() { + actix_web::Application::default("/") + .route("/static", actix_web::fs::StaticFiles::new(".", true)) + .finish(); +} +``` + +First parameter is a base directory. Second parameter is `show_index`, if it set to *true* +directory listing would be returned for directories, if it is set to *false* +then *404 Not Found* would be returned instead of directory listing. diff --git a/src/error.rs b/src/error.rs index 8ed5ccf97..053df2d7e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -28,7 +28,7 @@ use httpcodes::{HTTPBadRequest, HTTPMethodNotAllowed, HTTPExpectationFailed}; /// /// This typedef is generally used to avoid writing out `actix_web::error::Error` directly and /// is otherwise a direct mapping to `Result`. -pub type Result = result::Result; +pub type Result = result::Result; /// General purpose actix web error #[derive(Fail, Debug)] diff --git a/src/fs.rs b/src/fs.rs index 84da53c28..df275ea83 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -270,3 +270,38 @@ impl Handler for StaticFiles { } } } + +#[cfg(test)] +mod tests { + use super::*; + use http::header; + + #[test] + fn test_named_file() { + assert!(NamedFile::open("test--").is_err()); + let mut file = NamedFile::open("Cargo.toml").unwrap(); + { file.file(); + let _f: &File = &file; } + { let _f: &mut File = &mut file; } + + let resp = file.from_request(HttpRequest::default()).unwrap(); + assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml") + } + + #[test] + fn test_static_files() { + let mut st = StaticFiles::new(".", true); + st.accessible = false; + assert!(st.handle(HttpRequest::default()).is_err()); + + st.accessible = true; + st.show_index = false; + assert!(st.handle(HttpRequest::default()).is_err()); + + st.show_index = true; + let resp = st.handle(HttpRequest::default()).from_request(HttpRequest::default()).unwrap(); + assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/html; charset=utf-8"); + assert!(resp.body().is_binary()); + assert!(format!("{:?}", resp.body()).contains("README.md")); + } +} diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 21663853c..da26c6789 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -600,7 +600,9 @@ mod tests { .version(Version::HTTP_10) .finish().unwrap(); assert_eq!(resp.version(), Some(Version::HTTP_10)); - assert_eq!(resp.status(), StatusCode::NO_CONTENT) + assert_eq!(resp.status(), StatusCode::NO_CONTENT); + + let _t = format!("{:?}", resp); } #[test]