mirror of
https://gitea.invidious.io/iv-org/shard-radix.git
synced 2024-08-15 00:43:21 +00:00
fix(Tree): allows catch all to be used as optional globbing
Catch all parameter was limited to be used *after* a slash (`/`) on a path (ie. `/members/*trailing`). Attempts to use it immediately after the path (ie. `/members*trailing`) was not properly identified or captured. This change ensures catch all can be used as globbing, allowing capture of anything after the matching path. Closes #12
This commit is contained in:
parent
e4c53ba7b9
commit
c8d20afc54
3 changed files with 33 additions and 8 deletions
|
@ -5,6 +5,8 @@ This project aims to comply with [Semantic Versioning](http://semver.org/),
|
||||||
so please check *Changed* and *Removed* notes before upgrading.
|
so please check *Changed* and *Removed* notes before upgrading.
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
### Fixed
|
||||||
|
- Ensure catch all parameter can be used as optional globbing (@jwoertink)
|
||||||
|
|
||||||
## [0.3.2] - 2016-11-05
|
## [0.3.2] - 2016-11-05
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
|
@ -279,7 +279,7 @@ module Radix
|
||||||
result.found?.should be_false
|
result.found?.should be_false
|
||||||
end
|
end
|
||||||
|
|
||||||
it "finds when using matching path" do
|
it "finds when key and path matches" do
|
||||||
tree = Tree(Symbol).new
|
tree = Tree(Symbol).new
|
||||||
tree.add "/about", :about
|
tree.add "/about", :about
|
||||||
|
|
||||||
|
@ -290,7 +290,7 @@ module Radix
|
||||||
result.payload.should eq(:about)
|
result.payload.should eq(:about)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "finds when using path with trailing slash" do
|
it "finds when path contains trailing slash" do
|
||||||
tree = Tree(Symbol).new
|
tree = Tree(Symbol).new
|
||||||
tree.add "/about", :about
|
tree.add "/about", :about
|
||||||
|
|
||||||
|
@ -299,7 +299,7 @@ module Radix
|
||||||
result.key.should eq("/about")
|
result.key.should eq("/about")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "finds when key has trailing slash" do
|
it "finds when key contains trailing slash" do
|
||||||
tree = Tree(Symbol).new
|
tree = Tree(Symbol).new
|
||||||
tree.add "/about/", :about
|
tree.add "/about/", :about
|
||||||
|
|
||||||
|
@ -363,7 +363,7 @@ module Radix
|
||||||
result.params["filepath"].should eq("src/file.png")
|
result.params["filepath"].should eq("src/file.png")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns optional catch all" do
|
it "returns optional catch all after slash" do
|
||||||
tree = Tree(Symbol).new
|
tree = Tree(Symbol).new
|
||||||
tree.add "/", :root
|
tree.add "/", :root
|
||||||
tree.add "/search/*extra", :extra
|
tree.add "/search/*extra", :extra
|
||||||
|
@ -375,6 +375,17 @@ module Radix
|
||||||
result.params["extra"].empty?.should be_true
|
result.params["extra"].empty?.should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns optional catch all by globbing" do
|
||||||
|
tree = Tree(Symbol).new
|
||||||
|
tree.add "/members*trailing", :members_catch_all
|
||||||
|
|
||||||
|
result = tree.find("/members")
|
||||||
|
result.found?.should be_true
|
||||||
|
result.key.should eq("/members*trailing")
|
||||||
|
result.params.has_key?("trailing").should be_true
|
||||||
|
result.params["trailing"].empty?.should be_true
|
||||||
|
end
|
||||||
|
|
||||||
it "does not find when catch all is not full match" do
|
it "does not find when catch all is not full match" do
|
||||||
tree = Tree(Symbol).new
|
tree = Tree(Symbol).new
|
||||||
tree.add "/", :root
|
tree.add "/", :root
|
||||||
|
@ -383,6 +394,16 @@ module Radix
|
||||||
result = tree.find("/search")
|
result = tree.find("/search")
|
||||||
result.found?.should be_false
|
result.found?.should be_false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does prefer specific path over catch all if both are present" do
|
||||||
|
tree = Tree(Symbol).new
|
||||||
|
tree.add "/members", :members
|
||||||
|
tree.add "/members*trailing", :members_catch_all
|
||||||
|
|
||||||
|
result = tree.find("/members")
|
||||||
|
result.found?.should be_true
|
||||||
|
result.key.should eq("/members")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "dealing with named parameters" do
|
context "dealing with named parameters" do
|
||||||
|
|
|
@ -336,10 +336,12 @@ module Radix
|
||||||
|
|
||||||
# check if remaining part is catch all
|
# check if remaining part is catch all
|
||||||
if key_reader.pos < node.key.size &&
|
if key_reader.pos < node.key.size &&
|
||||||
key_reader.current_char == '/' &&
|
((key_reader.current_char == '/' && key_reader.peek_next_char == '*') ||
|
||||||
key_reader.peek_next_char == '*'
|
key_reader.current_char == '*')
|
||||||
# skip '*'
|
# skip to '*' only if necessary
|
||||||
|
unless key_reader.current_char == '*'
|
||||||
key_reader.next_char
|
key_reader.next_char
|
||||||
|
end
|
||||||
|
|
||||||
# deal with catch all, but since there is nothing in the path
|
# deal with catch all, but since there is nothing in the path
|
||||||
# return parameter as empty
|
# return parameter as empty
|
||||||
|
|
Loading…
Reference in a new issue