From 8b7708476f0ff14c5e87f9e2a978763905ec8938 Mon Sep 17 00:00:00 2001 From: bbrtj Date: Thu, 10 Oct 2024 17:58:52 +0200 Subject: [PATCH] Fix bridges behavior broken in 2.18 --- Changes | 1 + lib/Kelp/Routes/Pattern.pm | 4 +++- t/routes_match.t | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Changes b/Changes index 5e54e29..d3aa0fd 100644 --- a/Changes +++ b/Changes @@ -9,6 +9,7 @@ Revision history for Kelp [Changes] - Middleware building is now done using Kelp::Middleware, making it easier to customize - Kelp now uses a dynamic PSGI execution chain, allowing adding PSGI middlewares at route level + - Fixed trailing slash bridges not applying for deeper paths (broken in 2.18) 2.18 - 2024-10-08 [New Interface] diff --git a/lib/Kelp/Routes/Pattern.pm b/lib/Kelp/Routes/Pattern.pm index 32cd621..ba3e683 100644 --- a/lib/Kelp/Routes/Pattern.pm +++ b/lib/Kelp/Routes/Pattern.pm @@ -153,7 +153,9 @@ sub _build_regex # - /test/ matches # - /test/something matches # - /testsomething does not match - $pattern .= '(?:/|$)'; + # if the bridge is already followed by a trailing slash, it's not a + # concern + $pattern .= '(?:/|$)' unless $trailing_slash; } else { diff --git a/t/routes_match.t b/t/routes_match.t index ce37ab5..0dace1d 100644 --- a/t/routes_match.t +++ b/t/routes_match.t @@ -81,6 +81,28 @@ my $r = Kelp::Routes->new; is_deeply _d($r->match('/a/b/c'), 'to'), [{to => 'A::b'}, {to => 'A::d'}]; } +# Bridges - root +{ + $r->clear; + $r->add('' => {to => 'a#b', bridge => 1}); + $r->add('/' => {to => 'a#c', bridge => 1}); + + is_deeply _d($r->match('/'), 'to'), [{to => 'A::b'}, {to => 'A::c'}]; + is_deeply _d($r->match('/test'), 'to'), [{to => 'A::b'}, {to => 'A::c'}]; +} + +# Bridges - trailing slashes +{ + $r->clear; + $r->add('/test' => {to => 'a#b', bridge => 1}); + $r->add('/test/' => {to => 'a#c', bridge => 1}); + + is_deeply _d($r->match('/test'), 'to'), [{to => 'A::b'}]; + is_deeply _d($r->match('/test/'), 'to'), [{to => 'A::b'}, {to => 'A::c'}]; + is_deeply _d($r->match('/test/more'), 'to'), [{to => 'A::b'}, {to => 'A::c'}]; + is_deeply _d($r->match('/test/more/stuff'), 'to'), [{to => 'A::b'}, {to => 'A::c'}]; +} + # Bridges - no longer url parts than defined { $r->clear;