aboutsummaryrefslogtreecommitdiffstats
path: root/askama_mendes/tests/basic.rs
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2021-12-01 06:43:36 -0800
committerLibravatar GitHub <noreply@github.com>2021-12-01 15:43:36 +0100
commite3d690ba102f3624ff65f5476dc68dfe98f2e221 (patch)
tree873c6957e727befe1103633a3b5559451e66f743 /askama_mendes/tests/basic.rs
parent671bc7f32fd150f04793d8d5dbe806e9d4cfe669 (diff)
downloadaskama-e3d690ba102f3624ff65f5476dc68dfe98f2e221.tar.gz
askama-e3d690ba102f3624ff65f5476dc68dfe98f2e221.tar.bz2
askama-e3d690ba102f3624ff65f5476dc68dfe98f2e221.zip
Move askama_mendes integration into Askama repo (#561)
Diffstat (limited to '')
-rw-r--r--askama_mendes/tests/basic.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/askama_mendes/tests/basic.rs b/askama_mendes/tests/basic.rs
new file mode 100644
index 0000000..2a07c13
--- /dev/null
+++ b/askama_mendes/tests/basic.rs
@@ -0,0 +1,96 @@
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use hyper::body::to_bytes;
+use hyper::{Body, Request};
+use mendes::application::Responder;
+use mendes::http::request::Parts;
+use mendes::http::{Response, StatusCode};
+use mendes::{handler, route, Application, Context};
+
+use askama::Template;
+
+#[tokio::test]
+async fn test() {
+ let req = Request::builder().body(()).unwrap();
+ let rsp = App::handle(Context::new(Arc::new(App), req)).await;
+ let (rsp, body) = rsp.into_parts();
+ assert_eq!(
+ rsp.headers
+ .get("content-type")
+ .and_then(|hv| hv.to_str().ok()),
+ Some("text/plain")
+ );
+ assert_eq!(to_bytes(body).await.unwrap(), &b"Hello, world!"[..]);
+}
+
+#[handler(GET)]
+async fn hello(_: &App) -> Result<HelloTemplate<'static>, Error> {
+ Ok(HelloTemplate { name: "world" })
+}
+
+#[derive(Template)]
+#[template(path = "hello.txt")]
+struct HelloTemplate<'a> {
+ name: &'a str,
+}
+
+struct App;
+
+#[async_trait]
+impl Application for App {
+ type RequestBody = ();
+ type ResponseBody = Body;
+ type Error = Error;
+
+ async fn handle(mut cx: Context<Self>) -> Response<Body> {
+ route!(match cx.path() {
+ _ => hello,
+ })
+ }
+}
+
+#[derive(Debug)]
+enum Error {
+ Askama(askama::Error),
+ Mendes(mendes::Error),
+}
+
+impl From<askama::Error> for Error {
+ fn from(e: askama::Error) -> Error {
+ Error::Askama(e)
+ }
+}
+
+impl From<mendes::Error> for Error {
+ fn from(e: mendes::Error) -> Error {
+ Error::Mendes(e)
+ }
+}
+
+impl std::fmt::Display for Error {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ Error::Askama(e) => write!(f, "{}", e),
+ Error::Mendes(e) => write!(f, "{}", e),
+ }
+ }
+}
+
+impl Responder<App> for Error {
+ fn into_response(self, _: &App, _: &Parts) -> Response<Body> {
+ Response::builder()
+ .status(StatusCode::from(&self))
+ .body(self.to_string().into())
+ .unwrap()
+ }
+}
+
+impl From<&Error> for StatusCode {
+ fn from(e: &Error) -> StatusCode {
+ match e {
+ Error::Mendes(e) => StatusCode::from(e),
+ Error::Askama(_) => StatusCode::INTERNAL_SERVER_ERROR,
+ }
+ }
+}