From e00867bc437b6a681491ef59e13423051e6d98f0 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Sun, 6 Mar 2011 07:08:50 +0000 Subject: [PATCH] Raise ArgumentError if route name is invalid [#6517 state:resolved] --- .../lib/action_dispatch/routing/route_set.rb | 2 ++ actionpack/test/dispatch/routing_test.rb | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index fc86d52a3a..61053d4464 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -1,5 +1,6 @@ require 'rack/mount' require 'forwardable' +require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/to_query' require 'active_support/core_ext/hash/slice' @@ -330,6 +331,7 @@ module ActionDispatch 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 diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 1a96587836..5e5758a60e 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -2313,6 +2313,38 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest 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_nested_route_in_nested_resource get "/posts/1/comments/2/views" assert_equal "comments#views", @response.body