From 35e1ba60aaaddb3428ba183e89c3de9d02255dff Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Sun, 17 Oct 2021 04:02:08 +0900 Subject: [PATCH] Fix Some Router Behaviour (#2107) * Add Redirect Comp. * Fix router behaviour. * Fix output. * Fix pr-flow. * Remove Redirect. * Readd 77b46bf. --- packages/yew-router/src/macro_helpers.rs | 21 ++++---- .../yew-router/tests/router_unit_tests.rs | 50 +++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 packages/yew-router/tests/router_unit_tests.rs diff --git a/packages/yew-router/src/macro_helpers.rs b/packages/yew-router/src/macro_helpers.rs index e29cc0dc874..9e136d9655e 100644 --- a/packages/yew-router/src/macro_helpers.rs +++ b/packages/yew-router/src/macro_helpers.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use crate::utils::{base_url, strip_slash_suffix}; use crate::Routable; @@ -9,16 +11,14 @@ pub fn build_router() -> Router { let base = base_url(); let mut router = Router::new(); R::routes().iter().for_each(|path| { - match &base { - Some(base) => { - let route = format!("{}{}", base, path); - let route = strip_slash_suffix(&route); - router.add(route, path.to_string()); - } - _ => { - router.add(path, path.to_string()); - } + let route = match base { + Some(ref base) => Cow::from(format!("{}{}", base, path)), + None => (*path).into(), }; + + let stripped_route = strip_slash_suffix(&route); + + router.add(stripped_route, path.to_string()); }); router @@ -30,7 +30,8 @@ pub fn recognize_with_router(router: &Router, pathname: &str) -> Op let matched = router.recognize(pathname); match matched { - Ok(matched) => R::from_path(matched.handler(), &matched.params().into_iter().collect()), + Ok(matched) => R::from_path(matched.handler(), &matched.params().into_iter().collect()) + .or_else(R::not_found_route), Err(_) => R::not_found_route(), } } diff --git a/packages/yew-router/tests/router_unit_tests.rs b/packages/yew-router/tests/router_unit_tests.rs new file mode 100644 index 00000000000..869060643eb --- /dev/null +++ b/packages/yew-router/tests/router_unit_tests.rs @@ -0,0 +1,50 @@ +use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; +use yew_router::prelude::*; + +wasm_bindgen_test_configure!(run_in_browser); + +#[test] +fn router_always_404() { + #[derive(Routable, Debug, Clone, PartialEq)] + enum AppRoute { + #[at("/")] + Home, + #[at("/:id")] + Article { id: u64 }, + #[at("/404")] + #[not_found] + NotFound, + } + + assert_eq!( + Some(AppRoute::NotFound), + AppRoute::recognize("/not/matched/route") + ); + assert_eq!( + Some(AppRoute::NotFound), + AppRoute::recognize("/not-matched-route") + ); +} + +#[test] +fn router_trailing_slash() { + #[derive(Routable, Debug, Clone, PartialEq)] + enum AppRoute { + #[at("/")] + Home, + #[at("/category/:name/")] + Category { name: String }, + #[at("/:id")] + Article { id: u64 }, + #[at("/404")] + #[not_found] + NotFound, + } + + assert_eq!( + Some(AppRoute::Category { + name: "cooking-recipes".to_string() + }), + AppRoute::recognize("/category/cooking-recipes/") + ); +}