1 #![deny(warnings)]
2 use warp::Filter;
3
4 #[tokio::test]
exact()5 async fn exact() {
6 let _ = pretty_env_logger::try_init();
7
8 let host = warp::header::exact("host", "localhost");
9
10 let req = warp::test::request().header("host", "localhost");
11
12 assert!(req.matches(&host).await);
13
14 let req = warp::test::request();
15 assert!(!req.matches(&host).await, "header missing");
16
17 let req = warp::test::request().header("host", "hyper.rs");
18 assert!(!req.matches(&host).await, "header value different");
19 }
20
21 #[tokio::test]
exact_rejections()22 async fn exact_rejections() {
23 let _ = pretty_env_logger::try_init();
24
25 let host = warp::header::exact("host", "localhost").map(warp::reply);
26
27 let res = warp::test::request()
28 .header("host", "nope")
29 .reply(&host)
30 .await;
31
32 assert_eq!(res.status(), 400);
33 assert_eq!(res.body(), "Invalid request header \"host\"");
34
35 let res = warp::test::request()
36 .header("not-even-a-host", "localhost")
37 .reply(&host)
38 .await;
39
40 assert_eq!(res.status(), 400);
41 assert_eq!(res.body(), "Missing request header \"host\"");
42 }
43
44 #[tokio::test]
optional()45 async fn optional() {
46 let _ = pretty_env_logger::try_init();
47
48 let con_len = warp::header::optional::<u64>("content-length");
49
50 let val = warp::test::request()
51 .filter(&con_len)
52 .await
53 .expect("missing header matches");
54 assert_eq!(val, None);
55
56 let val = warp::test::request()
57 .header("content-length", "5")
58 .filter(&con_len)
59 .await
60 .expect("existing header matches");
61
62 assert_eq!(val, Some(5));
63
64 assert!(
65 !warp::test::request()
66 .header("content-length", "boom")
67 .matches(&con_len)
68 .await,
69 "invalid optional header still rejects",
70 );
71 }
72