Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes exception when duplicate parameter names are given in path #36

Merged
merged 2 commits into from
Jan 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/http_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class HttpRouter
TooManyParametersException = Class.new(RuntimeError)
# Raised when there are left over options
LeftOverOptions = Class.new(RuntimeError)
# Raised when there are duplicate param names specified in a Path
AmbiguousVariableException = Class.new(RuntimeError)

RecognizeResponse = Struct.new(:matches, :acceptable_methods)

Expand Down
7 changes: 6 additions & 1 deletion lib/http_router/node/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Path < Node
def initialize(router, parent, route, path, param_names = [])
@route, @path, @param_names, @dynamic = route, path, param_names, !param_names.empty?
@route.add_path(self)
raise AmbiguousVariableException, "You have duplicate variable name present: #{param_names.join(', ')}" if param_names.uniq.size != param_names.size

raise AmbiguousVariableException, "You have duplicate variable names present: #{duplicates.join(', ')}" if param_names.uniq.size != param_names.size
super router, parent
router.uncompile
end
Expand Down Expand Up @@ -46,6 +47,10 @@ def usable?(other)
def inspect_label
"Path: #{path.inspect} for route #{route.name || 'unnamed route'} to #{route.dest.inspect}"
end

def duplicates
param_names.group_by { |e| e }.select { |k, v| v.size > 1 }.map(&:first)
end
end
end
end
6 changes: 6 additions & 0 deletions test/test_misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def test_too_many_params
assert_raises(HttpRouter::InvalidRouteException) { r.path(:route) }
end

def test_ambigiuous_parameters_in_route
r = router
r.add("/abc/:id/test/:id", :name => :route).default_destination
assert_raises(HttpRouter::AmbiguousVariableException) { r.path(:route, :id => 'fail') }
end

def test_public_interface
methods = HttpRouter.public_instance_methods.map(&:to_sym)
assert methods.include?(:url_mount)
Expand Down