2012-12-19 15:54:47 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
module Journey
|
2012-12-31 12:36:30 -05:00
|
|
|
class TestRoute < ActiveSupport::TestCase
|
2012-12-19 15:54:47 -05:00
|
|
|
def test_initialize
|
|
|
|
app = Object.new
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '/:controller(/:action(/:id(.:format)))'
|
2013-01-15 12:18:59 -05:00
|
|
|
defaults = {}
|
2015-08-17 19:07:39 -04:00
|
|
|
route = Route.build("name", app, path, {}, [], 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
|
|
|
|
app = Object.new
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '/:controller(/:action(/:id(.:format)))'
|
2013-01-15 12:18:59 -05:00
|
|
|
defaults = {}
|
2015-08-17 19:07:39 -04:00
|
|
|
route = Route.build("name", app, path, {}, [], defaults)
|
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
|
2015-08-17 16:29:54 -04:00
|
|
|
path = Path::Pattern.build(':name', { name: /love/ }, '/', true)
|
2015-01-07 18:53:57 -05:00
|
|
|
defaults = { name: 'tender' }
|
2015-08-17 19:30:53 -04:00
|
|
|
route = Route.build('name', nil, path, {}, [], 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
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '/messages/:id(.:format)'
|
2015-08-17 19:07:39 -04:00
|
|
|
route = Route.build("name", nil, path, {:ip => '192.168.1.1'}, [],
|
2012-12-19 15:54:47 -05:00
|
|
|
{ :controller => 'foo', :action => 'bar' })
|
|
|
|
assert_equal '192.168.1.1', route.ip
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_ip
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '/messages/:id(.:format)'
|
2015-08-17 19:07:39 -04:00
|
|
|
route = Route.build("name", nil, path, {}, [],
|
2012-12-19 15:54:47 -05:00
|
|
|
{ :controller => 'foo', :action => 'bar' })
|
|
|
|
assert_equal(//, route.ip)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_format_with_star
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '/:controller/*extra'
|
2015-08-17 19:07:39 -04:00
|
|
|
route = Route.build("name", nil, path, {}, [],
|
2012-12-19 15:54:47 -05:00
|
|
|
{ :controller => 'foo', :action => 'bar' })
|
|
|
|
assert_equal '/foo/himom', route.format({
|
|
|
|
:controller => 'foo',
|
|
|
|
:extra => 'himom',
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_connects_all_match
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '/:controller(/:action(/:id(.:format)))'
|
2015-08-17 19:07:39 -04:00
|
|
|
route = Route.build("name", nil, path, {:action => 'bar'}, [], { :controller => 'foo' })
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
assert_equal '/foo/bar/10', route.format({
|
|
|
|
:controller => 'foo',
|
|
|
|
:action => 'bar',
|
|
|
|
:id => 10
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extras_are_not_included_if_optional
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '/page/:id(/:action)'
|
2015-08-17 19:07:39 -04:00
|
|
|
route = Route.build("name", nil, path, { }, [], { :action => 'show' })
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
assert_equal '/page/10', route.format({ :id => 10 })
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extras_are_not_included_if_optional_with_parameter
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '(/sections/:section)/pages/:id'
|
2015-08-17 19:07:39 -04:00
|
|
|
route = Route.build("name", nil, path, { }, [], { :action => 'show' })
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
assert_equal '/pages/10', route.format({:id => 10})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extras_are_not_included_if_optional_parameter_is_nil
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '(/sections/:section)/pages/:id'
|
2015-08-17 19:07:39 -04:00
|
|
|
route = Route.build("name", nil, path, { }, [], { :action => 'show' })
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
assert_equal '/pages/10', route.format({:id => 10, :section => nil})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_score
|
2015-06-08 20:18:06 -04:00
|
|
|
constraints = {}
|
2013-01-15 12:18:59 -05:00
|
|
|
defaults = {:controller=>"pages", :action=>"show"}
|
|
|
|
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string "/page/:id(/:action)(.:format)"
|
2015-08-17 19:07:39 -04:00
|
|
|
specific = Route.build "name", nil, path, constraints, [:controller, :action], defaults
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string "/:controller(/:action(/:id))(.:format)"
|
2015-08-17 19:07:39 -04:00
|
|
|
generic = Route.build "name", nil, path, constraints, [], {}
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
knowledge = {:id=>20, :controller=>"pages", :action=>"show"}
|
|
|
|
|
|
|
|
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
|