class RouteSetTest

Attributes

controller[RW]
routes[R]
set[R]

Public Instance Methods

default_route_set() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 936
def default_route_set
  @default_route_set ||= begin
    set = ActionDispatch::Routing::RouteSet.new
    set.draw do

      ActiveSupport::Deprecation.silence do
        get "/:controller(/:action(/:id))"
      end
    end
    set
  end
end
request() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 932
def request
  @request ||= ActionController::TestRequest.new
end
setup() click to toggle source
Calls superclass method
# File actionpack/test/controller/routing_test.rb, line 927
def setup
  super
  @set = make_set
end
setup_named_route_test() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1031
def setup_named_route_test
  set.draw do
    get "/people(/:id)" => "people#show", :as => "show"
    get "/people" => "people#index", :as => "index"
    get "/people/go/:foo/:bar/joe(/:id)" => "people#multi", :as => "multi"
    get "/admin/users" => "admin/users#index", :as => "users"
  end

  get URI("http://test.host/people")
  controller
end
test_action_left_off_when_id_is_recalled() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1482
def test_action_left_off_when_id_is_recalled
  @set = make_set false

  set.draw do
    ActiveSupport::Deprecation.silence do
      get ":controller(/:action(/:id))"
    end
  end

  get URI("http://test.host/books/show/10")

  assert_equal "/books", controller.url_for(controller: "books",
                                            only_path: true,
                                            action: "index")
end
test_assign_route_options_with_anchor_chars() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1709
def test_assign_route_options_with_anchor_chars
  set.draw do
    ActiveSupport::Deprecation.silence do
      get "/cars/:action/:person/:car/", controller: "cars"
    end
  end

  assert_equal "/cars/buy/1/2", url_for(set, controller: "cars", action: "buy", person: "1", car: "2")

  assert_equal({ controller: "cars", action: "buy", person: "1", car: "2" }, set.recognize_path("/cars/buy/1/2"))
end
test_build_empty_query_string() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1807
def test_build_empty_query_string
  assert_uri_equal "/foo", url_for(default_route_set, controller: "foo")
end
test_build_query_string_with_nil_value() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1811
def test_build_query_string_with_nil_value
  assert_uri_equal "/foo", url_for(default_route_set, controller: "foo", x: nil)
end
test_convert_ints_build_query_string() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1819
def test_convert_ints_build_query_string
  assert_uri_equal "/foo?x=1&y=2", url_for(default_route_set, controller: "foo", x: 1, y: 2)
end
test_default_route_recognition() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1764
def test_default_route_recognition
  expected = { controller: "pages", action: "show", id: "10" }
  assert_equal expected, default_route_set.recognize_path("/pages/show/10")
  assert_equal expected, default_route_set.recognize_path("/pages/show/10/")

  expected[:id] = "jamis"
  assert_equal expected, default_route_set.recognize_path("/pages/show/jamis/")

  expected.delete :id
  assert_equal expected, default_route_set.recognize_path("/pages/show")
  assert_equal expected, default_route_set.recognize_path("/pages/show/")

  expected[:action] = "index"
  assert_equal expected, default_route_set.recognize_path("/pages/")
  assert_equal expected, default_route_set.recognize_path("/pages")

  assert_raise(ActionController::RoutingError) { default_route_set.recognize_path("/") }
  assert_raise(ActionController::RoutingError) { default_route_set.recognize_path("/pages/how/goood/it/is/to/be/free") }
end
test_default_route_should_include_default_action_when_id_present() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1788
def test_default_route_should_include_default_action_when_id_present
  assert_equal "/accounts/index/20", url_for(default_route_set, controller: "accounts", action: "index", id: "20")
end
test_default_route_should_omit_default_action() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1784
def test_default_route_should_omit_default_action
  assert_equal "/accounts", url_for(default_route_set, controller: "accounts", action: "index")
end
test_default_route_should_uri_escape_pluses() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1796
def test_default_route_should_uri_escape_pluses
  expected = { controller: "pages", action: "show", id: "hello world" }
  assert_equal expected, default_route_set.recognize_path("/pages/show/hello%20world")
  assert_equal "/pages/show/hello%20world", url_for(default_route_set, expected)

  expected[:id] = "hello+world"
  assert_equal expected, default_route_set.recognize_path("/pages/show/hello+world")
  assert_equal expected, default_route_set.recognize_path("/pages/show/hello%2Bworld")
  assert_equal "/pages/show/hello+world", url_for(default_route_set, expected)
end
test_default_route_should_work_with_action_but_no_id() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1792
def test_default_route_should_work_with_action_but_no_id
  assert_equal "/accounts/list_all", url_for(default_route_set, controller: "accounts", action: "list_all")
end
test_draw() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 996
def test_draw
  assert_equal 0, set.routes.size
  set.draw do
    get "/hello/world" => "a#b"
  end
  assert_equal 1, set.routes.size
end
test_draw_default_route() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1120
def test_draw_default_route
  set.draw do
    ActiveSupport::Deprecation.silence do
      get ":controller/:action/:id"
    end
  end

  assert_equal 1, set.routes.size

  assert_equal "/users/show/10",  url_for(set, controller: "users", action: "show", id: 10)
  assert_equal "/users/index/10", url_for(set, 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
test_draw_symbol_controller_name() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1004
def test_draw_symbol_controller_name
  assert_equal 0, set.routes.size
  set.draw do
    get "/users/index" => "users#index"
  end
  set.recognize_path("/users/index", method: :get)
  assert_equal 1, set.routes.size
end
test_duplicate_named_route_raises_rather_than_pick_precedence() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1022
def test_duplicate_named_route_raises_rather_than_pick_precedence
  assert_raise ArgumentError do
    set.draw do
      get "/hello/world" => "a#b", :as => "hello"
      get "/hello"       => "a#b", :as => "hello"
    end
  end
end
test_escape_spaces_build_query_string() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1823
def test_escape_spaces_build_query_string
  assert_uri_equal "/foo?x=hello+world&y=goodbye+world", url_for(default_route_set, controller: "foo", x: "hello world", y: "goodbye world")
end
test_escape_spaces_build_query_string_selected_keys() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1831
def test_escape_spaces_build_query_string_selected_keys
  assert_uri_equal "/foo?x=hello+world", url_for(default_route_set, controller: "foo", x: "hello world")
end
test_expand_array_build_query_string() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1827
def test_expand_array_build_query_string
  assert_uri_equal "/foo?x%5B%5D=1&x%5B%5D=2", url_for(default_route_set, controller: "foo", x: [1, 2])
end
test_expiry_determination_should_consider_values_with_to_param() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1531
def test_expiry_determination_should_consider_values_with_to_param
  @set = make_set false

  set.draw { ActiveSupport::Deprecation.silence { get "projects/:project_id/:controller/:action" } }

  get URI("http://test.host/projects/1/weblog/show")

  assert_equal(
    { controller: "weblog", action: "show", project_id: "1" },
    controller.request.path_parameters)

  assert_equal "/projects/1/weblog/show",
    controller.url_for(action: "show", project_id: 1, only_path: true)
end
test_extra_keys() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 956
def test_extra_keys
  set.draw { ActiveSupport::Deprecation.silence { get ":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() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 985
def test_extra_keys_not_first
  set.draw do
    ActiveSupport::Deprecation.silence do
      get ":controller/:action/:id.:format"
      get ":controller/:action/:id"
    end
  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_format_is_not_inherit() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1515
def test_format_is_not_inherit
  set.draw do
    get "/posts(.:format)" => "posts#index"
  end

  get URI("http://test.host/posts.xml")
  assert_equal({ controller: "posts", action: "index", format: "xml" },
               controller.request.path_parameters)

  assert_equal "/posts", controller.url_for(
    controller: "posts", only_path: true)

  assert_equal "/posts.xml", controller.url_for(
    controller: "posts", format: "xml", only_path: true)
end
test_generate() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1425
def test_generate
  set.draw { ActiveSupport::Deprecation.silence { get ":controller/:action/:id" } }

  args = { controller: "foo", action: "bar", id: "7", x: "y" }
  assert_equal "/foo/bar/7?x=y",     url_for(set, args)
  assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
  assert_equal [:x], set.extra_keys(args)
end
test_generate_extras() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 949
def test_generate_extras
  set.draw { ActiveSupport::Deprecation.silence { get ":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() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 962
def test_generate_extras_not_first
  set.draw do
    ActiveSupport::Deprecation.silence do
      get ":controller/:action/:id.:format"
      get ":controller/:action/:id"
    end
  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_not_first() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 974
def test_generate_not_first
  set.draw do
    ActiveSupport::Deprecation.silence do
      get ":controller/:action/:id.:format"
      get ":controller/:action/:id"
    end
  end
  assert_equal "/foo/bar/15?this=hello",
      url_for(set, controller: "foo", action: "bar", id: 15, this: "hello")
end
test_generate_with_blank_path_prefix() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1447
def test_generate_with_blank_path_prefix
  set.draw do
    scope "" do
      ActiveSupport::Deprecation.silence do
        get ":controller(/:action(/:id))"
      end
    end
  end

  args = { controller: "foo", action: "bar", id: "7", x: "y" }
  assert_equal "/foo/bar/7?x=y", url_for(set, args)
end
test_generate_with_default_action() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1325
def test_generate_with_default_action
  set.draw do
    get "/people", controller: "people", action: "index"
    get "/people/list", controller: "people", action: "list"
  end

  url = url_for(set, controller: "people", action: "list")
  assert_equal "/people/list", url
end
test_generate_with_default_params() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1835
def test_generate_with_default_params
  set.draw do
    get "dummy/page/:page" => "dummy#show"
    get "dummy/dots/page.:page" => "dummy#dots"
    get "ibocorp(/:page)" => "ibocorp#show",
                           :constraints => { page: /\d+/ },
                           :defaults => { page: 1 }

    ActiveSupport::Deprecation.silence do
      get ":controller/:action/:id"
    end
  end

  assert_equal "/ibocorp", url_for(set, controller: "ibocorp", action: "show", page: 1)
end
test_generate_with_optional_params_recalls_last_request() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1855
def test_generate_with_optional_params_recalls_last_request
  @set = make_set false

  set.draw do
    get "blog/", controller: "blog", action: "index"

    get "blog(/:year(/:month(/:day)))",
          controller: "blog",
          action: "show_date",
          constraints: { year: /(19|20)\d\d/, month: /[01]?\d/, day: /[0-3]?\d/ },
          day: nil, month: nil

    get "blog/show/:id", controller: "blog", action: "show", id: /\d+/

    ActiveSupport::Deprecation.silence do
      get "blog/:controller/:action(/:id)"
    end

    get "*anything", controller: "blog", action: "unknown_request"
  end

  recognize_path = ->(path) {
    get(URI("http://example.org" + path))
    controller.request.path_parameters
  }

  assert_equal({ controller: "blog", action: "index" }, recognize_path.("/blog"))
  assert_equal({ controller: "blog", action: "show", id: "123" }, recognize_path.("/blog/show/123"))
  assert_equal({ controller: "blog", action: "show_date", year: "2004", day: nil, month: nil }, recognize_path.("/blog/2004"))
  assert_equal({ controller: "blog", action: "show_date", year: "2004", month: "12", day: nil }, recognize_path.("/blog/2004/12"))
  assert_equal({ controller: "blog", action: "show_date", year: "2004", month: "12", day: "25" }, recognize_path.("/blog/2004/12/25"))
  assert_equal({ controller: "articles", action: "edit", id: "123" }, recognize_path.("/blog/articles/edit/123"))
  assert_equal({ controller: "articles", action: "show_stats" }, recognize_path.("/blog/articles/show_stats"))
  assert_equal({ controller: "blog", action: "unknown_request", anything: "blog/wibble" }, recognize_path.("/blog/wibble"))
  assert_equal({ controller: "blog", action: "unknown_request", anything: "junk" }, recognize_path.("/junk"))

  get URI("http://example.org/blog/2006/07/28")

  assert_equal({ controller: "blog",  action: "show_date", year: "2006", month: "07", day: "28" }, controller.request.path_parameters)
  assert_equal("/blog/2006/07/25", controller.url_for(day: 25, only_path: true))
  assert_equal("/blog/2005",       controller.url_for(year: 2005, only_path: true))
  assert_equal("/blog/show/123",   controller.url_for(action: "show" , id: 123, only_path: true))
  assert_equal("/blog/2006",       controller.url_for(year: 2006, only_path: true))
  assert_equal("/blog/2006",       controller.url_for(year: 2006, month: nil, only_path: true))
end
test_generate_with_path_prefix() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1434
def test_generate_with_path_prefix
  set.draw do
    scope "my" do
      ActiveSupport::Deprecation.silence do
        get ":controller(/:action(/:id))"
      end
    end
  end

  args = { controller: "foo", action: "bar", id: "7", x: "y" }
  assert_equal "/my/foo/bar/7?x=y", url_for(set, args)
end
test_id_is_sticky_when_it_ought_to_be() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1393
def test_id_is_sticky_when_it_ought_to_be
  @set = make_set false

  set.draw do
    ActiveSupport::Deprecation.silence do
      get ":controller/:id/:action"
    end
  end

  get URI("http://test.host/people/7/show")

  assert_equal "/people/7/destroy", controller.url_for(action: "destroy", only_path: true)
end
test_named_draw() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1013
def test_named_draw
  assert_equal 0, set.routes.size
  set.draw do
    get "/hello/world" => "a#b", :as => "hello"
  end
  assert_equal 1, set.routes.size
  assert_equal set.routes.first, set.named_routes[:hello]
end
test_named_route_in_nested_resource() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1546
def test_named_route_in_nested_resource
  set.draw do
    resources :projects do
      member do
        get "milestones" => "milestones#index", :as => "milestones"
      end
    end
  end

  params = set.recognize_path("/projects/1/milestones", method: :get)
  assert_equal("milestones", params[:controller])
  assert_equal("index", params[:action])
end
test_named_route_url_method() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1043
def test_named_route_url_method
  controller = setup_named_route_test

  assert_equal "http://test.host/people/5", controller.send(:show_url, id: 5)
  assert_equal "/people/5", controller.send(:show_path, id: 5)

  assert_equal "http://test.host/people", controller.send(:index_url)
  assert_equal "/people", controller.send(:index_path)

  assert_equal "http://test.host/admin/users", controller.send(:users_url)
  assert_equal "/admin/users", controller.send(:users_path)
end
test_named_route_url_method_with_anchor() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1056
def test_named_route_url_method_with_anchor
  controller = setup_named_route_test

  assert_equal "http://test.host/people/5#location", controller.send(:show_url, id: 5, anchor: "location")
  assert_equal "/people/5#location", controller.send(:show_path, id: 5, anchor: "location")

  assert_equal "http://test.host/people#location", controller.send(:index_url, anchor: "location")
  assert_equal "/people#location", controller.send(:index_path, anchor: "location")

  assert_equal "http://test.host/admin/users#location", controller.send(:users_url, anchor: "location")
  assert_equal "/admin/users#location", controller.send(:users_path, anchor: "location")

  assert_equal "http://test.host/people/go/7/hello/joe/5#location",
    controller.send(:multi_url, 7, "hello", 5, anchor: "location")

  assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar#location",
    controller.send(:multi_url, 7, "hello", 5, baz: "bar", anchor: "location")

  assert_equal "http://test.host/people?baz=bar#location",
    controller.send(:index_url, baz: "bar", anchor: "location")

  assert_equal "http://test.host/people", controller.send(:index_url, anchor: nil)
  assert_equal "http://test.host/people", controller.send(:index_url, anchor: false)
end
test_named_route_url_method_with_host() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1086
def test_named_route_url_method_with_host
  controller = setup_named_route_test
  assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, host: "some.example.com")
end
test_named_route_url_method_with_no_positional_arguments() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1114
def test_named_route_url_method_with_no_positional_arguments
  controller = setup_named_route_test
  assert_equal "http://test.host/people?baz=bar",
    controller.send(:index_url, baz: "bar")
end
test_named_route_url_method_with_ordered_parameters() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1096
def test_named_route_url_method_with_ordered_parameters
  controller = setup_named_route_test
  assert_equal "http://test.host/people/go/7/hello/joe/5",
    controller.send(:multi_url, 7, "hello", 5)
end
test_named_route_url_method_with_ordered_parameters_and_empty_hash() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1108
def test_named_route_url_method_with_ordered_parameters_and_empty_hash
  controller = setup_named_route_test
  assert_equal "http://test.host/people/go/7/hello/joe/5",
    controller.send(:multi_url, 7, "hello", 5, {})
end
test_named_route_url_method_with_ordered_parameters_and_hash() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1102
def test_named_route_url_method_with_ordered_parameters_and_hash
  controller = setup_named_route_test
  assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar",
    controller.send(:multi_url, 7, "hello", 5, baz: "bar")
end
test_named_route_url_method_with_port() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1081
def test_named_route_url_method_with_port
  controller = setup_named_route_test
  assert_equal "http://test.host:8080/people/5", controller.send(:show_url, 5, port: 8080)
end
test_named_route_url_method_with_protocol() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1091
def test_named_route_url_method_with_protocol
  controller = setup_named_route_test
  assert_equal "https://test.host/people/5", controller.send(:show_url, 5, protocol: "https")
end
test_named_routes_are_never_relative_to_modules() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1460
def test_named_routes_are_never_relative_to_modules
  @set = make_set false

  set.draw do
    ActiveSupport::Deprecation.silence do
      get "/connection/manage(/:action)" => "connection/manage#index"
      get "/connection/connection" => "connection/connection#index"
      get "/connection" => "connection#index", :as => "family_connection"
    end
  end

  assert_equal({ controller: "connection/manage",
                 action: "index", }, request_path_params("/connection/manage"))

  url = controller.url_for(controller: "connection", only_path: true)
  assert_equal "/connection/connection", url

  url = controller.url_for(use_route: "family_connection",
                             controller: "connection", only_path: true)
  assert_equal "/connection", url
end
test_namespace() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1343
def test_namespace
  set.draw do

    namespace "api" do
      get "inventory" => "products#inventory"
    end

  end

  params = request_path_params("/api/inventory", method: :get)
  assert_equal("api/products", params[:controller])
  assert_equal("inventory", params[:action])
end
test_namespace_with_blank_path_prefix() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1381
def test_namespace_with_blank_path_prefix
  set.draw do
    scope module: "api", path: "" do
      get "inventory" => "products#inventory"
    end
  end

  params = request_path_params("/inventory", method: :get)
  assert_equal("api/products", params[:controller])
  assert_equal("inventory", params[:action])
end
test_namespace_with_path_prefix() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1369
def test_namespace_with_path_prefix
  set.draw do
    scope module: "api", path: "prefix" do
      get "inventory" => "products#inventory"
    end
  end

  params = request_path_params("/prefix/inventory", method: :get)
  assert_equal("api/products", params[:controller])
  assert_equal("inventory", params[:action])
end
test_namespaced_root_map() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1357
def test_namespaced_root_map
  set.draw do
    namespace "api" do
      root to: "products#index"
    end
  end

  params = request_path_params("/api", method: :get)
  assert_equal("api/products", params[:controller])
  assert_equal("index", params[:action])
end
test_query_params_will_be_shown_when_recalled() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1498
def test_query_params_will_be_shown_when_recalled
  @set = make_set false

  set.draw do
    get "show_weblog/:parameter" => "weblog#show"

    ActiveSupport::Deprecation.silence do
      get ":controller(/:action(/:id))"
    end
  end

  get URI("http://test.host/weblog/show/1")

  assert_equal "/weblog/edit?parameter=1", controller.url_for(
    action: "edit", parameter: 1, only_path: true)
end
test_recognize_with_alias_in_conditions() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1261
def test_recognize_with_alias_in_conditions
  set.draw do
    match "/people" => "people#index", :as => "people", :via => :get
    root to: "people#index"
  end

  params = request_path_params("/people", method: :get)
  assert_equal("people", params[:controller])
  assert_equal("index", params[:action])

  params = request_path_params("/", method: :get)
  assert_equal("people", params[:controller])
  assert_equal("index", params[:action])
end
test_recognize_with_conditions_and_format() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1301
def test_recognize_with_conditions_and_format
  set.draw do
    get "people/:id" => "people#show", :as => "person"
    put "people/:id" => "people#update"
    patch "people/:id" => "people#update"
    get "people/:id(.:format)" => "people#show"
  end

  params = request_path_params("/people/5", method: :get)
  assert_equal("show", params[:action])
  assert_equal("5", params[:id])

  params = request_path_params("/people/5", method: :put)
  assert_equal("update", params[:action])

  params = request_path_params("/people/5", method: :patch)
  assert_equal("update", params[:action])

  params = request_path_params("/people/5.png", method: :get)
  assert_equal("show", params[:action])
  assert_equal("5", params[:id])
  assert_equal("png", params[:format])
end
test_recognize_with_encoded_id_and_regex() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1205
def test_recognize_with_encoded_id_and_regex
  set.draw do
    get "page/:id" => "pages#show", :id => /[a-zA-Z0-9\+]+/
  end

  assert_equal({ controller: "pages", action: "show", id: "10" }, request_path_params("/page/10"))
  assert_equal({ controller: "pages", action: "show", id: "hello+world" }, request_path_params("/page/hello+world"))
end
test_recognize_with_http_methods() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1214
def test_recognize_with_http_methods
  set.draw do
    get    "/people"     => "people#index", :as => "people"
    post   "/people"     => "people#create"
    get    "/people/:id" => "people#show",  :as => "person"
    put    "/people/:id" => "people#update"
    patch  "/people/:id" => "people#update"
    delete "/people/:id" => "people#destroy"
  end

  params = request_path_params("/people", method: :get)
  assert_equal("index", params[:action])

  params = request_path_params("/people", method: :post)
  assert_equal("create", params[:action])

  params = request_path_params("/people/5", method: :put)
  assert_equal("update", params[:action])

  params = request_path_params("/people/5", method: :patch)
  assert_equal("update", params[:action])

  assert_raise(ActionController::UnknownHttpMethod) {
    request_path_params("/people", method: :bacon)
  }

  params = request_path_params("/people/5", method: :get)
  assert_equal("show", params[:action])
  assert_equal("5", params[:id])

  params = request_path_params("/people/5", method: :put)
  assert_equal("update", params[:action])
  assert_equal("5", params[:id])

  params = request_path_params("/people/5", method: :patch)
  assert_equal("update", params[:action])
  assert_equal("5", params[:id])

  params = request_path_params("/people/5", method: :delete)
  assert_equal("destroy", params[:action])
  assert_equal("5", params[:id])

  assert_raise(ActionController::RoutingError) {
    request_path_params("/people/5", method: :post)
  }
end
test_regexp_chunk_should_add_question_mark_for_optionals() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1696
def test_regexp_chunk_should_add_question_mark_for_optionals
  set.draw do
    get "/" => "foo#index"
    get "/hello" => "bar#index"
  end

  assert_equal "/",      url_for(set, controller: "foo")
  assert_equal "/hello", url_for(set, controller: "bar")

  assert_equal({ controller: "foo", action: "index" }, set.recognize_path("/"))
  assert_equal({ controller: "bar", action: "index" }, set.recognize_path("/hello"))
end
test_root_map() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1335
def test_root_map
  set.draw { root to: "people#index" }

  params = request_path_params("", method: :get)
  assert_equal("people", params[:controller])
  assert_equal("index", params[:action])
end
test_route_constraints_on_request_object_with_anchors_are_valid() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1153
def test_route_constraints_on_request_object_with_anchors_are_valid
  assert_nothing_raised do
    set.draw do
      get "page/:id" => "pages#show", :constraints => { host: /^foo$/ }
    end
  end
end
test_route_constraints_with_anchor_chars_are_invalid() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1161
def test_route_constraints_with_anchor_chars_are_invalid
  assert_raise ArgumentError do
    set.draw do
      get "page/:id" => "pages#show", :id => /^\d+/
    end
  end
  assert_raise ArgumentError do
    set.draw do
      get "page/:id" => "pages#show", :id => /\A\d+/
    end
  end
  assert_raise ArgumentError do
    set.draw do
      get "page/:id" => "pages#show", :id => /\d+$/
    end
  end
  assert_raise ArgumentError do
    set.draw do
      get "page/:id" => "pages#show", :id => /\d+\Z/
    end
  end
  assert_raise ArgumentError do
    set.draw do
      get "page/:id" => "pages#show", :id => /\d+\z/
    end
  end
end
test_route_constraints_with_options_method_condition_is_valid() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1189
def test_route_constraints_with_options_method_condition_is_valid
  assert_nothing_raised do
    set.draw do
      match "valid/route" => "pages#show", :via => :options
    end
  end
end
test_route_constraints_with_supported_options_must_not_error() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1589
def test_route_constraints_with_supported_options_must_not_error
  assert_nothing_raised do
    set.draw do
      get "page/:name" => "pages#show",
        :constraints => { name: /(david|jamis)/i }
    end
  end
  assert_nothing_raised do
    set.draw do
      get "page/:name" => "pages#show",
        :constraints => { name: / # Desperately overcommented regexp
                                    ( #Either
                                     david #The Creator
                                    | #Or
                                      jamis #The Deployer
                                    )/x }
    end
  end
end
test_route_constraints_with_unsupported_regexp_options_must_error() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1580
def test_route_constraints_with_unsupported_regexp_options_must_error
  assert_raise ArgumentError do
    set.draw do
      get "page/:name" => "pages#show",
        :constraints => { name: /(david|jamis)/m }
    end
  end
end
test_route_error_with_missing_controller() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1197
def test_route_error_with_missing_controller
  set.draw do
    get    "/people" => "missing#index"
  end

  assert_raises(ActionController::RoutingError) { request_path_params "/people" }
end
test_route_requirement_generate_with_ignore_case() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1634
def test_route_requirement_generate_with_ignore_case
  set.draw do
    get "page/:name" => "pages#show",
      :constraints => { name: /(david|jamis)/i }
  end

  url = url_for(set, controller: "pages", action: "show", name: "david")
  assert_equal "/page/david", url
  assert_raise(ActionController::UrlGenerationError) do
    url_for(set, controller: "pages", action: "show", name: "davidjamis")
  end
  url = url_for(set, controller: "pages", action: "show", name: "JAMIS")
  assert_equal "/page/JAMIS", url
end
test_route_requirement_recognize_with_extended_syntax() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1649
def test_route_requirement_recognize_with_extended_syntax
  set.draw do
    get "page/:name" => "pages#show",
      :constraints => { name: / # Desperately overcommented regexp
                                  ( #Either
                                   david #The Creator
                                  | #Or
                                    jamis #The Deployer
                                  )/x }
  end
  assert_equal({ controller: "pages", action: "show", name: "jamis" }, set.recognize_path("/page/jamis"))
  assert_equal({ controller: "pages", action: "show", name: "david" }, set.recognize_path("/page/david"))
  assert_raise ActionController::RoutingError do
    set.recognize_path("/page/david #The Creator")
  end
  assert_raise ActionController::RoutingError do
    set.recognize_path("/page/David")
  end
end
test_route_requirement_recognize_with_ignore_case() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1622
def test_route_requirement_recognize_with_ignore_case
  set.draw do
    get "page/:name" => "pages#show",
      :constraints => { name: /(david|jamis)/i }
  end
  assert_equal({ controller: "pages", action: "show", name: "jamis" }, set.recognize_path("/page/jamis"))
  assert_raise ActionController::RoutingError do
    set.recognize_path("/page/davidjamis")
  end
  assert_equal({ controller: "pages", action: "show", name: "DAVID" }, set.recognize_path("/page/DAVID"))
end
test_route_requirement_with_xi_modifiers() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1669
def test_route_requirement_with_xi_modifiers
  set.draw do
    get "page/:name" => "pages#show",
      :constraints => { name: / # Desperately overcommented regexp
                                  ( #Either
                                   david #The Creator
                                  | #Or
                                    jamis #The Deployer
                                  )/xi }
  end

  assert_equal({ controller: "pages", action: "show", name: "JAMIS" },
      set.recognize_path("/page/JAMIS"))

  assert_equal "/page/JAMIS",
      url_for(set, controller: "pages", action: "show", name: "JAMIS")
end
test_route_with_parameter_shell() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1136
def test_route_with_parameter_shell
  set.draw do
    get "page/:id" => "pages#show", :id => /\d+/

    ActiveSupport::Deprecation.silence do
      get "/:controller(/:action(/:id))"
    end
  end

  assert_equal({ controller: "pages", action: "index" }, request_path_params("/pages"))
  assert_equal({ controller: "pages", action: "index" }, request_path_params("/pages/index"))
  assert_equal({ controller: "pages", action: "list" }, request_path_params("/pages/list"))

  assert_equal({ controller: "pages", action: "show", id: "10" }, request_path_params("/pages/show/10"))
  assert_equal({ controller: "pages", action: "show", id: "10" }, request_path_params("/page/10"))
end
test_route_with_subdomain_and_constraints_must_receive_params() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1609
def test_route_with_subdomain_and_constraints_must_receive_params
  name_param = nil
  set.draw do
    get "page/:name" => "pages#show", :constraints => lambda { |request|
      name_param = request.params[:name]
      return true
    }
  end
  assert_equal({ controller: "pages", action: "show", name: "mypage" },
    set.recognize_path("http://subdomain.example.org/page/mypage"))
  assert_equal(name_param, "mypage")
end
test_routes_with_symbols() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1687
def test_routes_with_symbols
  set.draw do
    get "unnamed", controller: :pages, action: :show, name: :as_symbol
    get "named"  , controller: :pages, action: :show, name: :as_symbol, as: :named
  end
  assert_equal({ controller: "pages", action: "show", name: :as_symbol }, set.recognize_path("/unnamed"))
  assert_equal({ controller: "pages", action: "show", name: :as_symbol }, set.recognize_path("/named"))
end
test_routing_traversal_does_not_load_extra_classes() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1290
def test_routing_traversal_does_not_load_extra_classes
  assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
  set.draw do
    get "/profile" => "profile#index"
  end

  request_path_params("/profile") rescue nil

  assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
end
test_segmentation_of_dot_path() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1721
def test_segmentation_of_dot_path
  set.draw do
    ActiveSupport::Deprecation.silence do
      get "/books/:action.rss", controller: "books"
    end
  end

  assert_equal "/books/list.rss", url_for(set, controller: "books", action: "list")

  assert_equal({ controller: "books", action: "list" }, set.recognize_path("/books/list.rss"))
end
test_segmentation_of_dynamic_dot_path() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1733
def test_segmentation_of_dynamic_dot_path
  set.draw do
    ActiveSupport::Deprecation.silence do
      get "/books(/:action(.:format))", controller: "books"
    end
  end

  assert_equal "/books/list.rss", url_for(set, controller: "books", action: "list", format: "rss")
  assert_equal "/books/list.xml", url_for(set, controller: "books", action: "list", format: "xml")
  assert_equal "/books/list",     url_for(set, controller: "books", action: "list")
  assert_equal "/books",          url_for(set, controller: "books", action: "index")

  assert_equal({ controller: "books", action: "list", format: "rss" }, set.recognize_path("/books/list.rss"))
  assert_equal({ controller: "books", action: "list", format: "xml" }, set.recognize_path("/books/list.xml"))
  assert_equal({ controller: "books", action: "list" },  set.recognize_path("/books/list"))
  assert_equal({ controller: "books", action: "index" }, set.recognize_path("/books"))
end
test_setting_root_in_namespace_using_string() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1570
def test_setting_root_in_namespace_using_string
  assert_nothing_raised do
    set.draw do
      namespace "admin" do
        root to: "home#index"
      end
    end
  end
end
test_setting_root_in_namespace_using_symbol() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1560
def test_setting_root_in_namespace_using_symbol
  assert_nothing_raised do
    set.draw do
      namespace :admin do
        root to: "home#index"
      end
    end
  end
end
test_simple_build_query_string() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1815
def test_simple_build_query_string
  assert_uri_equal "/foo?x=1&y=2", url_for(default_route_set, controller: "foo", x: "1", y: "2")
end
test_slashes_are_implied() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1751
def test_slashes_are_implied
  set.draw { ActiveSupport::Deprecation.silence { get("/:controller(/:action(/:id))") } }

  assert_equal "/content",        url_for(set, controller: "content", action: "index")
  assert_equal "/content/list",   url_for(set, controller: "content", action: "list")
  assert_equal "/content/show/1", url_for(set, controller: "content", action: "show", id: "1")

  assert_equal({ controller: "content", action: "index" }, set.recognize_path("/content"))
  assert_equal({ controller: "content", action: "index" }, set.recognize_path("/content/index"))
  assert_equal({ controller: "content", action: "list" },  set.recognize_path("/content/list"))
  assert_equal({ controller: "content", action: "show", id: "1" }, set.recognize_path("/content/show/1"))
end
test_typo_recognition() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1276
def test_typo_recognition
  set.draw do
    get "articles/:year/:month/:day/:title" => "articles#permalink",
           :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
  end

  params = request_path_params("/articles/2005/11/05/a-very-interesting-article", method: :get)
  assert_equal("permalink", params[:action])
  assert_equal("2005", params[:year])
  assert_equal("11", params[:month])
  assert_equal("05", params[:day])
  assert_equal("a-very-interesting-article", params[:title])
end
test_use_static_path_when_possible() click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1407
def test_use_static_path_when_possible
  @set = make_set false

  set.draw do
    get "about" => "welcome#about"

    ActiveSupport::Deprecation.silence do
      get ":controller/:id/:action"
    end
  end

  get URI("http://test.host/welcom/get/7")

  assert_equal "/about", controller.url_for(controller: "welcome",
                                            action: "about",
                                            only_path: true)
end

Private Instance Methods

assert_uri_equal(expected, actual) click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1902
def assert_uri_equal(expected, actual)
  assert_equal(sort_query_string_params(expected), sort_query_string_params(actual))
end
sort_query_string_params(uri) click to toggle source
# File actionpack/test/controller/routing_test.rb, line 1906
def sort_query_string_params(uri)
  path, qs = uri.split("?")
  qs = qs.split("&").sort.join("&") if qs
  qs ? "#{path}?#{qs}" : path
end