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 = {}
|
2012-12-19 15:54:47 -05:00
|
|
|
route = Route.new("name", app, path, {}, defaults)
|
|
|
|
|
|
|
|
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 = {}
|
2012-12-19 15:54:47 -05:00
|
|
|
route = Route.new("name", app, path, {}, defaults)
|
|
|
|
|
|
|
|
route.ast.grep(Nodes::Terminal).each do |node|
|
|
|
|
assert_equal route, node.memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ip_address
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string '/messages/:id(.:format)'
|
2012-12-19 15:54:47 -05:00
|
|
|
route = Route.new("name", nil, path, {:ip => '192.168.1.1'},
|
|
|
|
{ :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)'
|
2012-12-19 15:54:47 -05:00
|
|
|
route = Route.new("name", nil, path, {},
|
|
|
|
{ :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'
|
2012-12-19 15:54:47 -05:00
|
|
|
route = Route.new("name", nil, path, {},
|
|
|
|
{ :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)))'
|
2012-12-19 15:54:47 -05:00
|
|
|
route = Route.new("name", nil, path, {:action => 'bar'}, { :controller => 'foo' })
|
|
|
|
|
|
|
|
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)'
|
2012-12-19 15:54:47 -05:00
|
|
|
route = Route.new("name", nil, path, { }, { :action => 'show' })
|
|
|
|
|
|
|
|
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'
|
2012-12-19 15:54:47 -05:00
|
|
|
route = Route.new("name", nil, path, { }, { :action => 'show' })
|
|
|
|
|
|
|
|
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'
|
2012-12-19 15:54:47 -05:00
|
|
|
route = Route.new("name", nil, path, { }, { :action => 'show' })
|
|
|
|
|
|
|
|
assert_equal '/pages/10', route.format({:id => 10, :section => nil})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_score
|
2013-01-15 12:18:59 -05:00
|
|
|
constraints = {:required_defaults => [:controller, :action]}
|
|
|
|
defaults = {:controller=>"pages", :action=>"show"}
|
|
|
|
|
2014-05-29 18:44:54 -04:00
|
|
|
path = Path::Pattern.from_string "/page/:id(/:action)(.:format)"
|
2013-01-15 12:18:59 -05:00
|
|
|
specific = Route.new "name", nil, path, constraints, 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)"
|
2013-01-15 12:18:59 -05:00
|
|
|
generic = Route.new "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
|