Correctly build key from detected parameters

Given a key `/:foo/:bar`, the find mechanism was incorrectly picking
the separator character as part of the key name (`foo/`).

This caused incorrect match between expected name (`foo`) and the
one obtained.

When the key name was extracted from the named parameter, the size
of the returned key was not compensated, given that we move +1 positions
to avoid having ':' as part of the key.

This is now corrected by reducing the key size one shorter to
compensate.

Fixes Issue #2
This commit is contained in:
Luis Lavena 2016-02-29 11:35:57 -03:00
parent 2a7a3b77bc
commit fe1ebb7d76
2 changed files with 31 additions and 1 deletions

View file

@ -340,6 +340,34 @@ module Radix
end
end
context "dealing with multiple named parameters" do
it "finds matching path" do
tree = Tree.new
tree.add "/", :root
tree.add "/:section/:page", :static_page
result = tree.find("/about/shipping")
result.found?.should be_true
result.key.should eq("/:section/:page")
result.payload.should eq(:static_page)
end
it "returns named parameters in result" do
tree = Tree.new
tree.add "/", :root
tree.add "/:section/:page", :static_page
result = tree.find("/about/shipping")
result.found?.should be_true
result.params.has_key?("section").should be_true
result.params["section"].should eq("about")
result.params.has_key?("page").should be_true
result.params["page"].should eq("shipping")
end
end
context "dealing with both catch all and named parameters" do
it "finds matching path" do
tree = Tree.new