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