Debian Codegraph @

third_party/rust/warp/tests/method.rs

raw
1 #![deny(warnings)]
2 use warp::Filter;
3
4 #[tokio::test]
5 async fn method() {
6 let _ = pretty_env_logger::try_init();
7 let get = warp::get().map(warp::reply);
8
9 let req = warp::test::request();
10 assert!(req.matches(&get).await);
11
12 let req = warp::test::request().method("POST");
13 assert!(!req.matches(&get).await);
14
15 let req = warp::test::request().method("POST");
16 let resp = req.reply(&get).await;
17 assert_eq!(resp.status(), 405);
18 }
19
20 #[tokio::test]
21 async fn method_not_allowed_trumps_not_found() {
22 let _ = pretty_env_logger::try_init();
23 let get = warp::get().and(warp::path("hello").map(warp::reply));
24 let post = warp::post().and(warp::path("bye").map(warp::reply));
25
26 let routes = get.or(post);
27
28 let req = warp::test::request().method("GET").path("/bye");
29
30 let resp = req.reply(&routes).await;
31 // GET was allowed, but only for /hello, so POST returning 405 is fine.
32 assert_eq!(resp.status(), 405);
33 }
34
35 #[tokio::test]
36 async fn bad_request_trumps_method_not_allowed() {
37 let _ = pretty_env_logger::try_init();
38 let get = warp::get()
39 .and(warp::path("hello"))
40 .and(warp::header::exact("foo", "bar"))
41 .map(warp::reply);
42 let post = warp::post().and(warp::path("bye")).map(warp::reply);
43
44 let routes = get.or(post);
45
46 let req = warp::test::request().method("GET").path("/hello");
47
48 let resp = req.reply(&routes).await;
49 // GET was allowed, but header rejects with 400, should not
50 // assume POST was the appropriate method.
51 assert_eq!(resp.status(), 400);
52 }