summaryrefslogtreecommitdiffstats
path: root/third_party/rust/warp/tests/header.rs
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/warp/tests/header.rs')
-rw-r--r--third_party/rust/warp/tests/header.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/third_party/rust/warp/tests/header.rs b/third_party/rust/warp/tests/header.rs
new file mode 100644
index 0000000000..ea882b44a1
--- /dev/null
+++ b/third_party/rust/warp/tests/header.rs
@@ -0,0 +1,71 @@
+#![deny(warnings)]
+use warp::Filter;
+
+#[tokio::test]
+async fn exact() {
+ let _ = pretty_env_logger::try_init();
+
+ let host = warp::header::exact("host", "localhost");
+
+ let req = warp::test::request().header("host", "localhost");
+
+ assert!(req.matches(&host).await);
+
+ let req = warp::test::request();
+ assert!(!req.matches(&host).await, "header missing");
+
+ let req = warp::test::request().header("host", "hyper.rs");
+ assert!(!req.matches(&host).await, "header value different");
+}
+
+#[tokio::test]
+async fn exact_rejections() {
+ let _ = pretty_env_logger::try_init();
+
+ let host = warp::header::exact("host", "localhost").map(warp::reply);
+
+ let res = warp::test::request()
+ .header("host", "nope")
+ .reply(&host)
+ .await;
+
+ assert_eq!(res.status(), 400);
+ assert_eq!(res.body(), "Invalid request header \"host\"");
+
+ let res = warp::test::request()
+ .header("not-even-a-host", "localhost")
+ .reply(&host)
+ .await;
+
+ assert_eq!(res.status(), 400);
+ assert_eq!(res.body(), "Missing request header \"host\"");
+}
+
+#[tokio::test]
+async fn optional() {
+ let _ = pretty_env_logger::try_init();
+
+ let con_len = warp::header::optional::<u64>("content-length");
+
+ let val = warp::test::request()
+ .filter(&con_len)
+ .await
+ .expect("missing header matches");
+ assert_eq!(val, None);
+
+ let val = warp::test::request()
+ .header("content-length", "5")
+ .filter(&con_len)
+ .await
+ .expect("existing header matches");
+
+ assert_eq!(val, Some(5));
+
+ assert!(
+ !warp::test::request()
+ .header("content-length", "boom")
+ .matches(&con_len)
+ .await,
+ "invalid optional header still rejects",
+ );
+}