aboutsummaryrefslogtreecommitdiffstats
path: root/askama_hyper/tests/basic.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2022-07-20 21:18:18 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-07-25 11:39:58 +0200
commitbd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918 (patch)
treefdca7dca4591f62fca30d798b37779f6fadae00a /askama_hyper/tests/basic.rs
parentc5fbd2ebfb0ce84a49db47f09caa12a048ea61d0 (diff)
downloadaskama-bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918.tar.gz
askama-bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918.tar.bz2
askama-bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918.zip
Implement basic hyper integration
The integration is based on askama_gotham. There is no specific trait to convert an arbitrary T to `hyper::Response`, so I used `From<Template> for hyper::Response`.
Diffstat (limited to 'askama_hyper/tests/basic.rs')
-rw-r--r--askama_hyper/tests/basic.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/askama_hyper/tests/basic.rs b/askama_hyper/tests/basic.rs
new file mode 100644
index 0000000..d0038c1
--- /dev/null
+++ b/askama_hyper/tests/basic.rs
@@ -0,0 +1,70 @@
+use std::convert::Infallible;
+
+use askama::Template;
+use hyper::body::to_bytes;
+use hyper::{Body, Client, Request, Response, Server};
+use routerify::ext::RequestExt;
+use routerify::{Router, RouterService};
+
+#[derive(Template)]
+#[template(path = "hello.html")]
+struct HelloTemplate<'a, N>
+where
+ N: std::fmt::Display,
+{
+ name: &'a N,
+}
+
+async fn hello_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> {
+ let name = req.param("name").unwrap();
+ let template = &HelloTemplate { name: &name };
+ Ok(template.into())
+}
+
+fn router() -> Router<Body, Infallible> {
+ Router::builder()
+ .get("/hello/:name", hello_handler)
+ .build()
+ .unwrap()
+}
+
+#[tokio::test]
+async fn test_hyper() {
+ let addr = ([127, 0, 0, 1], 0).into();
+ let service = RouterService::new(router()).expect("Could not create service");
+ let server = Server::bind(&addr).serve(service);
+ let local_addr = server.local_addr();
+
+ let (tx, rx) = tokio::sync::oneshot::channel::<()>();
+ let serve = async move {
+ let server = server.with_graceful_shutdown(async {
+ rx.await.expect("Could not await signal to stop");
+ });
+ server.await.expect("Could not serve");
+ };
+ let query = async move {
+ let uri = format!("http://{}/hello/world", local_addr)
+ .parse()
+ .expect("Could not format URI");
+ let client = Client::new();
+
+ let res = client.get(uri).await.expect("Could not query client");
+ assert_eq!(res.status(), hyper::StatusCode::OK);
+
+ let content_type = res
+ .headers()
+ .get("content-type")
+ .expect("Response did not contain content-type header")
+ .to_str()
+ .expect("Content-type was not a UTF-8 string");
+ assert_eq!(content_type, mime::TEXT_HTML_UTF_8.to_string());
+
+ let body = to_bytes(res).await.expect("No body returned");
+ let body = std::str::from_utf8(&body).expect("Body was not UTF-8");
+ assert_eq!(body, "Hello, world!");
+
+ tx.send(()).unwrap();
+ };
+
+ tokio::join!(serve, query);
+}