aboutsummaryrefslogtreecommitdiff
path: root/tests/config-example/main.rs
blob: 9f084b71f091a2b5c8620f4d470fff1aeda0c64c (plain)
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
51
52
53
54
55
use std::env;
use std::io::Read;
use std::process::Command;

use hyper::{body::aggregate, body::Buf, Body, Client, Method, Request, Uri};

#[path = "../lib.rs"]
mod helpers;
use helpers::ChildExt;

#[tokio::test]
async fn main() {
    let narchttpd_path = env!("CARGO_BIN_EXE_narchttpd");
    let _narchttpd = Command::new(narchttpd_path)
        .current_dir("tests/config-example")
        .spawn()
        .unwrap()
        .kill_on_drop();

    let client = Client::new();

    let get = |uri: &'static str| async {
        let uri: Uri = uri.parse().unwrap();
        let mut uri_parts = uri.into_parts();
        let original_host = uri_parts.authority.take().unwrap();
        uri_parts.authority = "127.0.0.1:1337".parse().ok();
        let real_uri = Uri::from_parts(uri_parts).unwrap();
        let req = Request::builder()
            .method(Method::GET)
            .uri(real_uri)
            .header(hyper::header::HOST, original_host.as_str())
            .body(Body::empty())
            .unwrap();
        let res = client.request(req).await.unwrap();
        assert!(res.status().is_success(), "{:?}", res);
        let body = aggregate(res.into_body()).await.unwrap();
        let mut result = String::new();
        body.reader().read_to_string(&mut result).unwrap();
        result.trim().to_string()
    };

    assert_eq!(
        get("http://domain.test").await,
        "<p>Hello from domain.test</p>"
    );
    assert_eq!(
        get("http://alternate-domain.test").await,
        "<p>Hello from domain.test</p>"
    );
    assert_eq!(
        get("http://sub.domain.test").await,
        "<p>Hello from sub.domain.test</p>"
    );
    assert!(get("http://dynamic.test").await.contains("hello-there.txt"));
}