summaryrefslogtreecommitdiffstats
path: root/third_party/rust/warp/tests/host.rs
blob: 2fb5f4247567ba9505f6c6013674d8ef124b923b (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#![deny(warnings)]
use warp::host::Authority;

#[tokio::test]
async fn exact() {
    let filter = warp::host::exact("known.com");

    // no authority
    let req = warp::test::request();
    assert!(req.filter(&filter).await.unwrap_err().is_not_found());

    // specified in URI
    let req = warp::test::request().path("http://known.com/about-us");
    assert!(req.filter(&filter).await.is_ok());

    let req = warp::test::request().path("http://unknown.com/about-us");
    assert!(req.filter(&filter).await.unwrap_err().is_not_found());

    // specified in Host header
    let req = warp::test::request()
        .header("host", "known.com")
        .path("/about-us");
    assert!(req.filter(&filter).await.is_ok());

    let req = warp::test::request()
        .header("host", "unknown.com")
        .path("/about-us");
    assert!(req.filter(&filter).await.unwrap_err().is_not_found());

    // specified in both - matching
    let req = warp::test::request()
        .header("host", "known.com")
        .path("http://known.com/about-us");
    assert!(req.filter(&filter).await.is_ok());

    let req = warp::test::request()
        .header("host", "unknown.com")
        .path("http://unknown.com/about-us");
    assert!(req.filter(&filter).await.unwrap_err().is_not_found());

    // specified in both - mismatch
    let req = warp::test::request()
        .header("host", "known.com")
        .path("http://known2.com/about-us");
    assert!(req
        .filter(&filter)
        .await
        .unwrap_err()
        .find::<warp::reject::InvalidHeader>()
        .is_some());

    // bad host header - invalid chars
    let req = warp::test::request()
        .header("host", "😭")
        .path("http://known.com/about-us");
    assert!(req
        .filter(&filter)
        .await
        .unwrap_err()
        .find::<warp::reject::InvalidHeader>()
        .is_some());

    // bad host header - invalid format
    let req = warp::test::request()
        .header("host", "hello space.com")
        .path("http://known.com//about-us");
    assert!(req
        .filter(&filter)
        .await
        .unwrap_err()
        .find::<warp::reject::InvalidHeader>()
        .is_some());
}

#[tokio::test]
async fn optional() {
    let filter = warp::host::optional();

    let req = warp::test::request().header("host", "example.com");
    assert_eq!(
        req.filter(&filter).await.unwrap(),
        Some(Authority::from_static("example.com"))
    );

    let req = warp::test::request();
    assert_eq!(req.filter(&filter).await.unwrap(), None);
}