summaryrefslogtreecommitdiffstats
path: root/third_party/rust/warp/tests/filter.rs
blob: 5f694188475965b74a5c4c49c84525b6a139858f (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#![deny(warnings)]
use std::convert::Infallible;
use warp::Filter;

#[tokio::test]
async fn flattens_tuples() {
    let _ = pretty_env_logger::try_init();

    let str1 = warp::any().map(|| "warp");
    let true1 = warp::any().map(|| true);
    let unit1 = warp::any();

    // just 1 value
    let ext = warp::test::request().filter(&str1).await.unwrap();
    assert_eq!(ext, "warp");

    // just 1 unit
    let ext = warp::test::request().filter(&unit1).await.unwrap();
    assert_eq!(ext, ());

    // combine 2 values
    let and = str1.and(true1);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, ("warp", true));

    // combine 2 reversed
    let and = true1.and(str1);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, (true, "warp"));

    // combine 1 with unit
    let and = str1.and(unit1);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, "warp");

    let and = unit1.and(str1);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, "warp");

    // combine 3 values
    let and = str1.and(str1).and(true1);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, ("warp", "warp", true));

    // combine 2 with unit
    let and = str1.and(unit1).and(true1);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, ("warp", true));

    let and = unit1.and(str1).and(true1);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, ("warp", true));

    let and = str1.and(true1).and(unit1);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, ("warp", true));

    // nested tuples
    let str_true_unit = str1.and(true1).and(unit1);
    let unit_str_true = unit1.and(str1).and(true1);

    let and = str_true_unit.and(unit_str_true);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, ("warp", true, "warp", true));

    let and = unit_str_true.and(unit1).and(str1).and(str_true_unit);
    let ext = warp::test::request().filter(&and).await.unwrap();
    assert_eq!(ext, ("warp", true, "warp", "warp", true));
}

#[tokio::test]
async fn map() {
    let _ = pretty_env_logger::try_init();

    let ok = warp::any().map(warp::reply);

    let req = warp::test::request();
    let resp = req.reply(&ok).await;
    assert_eq!(resp.status(), 200);
}

#[tokio::test]
async fn or() {
    let _ = pretty_env_logger::try_init();

    // Or can be combined with an infallible filter
    let a = warp::path::param::<u32>();
    let b = warp::any().map(|| 41i32);
    let f = a.or(b);

    let _: Result<_, Infallible> = warp::test::request().filter(&f).await;
}

#[tokio::test]
async fn or_else() {
    let _ = pretty_env_logger::try_init();

    let a = warp::path::param::<u32>();
    let f = a.or_else(|_| async { Ok::<_, warp::Rejection>((44u32,)) });

    assert_eq!(
        warp::test::request().path("/33").filter(&f).await.unwrap(),
        33,
    );
    assert_eq!(warp::test::request().filter(&f).await.unwrap(), 44,);

    // OrElse can be combined with an infallible filter
    let a = warp::path::param::<u32>();
    let f = a.or_else(|_| async { Ok::<_, Infallible>((44u32,)) });

    let _: Result<_, Infallible> = warp::test::request().filter(&f).await;
}

#[tokio::test]
async fn recover() {
    let _ = pretty_env_logger::try_init();

    let a = warp::path::param::<String>();
    let f = a.recover(|err| async move { Err::<String, _>(err) });

    // not rejected
    let resp = warp::test::request().path("/hi").reply(&f).await;
    assert_eq!(resp.status(), 200);
    assert_eq!(resp.body(), "hi");

    // rejected, recovered, re-rejected
    let resp = warp::test::request().reply(&f).await;
    assert_eq!(resp.status(), 404);

    // Recover can be infallible
    let f = a.recover(|_| async move { Ok::<_, Infallible>("shh") });

    let _: Result<_, Infallible> = warp::test::request().filter(&f).await;
}

#[tokio::test]
async fn unify() {
    let _ = pretty_env_logger::try_init();

    let a = warp::path::param::<u32>();
    let b = warp::path::param::<u32>();
    let f = a.or(b).unify();

    let ex = warp::test::request().path("/1").filter(&f).await.unwrap();

    assert_eq!(ex, 1);
}

#[should_panic]
#[tokio::test]
async fn nested() {
    let f = warp::any().and_then(|| async {
        let p = warp::path::param::<u32>();
        warp::test::request().filter(&p).await
    });

    let _ = warp::test::request().filter(&f).await;
}