aboutsummaryrefslogtreecommitdiffstats
path: root/askama_actix/tests/basic.rs
blob: 615e324464398cebd230018af445c0040370f378 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use actix_web::http::header::CONTENT_TYPE;
use actix_web::web;
use askama_actix::{Template, TemplateToResponse};
use bytes::Bytes;

#[derive(Template)]
#[template(path = "hello.html")]
struct HelloTemplate<'a> {
    name: &'a str,
}

#[actix_rt::test]
async fn test_actix_web() {
    let srv = actix_test::start(|| {
        actix_web::App::new()
            .service(web::resource("/").to(|| async { HelloTemplate { name: "world" } }))
    });

    let request = srv.get("/");
    let mut response = request.send().await.unwrap();
    assert!(response.status().is_success());
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "text/html; charset=utf-8"
    );

    let bytes = response.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref()));
}

#[actix_rt::test]
async fn test_actix_web_responder() {
    let srv = actix_test::start(|| {
        actix_web::App::new().service(web::resource("/").to(|| async {
            let name = "world".to_owned();
            HelloTemplate { name: &name }.to_response()
        }))
    });

    let request = srv.get("/");
    let mut response = request.send().await.unwrap();
    assert!(response.status().is_success());
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "text/html; charset=utf-8"
    );

    let bytes = response.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref()));
}