From 9d687c6422db40cf2e67c4ed0ce43a63768623cf Mon Sep 17 00:00:00 2001 From: Adam Harbinson Date: Tue, 12 Nov 2024 09:00:03 +0000 Subject: [PATCH 1/2] Fixes head creation on route globbing --- spec/integration_spec.cr | 13 +++++++++++++ src/lucky_router/fragment.cr | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/spec/integration_spec.cr b/spec/integration_spec.cr index 74bffcd..628feff 100644 --- a/spec/integration_spec.cr +++ b/spec/integration_spec.cr @@ -194,6 +194,19 @@ describe LuckyRouter do }) end + it "matches both get and head with route globbing" do + router = LuckyRouter::Matcher(Symbol).new + router.add("get", "/posts/something/*", :post_index) + + router.match!("get", "/posts/something/1").params.should eq({ + "glob" => "1", + }) + + router.match!("head", "/posts/something/1").params.should eq({ + "glob" => "1", + }) + end + it "matches a route with more than 16 segments" do router = LuckyRouter::Matcher(Symbol).new router.add("get", "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/:z", :match) diff --git a/src/lucky_router/fragment.cr b/src/lucky_router/fragment.cr index 838f065..570a51d 100644 --- a/src/lucky_router/fragment.cr +++ b/src/lucky_router/fragment.cr @@ -90,7 +90,9 @@ class LuckyRouter::Fragment(T) end def add_part(path_part : PathPart) : Fragment(T) - if path_part.glob? + if glob_part = self.glob_part + return glob_part + elsif path_part.glob? self.glob_part = Fragment(T).new(path_part: path_part) elsif path_part.path_variable? existing = self.dynamic_parts.find { |fragment| fragment.path_part == path_part } From 18c318fff4d9cf68214db54c7a981eb8b7223114 Mon Sep 17 00:00:00 2001 From: Adam Harbinson Date: Tue, 12 Nov 2024 09:24:12 +0000 Subject: [PATCH 2/2] Fix breaking globs --- src/lucky_router/fragment.cr | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lucky_router/fragment.cr b/src/lucky_router/fragment.cr index 570a51d..8db368e 100644 --- a/src/lucky_router/fragment.cr +++ b/src/lucky_router/fragment.cr @@ -90,10 +90,8 @@ class LuckyRouter::Fragment(T) end def add_part(path_part : PathPart) : Fragment(T) - if glob_part = self.glob_part - return glob_part - elsif path_part.glob? - self.glob_part = Fragment(T).new(path_part: path_part) + if path_part.glob? + self.glob_part ||= Fragment(T).new(path_part: path_part) elsif path_part.path_variable? existing = self.dynamic_parts.find { |fragment| fragment.path_part == path_part } return existing if existing