2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2020-07-27 12:23:33 -04:00
|
|
|
require "support/path_helper"
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
module Journey
|
2012-12-31 12:36:30 -05:00
|
|
|
class TestRoute < ActiveSupport::TestCase
|
2020-07-27 12:23:33 -04:00
|
|
|
include PathHelper
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
def test_initialize
|
|
|
|
app = Object.new
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "/:controller(/:action(/:id(.:format)))"
|
2013-01-15 12:18:59 -05:00
|
|
|
defaults = {}
|
2019-05-22 17:31:22 -04:00
|
|
|
route = Route.new(name: "name", app: app, path: path, defaults: defaults)
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
assert_equal app, route.app
|
|
|
|
assert_equal path, route.path
|
2013-01-15 12:18:59 -05:00
|
|
|
assert_same defaults, route.defaults
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_adds_itself_as_memo
|
2019-05-22 17:31:22 -04:00
|
|
|
app = Object.new
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "/:controller(/:action(/:id(.:format)))"
|
2019-05-22 17:31:22 -04:00
|
|
|
route = Route.new(name: "name", app: app, path: path)
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
route.ast.grep(Nodes::Terminal).each do |node|
|
|
|
|
assert_equal route, node.memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-07 18:53:57 -05:00
|
|
|
def test_path_requirements_override_defaults
|
2020-07-27 12:23:33 -04:00
|
|
|
path = build_path(":name", { name: /love/ }, "/", true)
|
2019-05-22 17:31:22 -04:00
|
|
|
defaults = { name: "tender" }
|
|
|
|
route = Route.new(name: "name", path: path, defaults: defaults)
|
2015-05-04 10:08:24 -04:00
|
|
|
assert_equal(/love/, route.requirements[:name])
|
2015-01-07 18:53:57 -05:00
|
|
|
end
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
def test_ip_address
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "/messages/:id(.:format)"
|
2019-05-22 17:31:22 -04:00
|
|
|
route = Route.new(name: "name", path: path, constraints: { ip: "192.168.1.1" },
|
|
|
|
defaults: { controller: "foo", action: "bar" })
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "192.168.1.1", route.ip
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_ip
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "/messages/:id(.:format)"
|
2019-05-22 17:31:22 -04:00
|
|
|
route = Route.new(name: "name", path: path,
|
|
|
|
defaults: { controller: "foo", action: "bar" })
|
2012-12-19 15:54:47 -05:00
|
|
|
assert_equal(//, route.ip)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_format_with_star
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "/:controller/*extra"
|
2019-05-22 17:31:22 -04:00
|
|
|
route = Route.new(name: "name", path: path,
|
|
|
|
defaults: { controller: "foo", action: "bar" })
|
2016-08-09 17:36:39 -04:00
|
|
|
assert_equal "/foo/himom", route.format(
|
|
|
|
controller: "foo",
|
2016-08-06 13:44:11 -04:00
|
|
|
extra: "himom")
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_connects_all_match
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "/:controller(/:action(/:id(.:format)))"
|
2019-05-22 17:31:22 -04:00
|
|
|
route = Route.new(name: "name", path: path, constraints: { action: "bar" },
|
|
|
|
defaults: { controller: "foo" })
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2016-08-09 17:36:39 -04:00
|
|
|
assert_equal "/foo/bar/10", route.format(
|
|
|
|
controller: "foo",
|
2016-08-06 13:35:13 -04:00
|
|
|
action: "bar",
|
2016-08-06 13:44:11 -04:00
|
|
|
id: 10)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_extras_are_not_included_if_optional
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "/page/:id(/:action)"
|
2019-05-22 17:31:22 -04:00
|
|
|
route = Route.new(name: "name", path: path, defaults: { action: "show" })
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_equal "/page/10", route.format(id: 10)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_extras_are_not_included_if_optional_with_parameter
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "(/sections/:section)/pages/:id"
|
2019-05-22 17:31:22 -04:00
|
|
|
route = Route.new(name: "name", path: path, defaults: { action: "show" })
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_equal "/pages/10", route.format(id: 10)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_extras_are_not_included_if_optional_parameter_is_nil
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "(/sections/:section)/pages/:id"
|
2019-05-22 17:31:22 -04:00
|
|
|
route = Route.new(name: "name", path: path, defaults: { action: "show" })
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_equal "/pages/10", route.format(id: 10, section: nil)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_score
|
2016-08-16 03:30:11 -04:00
|
|
|
defaults = { controller: "pages", action: "show" }
|
2013-01-15 12:18:59 -05:00
|
|
|
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "/page/:id(/:action)(.:format)"
|
2019-05-22 17:31:22 -04:00
|
|
|
specific = Route.new name: "name", path: path, required_defaults: [:controller, :action], defaults: defaults
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2020-07-27 12:23:33 -04:00
|
|
|
path = path_from_string "/:controller(/:action(/:id))(.:format)"
|
2019-05-22 17:31:22 -04:00
|
|
|
generic = Route.new name: "name", path: path
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2016-12-28 14:35:43 -05:00
|
|
|
knowledge = { "id" => true, "controller" => true, "action" => true }
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
routes = [specific, generic]
|
|
|
|
|
2012-12-28 18:49:41 -05:00
|
|
|
assert_not_equal specific.score(knowledge), generic.score(knowledge)
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
found = routes.sort_by { |r| r.score(knowledge) }.last
|
|
|
|
|
|
|
|
assert_equal specific, found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|