2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2011-03-11 21:06:10 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
module Routing
|
|
|
|
class MapperTest < ActiveSupport::TestCase
|
2014-07-01 18:33:30 -04:00
|
|
|
class FakeSet < ActionDispatch::Routing::RouteSet
|
2011-03-11 21:06:10 -05:00
|
|
|
def resources_path_names
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_class
|
|
|
|
ActionDispatch::Request
|
|
|
|
end
|
|
|
|
|
2013-07-07 17:34:46 -04:00
|
|
|
def dispatcher_class
|
|
|
|
RouteSet::Dispatcher
|
|
|
|
end
|
|
|
|
|
2015-08-12 18:17:21 -04:00
|
|
|
def defaults
|
2015-08-14 17:41:48 -04:00
|
|
|
routes.map(&:defaults)
|
2015-08-12 18:17:21 -04:00
|
|
|
end
|
|
|
|
|
2011-03-11 21:06:10 -05:00
|
|
|
def conditions
|
2015-08-14 17:41:48 -04:00
|
|
|
routes.map(&:constraints)
|
2011-03-11 21:06:10 -05:00
|
|
|
end
|
2011-03-28 15:59:24 -04:00
|
|
|
|
|
|
|
def requirements
|
2015-08-14 13:05:03 -04:00
|
|
|
routes.map(&:path).map(&:requirements)
|
2011-03-28 15:59:24 -04:00
|
|
|
end
|
2015-08-14 12:45:44 -04:00
|
|
|
|
|
|
|
def asts
|
2015-08-14 12:49:53 -04:00
|
|
|
routes.map(&:path).map(&:spec)
|
2015-08-14 12:45:44 -04:00
|
|
|
end
|
2011-03-11 21:06:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize
|
|
|
|
Mapper.new FakeSet.new
|
|
|
|
end
|
|
|
|
|
2015-08-13 16:40:55 -04:00
|
|
|
def test_scope_raises_on_anchor
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
assert_raises(ArgumentError) do
|
|
|
|
mapper.scope(anchor: false) do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-12 17:20:15 -04:00
|
|
|
def test_blows_up_without_via
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
assert_raises(ArgumentError) do
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.match "/", to: "posts#index", as: :main
|
2015-08-12 17:20:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-12 19:29:33 -04:00
|
|
|
def test_unscoped_formatted
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/foo", to: "posts#index", as: :main, format: true
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ controller: "posts", action: "index" },
|
2015-08-12 19:29:33 -04:00
|
|
|
fakeset.defaults.first)
|
2015-08-14 12:45:44 -04:00
|
|
|
assert_equal "/foo.:format", fakeset.asts.first.to_s
|
2015-08-12 19:29:33 -04:00
|
|
|
end
|
|
|
|
|
2015-08-12 19:24:26 -04:00
|
|
|
def test_scoped_formatted
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
mapper.scope(format: true) do
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/foo", to: "posts#index", as: :main
|
2015-08-12 19:24:26 -04:00
|
|
|
end
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ controller: "posts", action: "index" },
|
2015-08-12 19:24:26 -04:00
|
|
|
fakeset.defaults.first)
|
2015-08-14 12:45:44 -04:00
|
|
|
assert_equal "/foo.:format", fakeset.asts.first.to_s
|
2015-08-12 19:24:26 -04:00
|
|
|
end
|
|
|
|
|
2015-08-12 18:17:21 -04:00
|
|
|
def test_random_keys
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
mapper.scope(omg: :awesome) do
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/", to: "posts#index", as: :main
|
2015-08-12 18:17:21 -04:00
|
|
|
end
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ omg: :awesome, controller: "posts", action: "index" },
|
2015-08-12 18:17:21 -04:00
|
|
|
fakeset.defaults.first)
|
2015-10-03 05:28:29 -04:00
|
|
|
assert_equal("GET", fakeset.routes.first.verb)
|
2015-08-12 18:17:21 -04:00
|
|
|
end
|
|
|
|
|
2011-07-26 20:33:22 -04:00
|
|
|
def test_mapping_requirements
|
2016-08-16 03:30:11 -04:00
|
|
|
options = {}
|
2015-08-12 17:30:40 -04:00
|
|
|
scope = Mapper::Scope.new({})
|
2016-08-06 12:54:50 -04:00
|
|
|
ast = Journey::Parser.parse "/store/:name(*rest)"
|
|
|
|
m = Mapper::Mapping.build(scope, FakeSet.new, ast, "foo", "bar", nil, [:get], nil, {}, true, options)
|
2015-08-14 17:41:48 -04:00
|
|
|
assert_equal(/.+?/, m.requirements[:rest])
|
2011-07-26 20:33:22 -04:00
|
|
|
end
|
|
|
|
|
2015-08-12 17:12:17 -04:00
|
|
|
def test_via_scope
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
mapper.scope(via: :put) do
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.match "/", to: "posts#index", as: :main
|
2015-08-12 17:12:17 -04:00
|
|
|
end
|
2015-10-03 05:28:29 -04:00
|
|
|
assert_equal("PUT", fakeset.routes.first.verb)
|
2015-08-12 17:12:17 -04:00
|
|
|
end
|
|
|
|
|
2016-06-27 15:02:19 -04:00
|
|
|
def test_to_scope
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
mapper.scope(to: "posts#index") do
|
|
|
|
mapper.get :all
|
|
|
|
mapper.post :most
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "posts#index", fakeset.routes.to_a[0].defaults[:to]
|
|
|
|
assert_equal "posts#index", fakeset.routes.to_a[1].defaults[:to]
|
|
|
|
end
|
|
|
|
|
2011-03-11 21:06:10 -05:00
|
|
|
def test_map_slash
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/", to: "posts#index", as: :main
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/", fakeset.asts.first.to_s
|
2011-03-11 21:06:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_more_slashes
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
|
|
|
|
# FIXME: is this a desired behavior?
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/one/two/", to: "posts#index", as: :main
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/one/two(.:format)", fakeset.asts.first.to_s
|
2011-03-11 21:06:10 -05:00
|
|
|
end
|
2011-03-22 11:19:31 -04:00
|
|
|
|
|
|
|
def test_map_wildcard
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/*path", to: "pages#show"
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/*path(.:format)", fakeset.asts.first.to_s
|
2011-03-29 08:40:42 -04:00
|
|
|
assert_equal(/.+?/, fakeset.requirements.first[:path])
|
2011-03-28 15:59:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_wildcard_with_other_element
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/*path/foo/:bar", to: "pages#show"
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/*path/foo/:bar(.:format)", fakeset.asts.first.to_s
|
2014-05-29 17:57:48 -04:00
|
|
|
assert_equal(/.+?/, fakeset.requirements.first[:path])
|
2011-03-28 15:59:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_wildcard_with_multiple_wildcard
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/*foo/*bar", to: "pages#show"
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/*foo/*bar(.:format)", fakeset.asts.first.to_s
|
2014-05-29 17:57:48 -04:00
|
|
|
assert_equal(/.+?/, fakeset.requirements.first[:foo])
|
2011-03-29 08:40:42 -04:00
|
|
|
assert_equal(/.+?/, fakeset.requirements.first[:bar])
|
2011-03-28 15:59:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_wildcard_with_format_false
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/*path", to: "pages#show", format: false
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/*path", fakeset.asts.first.to_s
|
2011-03-28 15:59:24 -04:00
|
|
|
assert_nil fakeset.requirements.first[:path]
|
2011-03-22 11:19:31 -04:00
|
|
|
end
|
2011-07-25 14:37:25 -04:00
|
|
|
|
|
|
|
def test_map_wildcard_with_format_true
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2016-08-06 13:35:13 -04:00
|
|
|
mapper.get "/*path", to: "pages#show", format: true
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/*path.:format", fakeset.asts.first.to_s
|
2011-07-25 14:37:25 -04:00
|
|
|
end
|
2012-07-10 19:16:00 -04:00
|
|
|
|
2015-11-22 05:22:36 -05:00
|
|
|
def test_raising_error_when_path_is_not_passed
|
2012-07-10 19:16:00 -04:00
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
app = lambda { |env| [200, {}, [""]] }
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
mapper.mount app
|
|
|
|
end
|
|
|
|
end
|
2015-11-22 05:22:36 -05:00
|
|
|
|
|
|
|
def test_raising_error_when_rack_app_is_not_passed
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
mapper.mount 10, as: "exciting"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
mapper.mount as: "exciting"
|
|
|
|
end
|
|
|
|
end
|
2016-03-03 10:25:28 -05:00
|
|
|
|
|
|
|
def test_scope_does_not_destructively_mutate_default_options
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
|
|
|
|
frozen = { foo: :bar }.freeze
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
mapper.scope(defaults: frozen) do
|
|
|
|
# pass
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-03-11 21:06:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|