Permalink
Browse files

Raise ArgumentError if route name is invalid [#6517 state:resolved]

  • Loading branch information...
pixeltrix committed Mar 6, 2011
1 parent eff41a2 commit 2437c78f40d5e89aa3dc1c960bbe12fe0427b627
Showing with 33 additions and 0 deletions.
  1. +1 −0 actionpack/lib/action_dispatch/routing/route_set.rb
  2. +32 −0 actionpack/test/dispatch/routing_test.rb
@@ -295,6 +295,7 @@ def empty?
end
def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
+ raise ArgumentError, "Invalid route name: '#{name}'" unless name.blank? || name.to_s.match(/^[_a-z]\w*$/i)
route = Route.new(self, app, conditions, requirements, defaults, name, anchor)
@set.add_route(*route)
named_routes[name] = route if name
@@ -2194,6 +2194,38 @@ def test_controller_name_with_leading_slash_raise_error
end
end
+ def test_invalid_route_name_raises_error
+ assert_raise(ArgumentError) do
+ self.class.stub_controllers do |routes|
+ routes.draw { get '/products', :to => 'products#index', :as => 'products ' }
+ end
+ end
+
+ assert_raise(ArgumentError) do
+ self.class.stub_controllers do |routes|
+ routes.draw { get '/products', :to => 'products#index', :as => ' products' }
+ end
+ end
+
+ assert_raise(ArgumentError) do
+ self.class.stub_controllers do |routes|
+ routes.draw { get '/products', :to => 'products#index', :as => 'products!' }
+ end
+ end
+
+ assert_raise(ArgumentError) do
+ self.class.stub_controllers do |routes|
+ routes.draw { get '/products', :to => 'products#index', :as => 'products index' }
+ end
+ end
+
+ assert_raise(ArgumentError) do
+ self.class.stub_controllers do |routes|
+ routes.draw { get '/products', :to => 'products#index', :as => '1products' }
+ end
+ end
+ end
+
def test_routing_constraints_with_anchors_raises_error
assert_raise(ArgumentError) do
self.class.stub_controllers do |routes|

0 comments on commit 2437c78

Please sign in to comment.