mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 16:02:59 +01:00
allow to override status code for NamedFile
This commit is contained in:
parent
89bf12605d
commit
37db7d8168
27
src/fs.rs
27
src/fs.rs
@ -36,6 +36,7 @@ pub struct NamedFile {
|
|||||||
modified: Option<SystemTime>,
|
modified: Option<SystemTime>,
|
||||||
cpu_pool: Option<CpuPool>,
|
cpu_pool: Option<CpuPool>,
|
||||||
only_get: bool,
|
only_get: bool,
|
||||||
|
status_code: StatusCode,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NamedFile {
|
impl NamedFile {
|
||||||
@ -54,7 +55,9 @@ impl NamedFile {
|
|||||||
let path = path.as_ref().to_path_buf();
|
let path = path.as_ref().to_path_buf();
|
||||||
let modified = md.modified().ok();
|
let modified = md.modified().ok();
|
||||||
let cpu_pool = None;
|
let cpu_pool = None;
|
||||||
Ok(NamedFile{path, file, md, modified, cpu_pool, only_get: false})
|
Ok(NamedFile{path, file, md, modified, cpu_pool,
|
||||||
|
only_get: false,
|
||||||
|
status_code: StatusCode::OK})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allow only GET and HEAD methods
|
/// Allow only GET and HEAD methods
|
||||||
@ -96,6 +99,12 @@ impl NamedFile {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set response **Status Code**
|
||||||
|
pub fn set_status_code(mut self, status: StatusCode) -> Self {
|
||||||
|
self.status_code = status;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
fn etag(&self) -> Option<header::EntityTag> {
|
fn etag(&self) -> Option<header::EntityTag> {
|
||||||
// This etag format is similar to Apache's.
|
// This etag format is similar to Apache's.
|
||||||
self.modified.as_ref().map(|mtime| {
|
self.modified.as_ref().map(|mtime| {
|
||||||
@ -207,7 +216,7 @@ impl Responder for NamedFile {
|
|||||||
false
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut resp = HttpResponse::Ok();
|
let mut resp = HttpResponse::build(self.status_code);
|
||||||
|
|
||||||
resp
|
resp
|
||||||
.if_some(self.path().extension(), |ext, resp| {
|
.if_some(self.path().extension(), |ext, resp| {
|
||||||
@ -509,6 +518,20 @@ mod tests {
|
|||||||
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml")
|
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_named_file_status_code() {
|
||||||
|
let mut file = NamedFile::open("Cargo.toml").unwrap()
|
||||||
|
.set_status_code(StatusCode::NOT_FOUND)
|
||||||
|
.set_cpu_pool(CpuPool::new(1));
|
||||||
|
{ file.file();
|
||||||
|
let _f: &File = &file; }
|
||||||
|
{ let _f: &mut File = &mut file; }
|
||||||
|
|
||||||
|
let resp = file.respond_to(HttpRequest::default()).unwrap();
|
||||||
|
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml");
|
||||||
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_named_file_not_allowed() {
|
fn test_named_file_not_allowed() {
|
||||||
let req = TestRequest::default().method(Method::POST).finish();
|
let req = TestRequest::default().method(Method::POST).finish();
|
||||||
|
Loading…
Reference in New Issue
Block a user