aboutsummaryrefslogtreecommitdiffstats
path: root/askama_gotham/tests/basic.rs
blob: fbb61a0ecb1f2830fc0b576f9d39f4cc58dfe69b (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
use askama::Template;
use gotham::state::State;
use gotham::test::TestServer;
use hyper::StatusCode;

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

fn hello(state: State) -> (State, HelloTemplate<'static>) {
    (state, HelloTemplate { name: "world" })
}

#[test]
fn test_gotham() {
    let test_server = TestServer::new(|| Ok(hello)).expect("Failed to mount test router");

    let res = test_server
        .client()
        .get("http://localhost/")
        .perform()
        .expect("Failed to send request to gotham");

    assert_eq!(res.status(), StatusCode::OK);
    {
        let headers = res.headers();
        let content_type = headers
            .get("content-type")
            .expect("Response did not contain content-type header");
        assert_eq!(
            content_type.to_str().unwrap(),
            mime::TEXT_HTML_UTF_8.to_string()
        );
    }

    let body = res.read_utf8_body().expect("failed to read response body");
    assert_eq!(&body, "Hello, world!");
}