2011-03-11 21:06:10 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
module Routing
|
|
|
|
class MapperTest < ActiveSupport::TestCase
|
|
|
|
class FakeSet
|
2012-06-29 13:33:09 -04:00
|
|
|
attr_reader :routes
|
2011-09-11 20:14:29 -04:00
|
|
|
alias :set :routes
|
2011-03-11 21:06:10 -05:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@routes = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def resources_path_names
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_class
|
|
|
|
ActionDispatch::Request
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_route(*args)
|
|
|
|
routes << args
|
|
|
|
end
|
|
|
|
|
|
|
|
def conditions
|
|
|
|
routes.map { |x| x[1] }
|
|
|
|
end
|
2011-03-28 15:59:24 -04:00
|
|
|
|
|
|
|
def requirements
|
|
|
|
routes.map { |x| x[2] }
|
|
|
|
end
|
2011-03-11 21:06:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize
|
|
|
|
Mapper.new FakeSet.new
|
|
|
|
end
|
|
|
|
|
2011-07-26 20:33:22 -04:00
|
|
|
def test_mapping_requirements
|
2012-04-24 23:32:09 -04:00
|
|
|
options = { :controller => 'foo', :action => 'bar', :via => :get }
|
2011-07-26 20:33:22 -04:00
|
|
|
m = Mapper::Mapping.new FakeSet.new, {}, '/store/:name(*rest)', options
|
|
|
|
_, _, requirements, _ = m.to_route
|
|
|
|
assert_equal(/.+?/, requirements[:rest])
|
|
|
|
end
|
|
|
|
|
2011-03-11 21:06:10 -05:00
|
|
|
def test_map_slash
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2012-04-24 23:32:09 -04:00
|
|
|
mapper.get '/', :to => 'posts#index', :as => :main
|
2011-03-11 21:06:10 -05:00
|
|
|
assert_equal '/', fakeset.conditions.first[:path_info]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_more_slashes
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
|
|
|
|
# FIXME: is this a desired behavior?
|
2012-04-24 23:32:09 -04:00
|
|
|
mapper.get '/one/two/', :to => 'posts#index', :as => :main
|
2011-03-11 21:06:10 -05:00
|
|
|
assert_equal '/one/two(.:format)', fakeset.conditions.first[:path_info]
|
|
|
|
end
|
2011-03-22 11:19:31 -04:00
|
|
|
|
|
|
|
def test_map_wildcard
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2012-04-24 23:32:09 -04:00
|
|
|
mapper.get '/*path', :to => 'pages#show'
|
2011-03-28 15:59:24 -04:00
|
|
|
assert_equal '/*path(.:format)', fakeset.conditions.first[:path_info]
|
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
|
2012-04-24 23:32:09 -04:00
|
|
|
mapper.get '/*path/foo/:bar', :to => 'pages#show'
|
2011-03-28 15:59:24 -04:00
|
|
|
assert_equal '/*path/foo/:bar(.:format)', fakeset.conditions.first[:path_info]
|
|
|
|
assert_nil fakeset.requirements.first[:path]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_wildcard_with_multiple_wildcard
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
2012-04-24 23:32:09 -04:00
|
|
|
mapper.get '/*foo/*bar', :to => 'pages#show'
|
2011-03-28 15:59:24 -04:00
|
|
|
assert_equal '/*foo/*bar(.:format)', fakeset.conditions.first[:path_info]
|
|
|
|
assert_nil 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
|
2012-04-24 23:32:09 -04:00
|
|
|
mapper.get '/*path', :to => 'pages#show', :format => false
|
2011-03-22 11:19:31 -04:00
|
|
|
assert_equal '/*path', fakeset.conditions.first[:path_info]
|
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
|
2012-04-24 23:32:09 -04:00
|
|
|
mapper.get '/*path', :to => 'pages#show', :format => true
|
2011-07-25 14:37:25 -04:00
|
|
|
assert_equal '/*path.:format', fakeset.conditions.first[:path_info]
|
|
|
|
end
|
2012-07-10 19:16:00 -04:00
|
|
|
|
|
|
|
def test_raising_helpful_error_on_invalid_arguments
|
|
|
|
fakeset = FakeSet.new
|
|
|
|
mapper = Mapper.new fakeset
|
|
|
|
app = lambda { |env| [200, {}, [""]] }
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
mapper.mount app
|
|
|
|
end
|
|
|
|
end
|
2011-03-11 21:06:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|