diff options
-rw-r--r-- | askama/src/lib.rs | 1 | ||||
-rw-r--r-- | askama_shared/src/generator.rs | 9 | ||||
-rw-r--r-- | testing/templates/hello.txt | 1 | ||||
-rw-r--r-- | testing/tests/iron.rs | 20 |
4 files changed, 31 insertions, 0 deletions
diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 4d25b1a..ec89998 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -301,6 +301,7 @@ pub mod iron { extern crate iron; pub use self::iron::modifier::Modifier; pub use self::iron::response::Response; + pub use self::iron::headers::ContentType; } #[cfg(feature = "with-rocket")] diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index e4eb9ec..173ecbf 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -251,6 +251,15 @@ impl<'a> Generator<'a> { self.write_header(state, "::askama::iron::Modifier<::askama::iron::Response>", &[]); self.writeln("fn modify(self, res: &mut ::askama::iron::Response) {"); self.writeln("res.body = Some(Box::new(self.render().unwrap().into_bytes()));"); + + let ext = state.input.path.extension().map_or("", |s| s.to_str().unwrap_or("")); + match ext { + "html" | "htm" => { + self.writeln("::askama::iron::ContentType::html().0.modify(res);"); + }, + _ => (), + }; + self.writeln("}"); self.writeln("}"); } diff --git a/testing/templates/hello.txt b/testing/templates/hello.txt new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/testing/templates/hello.txt @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/testing/tests/iron.rs b/testing/tests/iron.rs index 6f8868f..95c366c 100644 --- a/testing/tests/iron.rs +++ b/testing/tests/iron.rs @@ -11,10 +11,30 @@ struct HelloTemplate<'a> { name: &'a str, } +#[derive(Template)] +#[template(path = "hello.txt")] +struct HelloTextTemplate<'a> { + name: &'a str, +} + #[test] fn test_iron() { let rsp = Response::with((status::Ok, HelloTemplate { name: "world" })); let mut buf = Vec::new(); let _ = rsp.body.unwrap().write_body(&mut buf); assert_eq!(buf, b"Hello, world!"); + + let content_type = rsp.headers.get::<iron::headers::ContentType>().unwrap(); + assert_eq!(format!("{}", content_type), "text/html; charset=utf-8"); +} + +#[test] +fn test_iron_non_html() { + let rsp = Response::with((status::Ok, HelloTextTemplate { name: "world" })); + let mut buf = Vec::new(); + let _ = rsp.body.unwrap().write_body(&mut buf); + assert_eq!(buf, b"Hello, world!"); + + let content_type = rsp.headers.get::<iron::headers::ContentType>(); + assert_eq!(content_type, None); } |