mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-30 06:21:13 +00:00
37 lines
859 B
Rust
37 lines
859 B
Rust
use axum::{
|
|
body::Body,
|
|
http::{Request, StatusCode},
|
|
routing::get,
|
|
Router,
|
|
};
|
|
use http_body_util::BodyExt;
|
|
use rinja_axum::Template;
|
|
use tower::util::ServiceExt;
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "hello.html")]
|
|
struct HelloTemplate<'a> {
|
|
name: &'a str,
|
|
}
|
|
|
|
async fn hello() -> HelloTemplate<'static> {
|
|
HelloTemplate { name: "world" }
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn template_to_response() {
|
|
let app = Router::new().route("/", get(hello));
|
|
|
|
let res = app
|
|
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let headers = res.headers();
|
|
assert_eq!(headers["Content-Type"], "text/html; charset=utf-8");
|
|
|
|
let body = res.into_body().collect().await.unwrap().to_bytes();
|
|
assert_eq!(&body[..], b"Hello, world!");
|
|
}
|