Namespace
Methods
R
S
T
Instance Public methods
request()
# File rails/actionpack/test/controller/routing_test.rb, line 1278
def request
  @request ||= MockRequest.new(:host => "named.routes.test", :method => :get)
end
set()
# File rails/actionpack/test/controller/routing_test.rb, line 1274
def set
  @set ||= ROUTING::RouteSet.new
end
setup_named_route_test()
# File rails/actionpack/test/controller/routing_test.rb, line 1347
def setup_named_route_test
  set.draw do |map|
    map.show '/people/:id', :controller => 'people', :action => 'show'
    map.index '/people', :controller => 'people', :action => 'index'
    map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi'
    map.users '/admin/users', :controller => 'admin/users', :action => 'index'
  end

  klass = Class.new(MockController)
  set.named_routes.install(klass)
  klass.new(set)
end
test_action_left_off_when_id_is_recalled()
# File rails/actionpack/test/controller/routing_test.rb, line 1710
def test_action_left_off_when_id_is_recalled
  set.draw do |map|
    map.connect ':controller/:action/:id'
  end
  assert_equal '/post', set.generate(
    {:controller => 'post', :action => 'index'},
    {:controller => 'post', :action => 'show', :id => '10'}
  )
end
test_deprecation_warning_for_root_route()
# File rails/actionpack/test/controller/routing_test.rb, line 1617
def test_deprecation_warning_for_root_route
  Object.const_set(:PeopleController, Class.new)

  set.draw do |map|
    assert_deprecated do
      map.root('', :controller => "people")
    end    
  end
ensure
  Object.send(:remove_const, :PeopleController)
end
test_draw()
# File rails/actionpack/test/controller/routing_test.rb, line 1322
def test_draw
  assert_equal 0, set.routes.size
  set.draw do |map|
    map.connect '/hello/world', :controller => 'a', :action => 'b'
  end
  assert_equal 1, set.routes.size
end
test_draw_default_route()
# File rails/actionpack/test/controller/routing_test.rb, line 1397
def test_draw_default_route
  ActionController::Routing.with_controllers(['users']) do
    set.draw do |map|
      map.connect '/:controller/:action/:id'
    end

    assert_equal 1, set.routes.size
    route = set.routes.first

    assert route.segments.last.optional?

    assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10)
    assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10)

    assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
    assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
  end
end
test_draw_default_route_with_default_controller()
# File rails/actionpack/test/controller/routing_test.rb, line 1416
def test_draw_default_route_with_default_controller
  ActionController::Routing.with_controllers(['users']) do
    set.draw do |map|
      map.connect '/:controller/:action/:id', :controller => 'users'
    end      
    assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/'))
  end
end
test_extra_keys()
# File rails/actionpack/test/controller/routing_test.rb, line 1289
def test_extra_keys
  set.draw { |m| m.connect ':controller/:action/:id' }
  extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
  assert_equal %w(that this), extras.map(&:to_s).sort
end
test_extra_keys_not_first()
# File rails/actionpack/test/controller/routing_test.rb, line 1313
def test_extra_keys_not_first
  set.draw do |map| 
    map.connect ':controller/:action/:id.:format'
    map.connect ':controller/:action/:id'
  end
  extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
  assert_equal %w(that this), extras.map(&:to_s).sort
end
test_generate()
# File rails/actionpack/test/controller/routing_test.rb, line 1687
def test_generate
  set.draw { |map| map.connect ':controller/:action/:id' }

  args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
  assert_equal "/foo/bar/7?x=y", set.generate(args)
  assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
  assert_equal [:x], set.extra_keys(args)
end
test_generate_changes_controller_module()
# File rails/actionpack/test/controller/routing_test.rb, line 1649
def test_generate_changes_controller_module
  set.draw { |map| map.connect ':controller/:action/:id' }
  current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
  url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current)
  assert_equal "/foo/bar/baz/7", url
end
test_generate_extras()
# File rails/actionpack/test/controller/routing_test.rb, line 1282
def test_generate_extras
  set.draw { |m| m.connect ':controller/:action/:id' }
  path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
  assert_equal "/foo/bar/15", path
  assert_equal %w(that this), extras.map(&:to_s).sort
end
test_generate_extras_not_first()
# File rails/actionpack/test/controller/routing_test.rb, line 1295
def test_generate_extras_not_first
  set.draw do |map| 
    map.connect ':controller/:action/:id.:format'
    map.connect ':controller/:action/:id'
  end    
  path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
  assert_equal "/foo/bar/15", path
  assert_equal %w(that this), extras.map(&:to_s).sort
end
test_generate_finds_best_fit()
# File rails/actionpack/test/controller/routing_test.rb, line 1639
def test_generate_finds_best_fit
  set.draw do |map|
    map.connect "/people", :controller => "people", :action => "index"
    map.connect "/ws/people", :controller => "people", :action => "index", :ws => true
  end

  url = set.generate(:controller => "people", :action => "index", :ws => true)
  assert_equal "/ws/people", url
end
test_generate_not_first()
# File rails/actionpack/test/controller/routing_test.rb, line 1305
def test_generate_not_first
  set.draw do |map| 
    map.connect ':controller/:action/:id.:format'
    map.connect ':controller/:action/:id'
  end    
  assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
end
test_generate_with_default_action()
# File rails/actionpack/test/controller/routing_test.rb, line 1629
def test_generate_with_default_action
  set.draw do |map|
    map.connect "/people", :controller => "people"
    map.connect "/people/list", :controller => "people", :action => "list"
  end

  url = set.generate(:controller => "people", :action => "list")
  assert_equal "/people/list", url
end
test_id_is_not_impossibly_sticky()
# File rails/actionpack/test/controller/routing_test.rb, line 1656
def test_id_is_not_impossibly_sticky
  set.draw do |map|
    map.connect 'foo/:number', :controller => "people", :action => "index"
    map.connect ':controller/:action/:id'
  end

  url = set.generate({:controller => "people", :action => "index", :number => 3},
    {:controller => "people", :action => "index", :id => "21"})
  assert_equal "/foo/3", url
end
test_id_is_sticky_when_it_ought_to_be()
# File rails/actionpack/test/controller/routing_test.rb, line 1667
def test_id_is_sticky_when_it_ought_to_be
  set.draw do |map|
    map.connect ':controller/:id/:action'
  end

  url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
  assert_equal "/people/7/destroy", url
end
test_later_named_routes_take_precedence()
# File rails/actionpack/test/controller/routing_test.rb, line 1339
def test_later_named_routes_take_precedence
  set.draw do |map|
    map.hello '/hello/world', :controller => 'a', :action => 'b'
    map.hello '/hello', :controller => 'a', :action => 'b'
  end
  assert_equal set.routes.last, set.named_routes[:hello]
end
test_namd_route_url_method_with_ordered_parameters()
# File rails/actionpack/test/controller/routing_test.rb, line 1391
def test_namd_route_url_method_with_ordered_parameters
  controller = setup_named_route_test
  assert_equal "http://named.route.test/people/go/7/hello/joe/5",
    controller.send(:multi_url, 7, "hello", 5)
end
test_named_draw()
# File rails/actionpack/test/controller/routing_test.rb, line 1330
def test_named_draw
  assert_equal 0, set.routes.size
  set.draw do |map|
    map.hello '/hello/world', :controller => 'a', :action => 'b'
  end
  assert_equal 1, set.routes.size
  assert_equal set.routes.first, set.named_routes[:hello]
end
test_named_route_hash_access_method()
# File rails/actionpack/test/controller/routing_test.rb, line 1360
def test_named_route_hash_access_method
  controller = setup_named_route_test

  assert_equal(
    { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false },
    controller.send(:hash_for_show_url, :id => 5))

  assert_equal(
    { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false },
    controller.send(:hash_for_index_url))
  
  assert_equal(
    { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true },
    controller.send(:hash_for_show_path, :id => 5)
  )
end
test_named_route_url_method()
# File rails/actionpack/test/controller/routing_test.rb, line 1377
def test_named_route_url_method
  controller = setup_named_route_test
  
  assert_equal "http://named.route.test/people/5", controller.send(:show_url, :id => 5)
  assert_equal "/people/5", controller.send(:show_path, :id => 5)
  
  assert_equal "http://named.route.test/people", controller.send(:index_url)
  assert_equal "/people", controller.send(:index_path)

  assert_equal "http://named.route.test/admin/users", controller.send(:users_url)
  assert_equal '/admin/users', controller.send(:users_path)
  assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
end
test_named_routes_are_never_relative_to_modules()
# File rails/actionpack/test/controller/routing_test.rb, line 1696
def test_named_routes_are_never_relative_to_modules
  set.draw do |map|
    map.connect "/connection/manage/:action", :controller => 'connection/manage'
    map.connect "/connection/connection", :controller => "connection/connection"
    map.family_connection "/connection", :controller => "connection"
  end

  url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'})
  assert_equal "/connection/connection", url

  url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
  assert_equal "/connection", url
end
test_non_path_route_requirements_match_all()
# File rails/actionpack/test/controller/routing_test.rb, line 1477
def test_non_path_route_requirements_match_all
  set.draw do |map|
    map.connect 'page/37s', :controller => 'pages', :action => 'show', :name => %r(jamis|david)/
  end
  assert_equal '/page/37s', set.generate(:controller => 'pages', :action => 'show', :name => 'jamis')
  assert_raises ActionController::RoutingError do
    set.generate(:controller => 'pages', :action => 'show', :name => 'not_jamis')
  end
  assert_raises ActionController::RoutingError do
    set.generate(:controller => 'pages', :action => 'show', :name => 'nor_jamis_and_david')
  end
end
test_query_params_will_be_shown_when_recalled()
# File rails/actionpack/test/controller/routing_test.rb, line 1720
def test_query_params_will_be_shown_when_recalled
  set.draw do |map|
    map.connect 'show_post/:parameter', :controller => 'post', :action => 'show'
    map.connect ':controller/:action/:id'
  end
  assert_equal '/post/edit?parameter=1', set.generate(
    {:action => 'edit', :parameter => 1},
    {:controller => 'post', :action => 'show', :parameter => 1}
  )
end
test_recognize_with_conditions()
# File rails/actionpack/test/controller/routing_test.rb, line 1499
def test_recognize_with_conditions
  Object.const_set(:PeopleController, Class.new)

  set.draw do |map|
    map.with_options(:controller => "people") do |people|
      people.people  "/people",     :action => "index",   :conditions => { :method => :get }
      people.connect "/people",     :action => "create",  :conditions => { :method => :post }
      people.person  "/people/:id", :action => "show",    :conditions => { :method => :get }
      people.connect "/people/:id", :action => "update",  :conditions => { :method => :put }
      people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete }
    end
  end

  request.path = "/people"
  request.method = :get
  assert_nothing_raised { set.recognize(request) }
  assert_equal("index", request.path_parameters[:action])
  
  request.method = :post
  assert_nothing_raised { set.recognize(request) }
  assert_equal("create", request.path_parameters[:action])
  
  request.method = :put
  assert_nothing_raised { set.recognize(request) }
  assert_equal("update", request.path_parameters[:action])

  request.method = :update
  assert_raises(ActionController::RoutingError) { set.recognize(request) }

  request.path = "/people/5"
  request.method = :get
  assert_nothing_raised { set.recognize(request) }
  assert_equal("show", request.path_parameters[:action])
  assert_equal("5", request.path_parameters[:id])

  request.method = :put
  assert_nothing_raised { set.recognize(request) }
  assert_equal("update", request.path_parameters[:action])
  assert_equal("5", request.path_parameters[:id])

  request.method = :delete
  assert_nothing_raised { set.recognize(request) }
  assert_equal("destroy", request.path_parameters[:action])
  assert_equal("5", request.path_parameters[:id])
  
  request.method = :post
  assert_raises(ActionController::RoutingError) { set.recognize(request) }
  
ensure
  Object.send(:remove_const, :PeopleController)
end
test_recognize_with_conditions_and_format()
# File rails/actionpack/test/controller/routing_test.rb, line 1586
def test_recognize_with_conditions_and_format
  Object.const_set(:PeopleController, Class.new)

  set.draw do |map|
    map.with_options(:controller => "people") do |people|
      people.person  "/people/:id", :action => "show",    :conditions => { :method => :get }
      people.connect "/people/:id", :action => "update",  :conditions => { :method => :put }
      people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get }
    end
  end

  request.path = "/people/5"
  request.method = :get
  assert_nothing_raised { set.recognize(request) }
  assert_equal("show", request.path_parameters[:action])
  assert_equal("5", request.path_parameters[:id])

  request.method = :put
  assert_nothing_raised { set.recognize(request) }
  assert_equal("update", request.path_parameters[:action])

  request.path = "/people/5.png"
  request.method = :get
  assert_nothing_raised { set.recognize(request) }
  assert_equal("show", request.path_parameters[:action])
  assert_equal("5", request.path_parameters[:id])
  assert_equal("png", request.path_parameters[:_format])
ensure
  Object.send(:remove_const, :PeopleController)
end
test_recognize_with_encoded_id_and_regex()
# File rails/actionpack/test/controller/routing_test.rb, line 1490
def test_recognize_with_encoded_id_and_regex
  set.draw do |map|
    map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => %r[a-zA-Z0-9 ]+/
  end

  assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
  assert_equal({:controller => 'pages', :action => 'show', :id => 'hello world'}, set.recognize_path('/page/hello+world'))
end
test_route_requirements_with_anchor_chars_are_invalid()
# File rails/actionpack/test/controller/routing_test.rb, line 1441
def test_route_requirements_with_anchor_chars_are_invalid
  assert_raises ArgumentError do
    set.draw do |map|
      map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => %r^\d+/
    end
  end
  assert_raises ArgumentError do
    set.draw do |map|
      map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => %r\A\d+/
    end
  end
  assert_raises ArgumentError do
    set.draw do |map|
      map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => %r\d+$/
    end
  end
  assert_raises ArgumentError do
    set.draw do |map|
      map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => %r\d+\Z/
    end
  end
  assert_raises ArgumentError do
    set.draw do |map|
      map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => %r\d+\z/
    end
  end
  assert_nothing_raised do
    set.draw do |map|
      map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => %r\d+/, :name => %r^(david|jamis)/
    end
    assert_raises ActionController::RoutingError do
      set.generate :controller => 'pages', :action => 'show', :id => 10
    end
  end
end
test_route_with_parameter_shell()
# File rails/actionpack/test/controller/routing_test.rb, line 1425
def test_route_with_parameter_shell
  ActionController::Routing.with_controllers(['users', 'pages']) do
    set.draw do |map|
      map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => %r\d+/
      map.connect '/:controller/:action/:id'
    end

    assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages'))
    assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index'))
    assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list'))

    assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10'))
    assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
  end
end
test_routing_traversal_does_not_load_extra_classes()
# File rails/actionpack/test/controller/routing_test.rb, line 1573
def test_routing_traversal_does_not_load_extra_classes
  assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
  set.draw do |map|
    map.connect '/profile', :controller => 'profile'
  end

  request.path = '/profile'

  set.recognize(request) rescue nil
  
  assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
end
test_typo_recognition()
# File rails/actionpack/test/controller/routing_test.rb, line 1551
def test_typo_recognition
  Object.const_set(:ArticlesController, Class.new)

  set.draw do |map|
    map.connect 'articles/:year/:month/:day/:title',
           :controller => 'articles', :action => 'permalink',
           :year => %r\d{4}/, :day => %r\d{1,2}/, :month => %r\d{1,2}/
  end

  request.path = "/articles/2005/11/05/a-very-interesting-article"
  request.method = :get
  assert_nothing_raised { set.recognize(request) }
  assert_equal("permalink", request.path_parameters[:action])
  assert_equal("2005", request.path_parameters[:year])
  assert_equal("11", request.path_parameters[:month])
  assert_equal("05", request.path_parameters[:day])
  assert_equal("a-very-interesting-article", request.path_parameters[:title])
  
ensure
  Object.send(:remove_const, :ArticlesController)
end
test_use_static_path_when_possible()
# File rails/actionpack/test/controller/routing_test.rb, line 1676
def test_use_static_path_when_possible
  set.draw do |map|
    map.connect 'about', :controller => "welcome", :action => "about"
    map.connect ':controller/:action/:id'
  end

  url = set.generate({:controller => "welcome", :action => "about"},
    {:controller => "welcome", :action => "get", :id => "7"})
  assert_equal "/about", url
end