use mimeograph_router::*; type Result = std::result::Result>; #[get("/foo/bar/baz")] pub fn foo_bar_baz(_request: http::Request) -> Result> { http::Response::builder() .body(String::from("hello world")) .map_err(Into::into) } #[get("/bar/baz/:qux")] pub fn bar_baz_qux(_request: http::Request, qux: &str) -> Result> { http::Response::builder() .body(qux.to_owned()) .map_err(Into::into) } #[get("/foo/*/bar/:qux")] pub fn wild_foo_bar(_request: http::Request, qux: &str) -> Result> { http::Response::builder() .body(qux.to_owned()) .map_err(Into::into) } #[get("/foo/:bar/baz/**")] pub fn glob_foo(_request: http::Request, text: &str) -> Result> { http::Response::builder() .body(text.into()) .map_err(Into::into) } #[get("/**/:text/*/foo")] pub fn foo_glob(_request: http::Request, text: &str) -> Result> { http::Response::builder() .body(text.into()) .map_err(Into::into) } #[get("/**")] pub fn glob(_request: http::Request) -> Result> { http::Response::builder() .body(String::from("test")) .map_err(Into::into) } #[test] fn match_get() { let request = http::Request::get("http://mimeograph.roosterbockets.com/foo/bar/baz") .body(String::new()) .unwrap(); // match the second route let routes = routes![bar_baz_qux, foo_bar_baz]; let response = match router(request, routes) { RouterResult::Handled(response) => response, RouterResult::NotHandled(_) => panic!("should have been handled!"), }; let body = response.unwrap().into_body(); assert_eq!("hello world", body); } #[test] fn match_get_with_arg() { let request = http::Request::get("http://mimeograph.roosterbockets.com/bar/baz/aoeu") .body(String::new()) .unwrap(); let routes = routes![bar_baz_qux, foo_bar_baz]; let response = match router(request, routes) { RouterResult::Handled(response) => response, RouterResult::NotHandled(_) => panic!("should have been handled!"), }; let body = response.unwrap().into_body(); assert_eq!("aoeu", body); } #[test] fn match_wildcard() { let request = http::Request::get("http://mimeograph.roosterbockets.com/foo/hello/bar/world") .body(String::new()) .unwrap(); let routes = routes![wild_foo_bar]; let response = match router(request, routes) { RouterResult::Handled(response) => response, RouterResult::NotHandled(_) => panic!("should have been handled!"), }; let body = response.unwrap().into_body(); assert_eq!("world", body); } #[test] fn match_glob() { let request = http::Request::get("http://mimeograph.roosterbockets.com/this/is/some/test") .body(String::new()) .unwrap(); let routes = routes![glob]; let response = match router(request, routes) { RouterResult::Handled(response) => response, RouterResult::NotHandled(_) => panic!("should have been handled!"), }; let body = response.unwrap().into_body(); assert_eq!("test", body); } #[test] fn match_globby() { let request = http::Request::get("http://mimeograph.roosterbockets.com/foo/is/some/test") .body(String::new()) .unwrap(); let routes = routes![glob]; let response = match router(request, routes) { RouterResult::Handled(response) => response, RouterResult::NotHandled(_) => panic!("should have been handled!"), }; let body = response.unwrap().into_body(); assert_eq!("test", body); } #[test] fn match_glob_foo() { let request = http::Request::get("http://mimeograph.roosterbockets.com/foo/bar/baz/this/is/some/glob") .body(String::new()) .unwrap(); let routes = routes![glob_foo]; let response = match router(request, routes) { RouterResult::Handled(response) => response, RouterResult::NotHandled(_) => panic!("should have been handled!"), }; let body = response.unwrap().into_body(); assert_eq!("bar", body); } #[test] fn match_foo_glob() { let request = http::Request::get("http://mimeograph.roosterbockets.com/this/is/a/test/foo") .body(String::new()) .unwrap(); let routes = routes![foo_glob]; let response = match router(request, routes) { RouterResult::Handled(response) => response, RouterResult::NotHandled(_) => panic!("should have been handled!"), }; let body = response.unwrap().into_body(); assert_eq!("a", body); }