Source code
Revision control
Copy as Markdown
Other Tools
#![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
assert!(req.filter(&filter).await.is_ok());
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")
assert!(req.filter(&filter).await.is_ok());
let req = warp::test::request()
.header("host", "unknown.com")
assert!(req.filter(&filter).await.unwrap_err().is_not_found());
// specified in both - mismatch
let req = warp::test::request()
.header("host", "known.com")
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", "ðŸ˜")
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")
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);
}