提交 c767fbfc 编写于 作者: A Aaron Patterson

Merge branch 'mapper'

* mapper: (34 commits)
  no more is_a checks on instantiation
  Path::Pattern is instantiated internally, so make the contructor require a strexp object
  Strexp#names is only used in a test, so rm
  pass the parsed path from mapper to the Strexp
  add an alternate constructor to Strexp that takes a string
  ask the strexp for the ast
  remove dead code
  disconnect path from the instance
  reuse the ast we already made
  use a parser to extract the group parts from the path
  pass the parsed parameters through the methods so we don't reparse or require caching code
  "controllers" should be a valid path name
  controllers with slash names are also not supported, so we can reuse the message
  only validate controllers
  golf down a bit
  only error handling between controller and action is the same
  add a test for controllers without colons
  move nil check to a method that yields to a block if the value is not nil
  translate action / controller to the desired object
  only one nil check on the action variable
  ...
...@@ -93,6 +93,10 @@ def type; :GROUP; end ...@@ -93,6 +93,10 @@ def type; :GROUP; end
class Star < Unary # :nodoc: class Star < Unary # :nodoc:
def type; :STAR; end def type; :STAR; end
def name
left.name.tr '*:', ''
end
end end
class Binary < Node # :nodoc: class Binary < Node # :nodoc:
......
require 'action_dispatch/journey/router/strexp'
module ActionDispatch module ActionDispatch
module Journey # :nodoc: module Journey # :nodoc:
module Path # :nodoc: module Path # :nodoc:
class Pattern # :nodoc: class Pattern # :nodoc:
attr_reader :spec, :requirements, :anchored attr_reader :spec, :requirements, :anchored
def self.from_string string
new Journey::Router::Strexp.build(string, {}, ["/.?"], true)
end
def initialize(strexp) def initialize(strexp)
parser = Journey::Parser.new @spec = strexp.ast
@requirements = strexp.requirements
@anchored = true @separators = strexp.separators.join
@anchored = strexp.anchor
case strexp
when String
@spec = parser.parse(strexp)
@requirements = {}
@separators = "/.?"
when Router::Strexp
@spec = parser.parse(strexp.path)
@requirements = strexp.requirements
@separators = strexp.separators.join
@anchored = strexp.anchor
else
raise ArgumentError, "Bad expression: #{strexp}"
end
@names = nil @names = nil
@optional_names = nil @optional_names = nil
......
...@@ -6,18 +6,21 @@ class << self ...@@ -6,18 +6,21 @@ class << self
alias :compile :new alias :compile :new
end end
attr_reader :path, :requirements, :separators, :anchor attr_reader :path, :requirements, :separators, :anchor, :ast
def initialize(path, requirements, separators, anchor = true) def self.build(path, requirements, separators, anchor = true)
parser = Journey::Parser.new
ast = parser.parse path
new ast, path, requirements, separators, anchor
end
def initialize(ast, path, requirements, separators, anchor = true)
@ast = ast
@path = path @path = path
@requirements = requirements @requirements = requirements
@separators = separators @separators = separators
@anchor = anchor @anchor = anchor
end end
def names
@path.scan(/:\w+/).map { |s| s.tr(':', '') }
end
end end
end end
end end
......
...@@ -62,13 +62,12 @@ def constraint_args(constraint, request) ...@@ -62,13 +62,12 @@ def constraint_args(constraint, request)
class Mapping #:nodoc: class Mapping #:nodoc:
IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix, :format] IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix, :format]
ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z} ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
WILDCARD_PATH = %r{\*([^/\)]+)\)?$}
attr_reader :scope, :path, :options, :requirements, :conditions, :defaults attr_reader :scope, :options, :requirements, :conditions, :defaults
attr_reader :to, :default_controller, :default_action attr_reader :to, :default_controller, :default_action
def initialize(set, scope, path, options) def initialize(set, scope, path, options)
@set, @scope, @path = set, scope, path @set, @scope = set, scope
@requirements, @conditions, @defaults = {}, {}, {} @requirements, @conditions, @defaults = {}, {}, {}
options = scope[:options].merge(options) if scope[:options] options = scope[:options].merge(options) if scope[:options]
...@@ -76,10 +75,12 @@ def initialize(set, scope, path, options) ...@@ -76,10 +75,12 @@ def initialize(set, scope, path, options)
@default_controller = options[:controller] || scope[:controller] @default_controller = options[:controller] || scope[:controller]
@default_action = options[:action] || scope[:action] @default_action = options[:action] || scope[:action]
@options = normalize_options!(options) path = normalize_path! path, options[:format]
normalize_path! ast = path_ast path
normalize_requirements! path_params = path_params ast
normalize_conditions! @options = normalize_options!(options, path_params, ast)
normalize_requirements!(path_params)
normalize_conditions!(path_params, path, ast)
normalize_defaults! normalize_defaults!
end end
...@@ -89,35 +90,33 @@ def to_route ...@@ -89,35 +90,33 @@ def to_route
private private
def normalize_path! def normalize_path!(path, format)
raise ArgumentError, "path is required" if @path.blank? raise ArgumentError, "path is required" if path.blank?
@path = Mapper.normalize_path(@path) path = Mapper.normalize_path(path)
if required_format? if format == true
@path = "#{@path}.:format" "#{path}.:format"
elsif optional_format? elsif optional_format?(path, format)
@path = "#{@path}(.:format)" "#{path}(.:format)"
else
path
end end
end end
def required_format? def optional_format?(path, format)
options[:format] == true format != false && !path.include?(':format') && !path.end_with?('/')
end
def optional_format?
options[:format] != false && !path.include?(':format') && !path.end_with?('/')
end end
def normalize_options!(options) def normalize_options!(options, path_params, path_ast)
path_without_format = path.sub(/\(\.:format\)$/, '')
# Add a constraint for wildcard route to make it non-greedy and match the # Add a constraint for wildcard route to make it non-greedy and match the
# optional format part of the route by default # optional format part of the route by default
if path_without_format.match(WILDCARD_PATH) && options[:format] != false if options[:format] != false
options[$1.to_sym] ||= /.+?/ path_ast.grep(Journey::Nodes::Star) do |node|
options[node.name.to_sym] ||= /.+?/
end
end end
if path_without_format.match(':controller') if path_params.include?(:controller)
raise ArgumentError, ":controller segment is not allowed within a namespace block" if scope[:module] raise ArgumentError, ":controller segment is not allowed within a namespace block" if scope[:module]
# Add a default constraint for :controller path segments that matches namespaced # Add a default constraint for :controller path segments that matches namespaced
...@@ -127,12 +126,16 @@ def normalize_options!(options) ...@@ -127,12 +126,16 @@ def normalize_options!(options)
options[:controller] ||= /.+?/ options[:controller] ||= /.+?/
end end
options.merge!(default_controller_and_action) if to.respond_to? :call
options
else
options.merge!(default_controller_and_action(path_params))
end
end end
def normalize_requirements! def normalize_requirements!(path_params)
constraints.each do |key, requirement| constraints.each do |key, requirement|
next unless segment_keys.include?(key) || key == :controller next unless path_params.include?(key) || key == :controller
verify_regexp_requirement(requirement) if requirement.is_a?(Regexp) verify_regexp_requirement(requirement) if requirement.is_a?(Regexp)
@requirements[key] = requirement @requirements[key] = requirement
end end
...@@ -189,18 +192,19 @@ def verify_callable_constraint(callable_constraint) ...@@ -189,18 +192,19 @@ def verify_callable_constraint(callable_constraint)
end end
end end
def normalize_conditions! def normalize_conditions!(path_params, path, ast)
@conditions[:path_info] = path @conditions[:path_info] = path
@conditions[:parsed_path_info] = ast
constraints.each do |key, condition| constraints.each do |key, condition|
unless segment_keys.include?(key) || key == :controller unless path_params.include?(key) || key == :controller
@conditions[key] = condition @conditions[key] = condition
end end
end end
required_defaults = [] required_defaults = []
options.each do |key, required_default| options.each do |key, required_default|
unless segment_keys.include?(key) || IGNORE_OPTIONS.include?(key) || Regexp === required_default unless path_params.include?(key) || IGNORE_OPTIONS.include?(key) || Regexp === required_default
required_defaults << key required_defaults << key
end end
end end
...@@ -236,55 +240,61 @@ def app ...@@ -236,55 +240,61 @@ def app
end end
end end
def default_controller_and_action def default_controller_and_action(path_params)
if to.respond_to?(:call) controller, action = get_controller_and_action(default_controller,
{ } default_action,
else to,
if to.is_a?(String) @scope[:module]
controller, action = to.split('#') )
elsif to.is_a?(Symbol)
action = to.to_s
end
controller ||= default_controller
action ||= default_action
if @scope[:module] && !controller.is_a?(Regexp) hash = check_part(:controller, controller, path_params, {}) do |part|
if controller =~ %r{\A/} translate_controller(part) {
controller = controller[1..-1] message = "'#{part}' is not a supported controller name. This can lead to potential routing problems."
else message << " See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use"
controller = [@scope[:module], controller].compact.join("/").presence
end
end
if controller.is_a?(String) && controller =~ %r{\A/} raise ArgumentError, message
raise ArgumentError, "controller name should not start with a slash" }
end end
controller = controller.to_s unless controller.is_a?(Regexp) check_part(:action, action, path_params, hash) { |part|
action = action.to_s unless action.is_a?(Regexp) part.is_a?(Regexp) ? part : part.to_s
}
end
if controller.blank? && segment_keys.exclude?(:controller) def check_part(name, part, path_params, hash)
message = "Missing :controller key on routes definition, please check your routes." if part
hash[name] = yield(part)
else
unless path_params.include?(name)
message = "Missing :#{name} key on routes definition, please check your routes."
raise ArgumentError, message raise ArgumentError, message
end end
end
hash
end
if action.blank? && segment_keys.exclude?(:action) def get_controller_and_action(controller, action, to, modyoule)
message = "Missing :action key on routes definition, please check your routes." case to
raise ArgumentError, message when Symbol then action = to.to_s
end when /#/ then controller, action = to.split('#')
when String then controller = to
end
if controller.is_a?(String) && controller !~ /\A[a-z_0-9\/]*\z/ if modyoule && !controller.is_a?(Regexp)
message = "'#{controller}' is not a supported controller name. This can lead to potential routing problems." if controller =~ %r{\A/}
message << " See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use" controller = controller[1..-1]
raise ArgumentError, message else
controller = [modyoule, controller].compact.join("/")
end end
hash = {}
hash[:controller] = controller unless controller.blank?
hash[:action] = action unless action.blank?
hash
end end
[controller, action]
end
def translate_controller(controller)
return controller if Regexp === controller
return controller.to_s if controller =~ /\A[a-z_0-9][a-z_0-9\/]*\z/
yield
end end
def blocks def blocks
...@@ -307,16 +317,13 @@ def constraints ...@@ -307,16 +317,13 @@ def constraints
end end
end end
def segment_keys def path_params(ast)
@segment_keys ||= path_pattern.names.map{ |s| s.to_sym } ast.grep(Journey::Nodes::Symbol).map { |n| n.name.to_sym }
end
def path_pattern
Journey::Path::Pattern.new(strexp)
end end
def strexp def path_ast(path)
Journey::Router::Strexp.compile(path, requirements, SEPARATORS) parser = Journey::Parser.new
parser.parse path
end end
def dispatcher def dispatcher
......
...@@ -418,7 +418,9 @@ def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil ...@@ -418,7 +418,9 @@ def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil
"http://guides.rubyonrails.org/routing.html#restricting-the-routes-created" "http://guides.rubyonrails.org/routing.html#restricting-the-routes-created"
end end
path = build_path(conditions.delete(:path_info), requirements, SEPARATORS, anchor) path = conditions.delete :path_info
ast = conditions.delete :parsed_path_info
path = build_path(path, ast, requirements, SEPARATORS, anchor)
conditions = build_conditions(conditions, path.names.map { |x| x.to_sym }) conditions = build_conditions(conditions, path.names.map { |x| x.to_sym })
route = @set.add_route(app, path, conditions, defaults, name) route = @set.add_route(app, path, conditions, defaults, name)
...@@ -426,8 +428,9 @@ def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil ...@@ -426,8 +428,9 @@ def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil
route route
end end
def build_path(path, requirements, separators, anchor) def build_path(path, ast, requirements, separators, anchor)
strexp = Journey::Router::Strexp.new( strexp = Journey::Router::Strexp.new(
ast,
path, path,
requirements, requirements,
SEPARATORS, SEPARATORS,
......
...@@ -72,7 +72,7 @@ def test_map_wildcard_with_other_element ...@@ -72,7 +72,7 @@ def test_map_wildcard_with_other_element
mapper = Mapper.new fakeset mapper = Mapper.new fakeset
mapper.get '/*path/foo/:bar', :to => 'pages#show' mapper.get '/*path/foo/:bar', :to => 'pages#show'
assert_equal '/*path/foo/:bar(.:format)', fakeset.conditions.first[:path_info] assert_equal '/*path/foo/:bar(.:format)', fakeset.conditions.first[:path_info]
assert_nil fakeset.requirements.first[:path] assert_equal(/.+?/, fakeset.requirements.first[:path])
end end
def test_map_wildcard_with_multiple_wildcard def test_map_wildcard_with_multiple_wildcard
...@@ -80,7 +80,7 @@ def test_map_wildcard_with_multiple_wildcard ...@@ -80,7 +80,7 @@ def test_map_wildcard_with_multiple_wildcard
mapper = Mapper.new fakeset mapper = Mapper.new fakeset
mapper.get '/*foo/*bar', :to => 'pages#show' mapper.get '/*foo/*bar', :to => 'pages#show'
assert_equal '/*foo/*bar(.:format)', fakeset.conditions.first[:path_info] assert_equal '/*foo/*bar(.:format)', fakeset.conditions.first[:path_info]
assert_nil fakeset.requirements.first[:foo] assert_equal(/.+?/, fakeset.requirements.first[:foo])
assert_equal(/.+?/, fakeset.requirements.first[:bar]) assert_equal(/.+?/, fakeset.requirements.first[:bar])
end end
......
...@@ -99,6 +99,16 @@ def test_namespace_with_controller_segment ...@@ -99,6 +99,16 @@ def test_namespace_with_controller_segment
end end
end end
def test_namespace_without_controller_segment
draw do
namespace :admin do
get 'hello/:controllers/:action'
end
end
get '/admin/hello/foo/new'
assert_equal 'foo', @request.params["controllers"]
end
def test_session_singleton_resource def test_session_singleton_resource
draw do draw do
resource :session do resource :session do
...@@ -3137,6 +3147,18 @@ def test_absolute_controller_namespace ...@@ -3137,6 +3147,18 @@ def test_absolute_controller_namespace
assert_equal '/foo', foo_root_path assert_equal '/foo', foo_root_path
end end
def test_namespace_as_controller
draw do
namespace :foo do
get '/', to: '/bar#index', as: 'root'
end
end
get '/foo'
assert_equal 'bar#index', @response.body
assert_equal '/foo', foo_root_path
end
def test_trailing_slash def test_trailing_slash
draw do draw do
resources :streams resources :streams
...@@ -3546,6 +3568,16 @@ def test_warn_with_ruby_constant_syntax_namespaced_controller_option ...@@ -3546,6 +3568,16 @@ def test_warn_with_ruby_constant_syntax_namespaced_controller_option
assert_match "'Admin::StorageFiles' is not a supported controller name", e.message assert_match "'Admin::StorageFiles' is not a supported controller name", e.message
end end
def test_warn_with_ruby_constant_syntax_no_colons
e = assert_raise(ArgumentError) do
draw do
resources :storage_files, :controller => 'Admin'
end
end
assert_match "'Admin' is not a supported controller name", e.message
end
end end
class TestDefaultScope < ActionDispatch::IntegrationTest class TestDefaultScope < ActionDispatch::IntegrationTest
......
...@@ -18,7 +18,7 @@ class TestPattern < ActiveSupport::TestCase ...@@ -18,7 +18,7 @@ class TestPattern < ActiveSupport::TestCase
'/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar\Z}, '/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar\Z},
}.each do |path, expected| }.each do |path, expected|
define_method(:"test_to_regexp_#{path}") do define_method(:"test_to_regexp_#{path}") do
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
path, path,
{ :controller => /.+/ }, { :controller => /.+/ },
["/", ".", "?"] ["/", ".", "?"]
...@@ -41,7 +41,7 @@ class TestPattern < ActiveSupport::TestCase ...@@ -41,7 +41,7 @@ class TestPattern < ActiveSupport::TestCase
'/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar}, '/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar},
}.each do |path, expected| }.each do |path, expected|
define_method(:"test_to_non_anchored_regexp_#{path}") do define_method(:"test_to_non_anchored_regexp_#{path}") do
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
path, path,
{ :controller => /.+/ }, { :controller => /.+/ },
["/", ".", "?"], ["/", ".", "?"],
...@@ -65,7 +65,7 @@ class TestPattern < ActiveSupport::TestCase ...@@ -65,7 +65,7 @@ class TestPattern < ActiveSupport::TestCase
'/:controller/*foo/bar' => %w{ controller foo }, '/:controller/*foo/bar' => %w{ controller foo },
}.each do |path, expected| }.each do |path, expected|
define_method(:"test_names_#{path}") do define_method(:"test_names_#{path}") do
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
path, path,
{ :controller => /.+/ }, { :controller => /.+/ },
["/", ".", "?"] ["/", ".", "?"]
...@@ -75,12 +75,8 @@ class TestPattern < ActiveSupport::TestCase ...@@ -75,12 +75,8 @@ class TestPattern < ActiveSupport::TestCase
end end
end end
def test_to_raise_exception_with_bad_expression
assert_raise(ArgumentError, "Bad expression: []") { Pattern.new [] }
end
def test_to_regexp_with_extended_group def test_to_regexp_with_extended_group
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
'/page/:name', '/page/:name',
{ :name => / { :name => /
#ROFL #ROFL
...@@ -101,13 +97,13 @@ def test_optional_names ...@@ -101,13 +97,13 @@ def test_optional_names
['/:foo(/:bar)', %w{ bar }], ['/:foo(/:bar)', %w{ bar }],
['/:foo(/:bar)/:lol(/:baz)', %w{ bar baz }], ['/:foo(/:bar)/:lol(/:baz)', %w{ bar baz }],
].each do |pattern, list| ].each do |pattern, list|
path = Pattern.new pattern path = Pattern.from_string pattern
assert_equal list.sort, path.optional_names.sort assert_equal list.sort, path.optional_names.sort
end end
end end
def test_to_regexp_match_non_optional def test_to_regexp_match_non_optional
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
'/:name', '/:name',
{ :name => /\d+/ }, { :name => /\d+/ },
["/", ".", "?"] ["/", ".", "?"]
...@@ -118,7 +114,7 @@ def test_to_regexp_match_non_optional ...@@ -118,7 +114,7 @@ def test_to_regexp_match_non_optional
end end
def test_to_regexp_with_group def test_to_regexp_with_group
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
'/page/:name', '/page/:name',
{ :name => /(tender|love)/ }, { :name => /(tender|love)/ },
["/", ".", "?"] ["/", ".", "?"]
...@@ -131,7 +127,7 @@ def test_to_regexp_with_group ...@@ -131,7 +127,7 @@ def test_to_regexp_with_group
def test_ast_sets_regular_expressions def test_ast_sets_regular_expressions
requirements = { :name => /(tender|love)/, :value => /./ } requirements = { :name => /(tender|love)/, :value => /./ }
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
'/page/:name/:value', '/page/:name/:value',
requirements, requirements,
["/", ".", "?"] ["/", ".", "?"]
...@@ -148,7 +144,7 @@ def test_ast_sets_regular_expressions ...@@ -148,7 +144,7 @@ def test_ast_sets_regular_expressions
end end
def test_match_data_with_group def test_match_data_with_group
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
'/page/:name', '/page/:name',
{ :name => /(tender|love)/ }, { :name => /(tender|love)/ },
["/", ".", "?"] ["/", ".", "?"]
...@@ -160,7 +156,7 @@ def test_match_data_with_group ...@@ -160,7 +156,7 @@ def test_match_data_with_group
end end
def test_match_data_with_multi_group def test_match_data_with_multi_group
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
'/page/:name/:id', '/page/:name/:id',
{ :name => /t(((ender|love)))()/ }, { :name => /t(((ender|love)))()/ },
["/", ".", "?"] ["/", ".", "?"]
...@@ -175,7 +171,7 @@ def test_match_data_with_multi_group ...@@ -175,7 +171,7 @@ def test_match_data_with_multi_group
def test_star_with_custom_re def test_star_with_custom_re
z = /\d+/ z = /\d+/
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
'/page/*foo', '/page/*foo',
{ :foo => z }, { :foo => z },
["/", ".", "?"] ["/", ".", "?"]
...@@ -185,7 +181,7 @@ def test_star_with_custom_re ...@@ -185,7 +181,7 @@ def test_star_with_custom_re
end end
def test_insensitive_regexp_with_group def test_insensitive_regexp_with_group
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
'/page/:name/aaron', '/page/:name/aaron',
{ :name => /(tender|love)/i }, { :name => /(tender|love)/i },
["/", ".", "?"] ["/", ".", "?"]
...@@ -197,7 +193,7 @@ def test_insensitive_regexp_with_group ...@@ -197,7 +193,7 @@ def test_insensitive_regexp_with_group
end end
def test_to_regexp_with_strexp def test_to_regexp_with_strexp
strexp = Router::Strexp.new('/:controller', { }, ["/", ".", "?"]) strexp = Router::Strexp.build('/:controller', { }, ["/", ".", "?"])
path = Pattern.new strexp path = Pattern.new strexp
x = %r{\A/([^/.?]+)\Z} x = %r{\A/([^/.?]+)\Z}
...@@ -205,20 +201,20 @@ def test_to_regexp_with_strexp ...@@ -205,20 +201,20 @@ def test_to_regexp_with_strexp
end end
def test_to_regexp_defaults def test_to_regexp_defaults
path = Pattern.new '/:controller(/:action(/:id))' path = Pattern.from_string '/:controller(/:action(/:id))'
expected = %r{\A/([^/.?]+)(?:/([^/.?]+)(?:/([^/.?]+))?)?\Z} expected = %r{\A/([^/.?]+)(?:/([^/.?]+)(?:/([^/.?]+))?)?\Z}
assert_equal expected, path.to_regexp assert_equal expected, path.to_regexp
end end
def test_failed_match def test_failed_match
path = Pattern.new '/:controller(/:action(/:id(.:format)))' path = Pattern.from_string '/:controller(/:action(/:id(.:format)))'
uri = 'content' uri = 'content'
assert_not path =~ uri assert_not path =~ uri
end end
def test_match_controller def test_match_controller
path = Pattern.new '/:controller(/:action(/:id(.:format)))' path = Pattern.from_string '/:controller(/:action(/:id(.:format)))'
uri = '/content' uri = '/content'
match = path =~ uri match = path =~ uri
...@@ -230,7 +226,7 @@ def test_match_controller ...@@ -230,7 +226,7 @@ def test_match_controller
end end
def test_match_controller_action def test_match_controller_action
path = Pattern.new '/:controller(/:action(/:id(.:format)))' path = Pattern.from_string '/:controller(/:action(/:id(.:format)))'
uri = '/content/list' uri = '/content/list'
match = path =~ uri match = path =~ uri
...@@ -242,7 +238,7 @@ def test_match_controller_action ...@@ -242,7 +238,7 @@ def test_match_controller_action
end end
def test_match_controller_action_id def test_match_controller_action_id
path = Pattern.new '/:controller(/:action(/:id(.:format)))' path = Pattern.from_string '/:controller(/:action(/:id(.:format)))'
uri = '/content/list/10' uri = '/content/list/10'
match = path =~ uri match = path =~ uri
...@@ -254,7 +250,7 @@ def test_match_controller_action_id ...@@ -254,7 +250,7 @@ def test_match_controller_action_id
end end
def test_match_literal def test_match_literal
path = Path::Pattern.new "/books(/:action(.:format))" path = Path::Pattern.from_string "/books(/:action(.:format))"
uri = '/books' uri = '/books'
match = path =~ uri match = path =~ uri
...@@ -264,7 +260,7 @@ def test_match_literal ...@@ -264,7 +260,7 @@ def test_match_literal
end end
def test_match_literal_with_action def test_match_literal_with_action
path = Path::Pattern.new "/books(/:action(.:format))" path = Path::Pattern.from_string "/books(/:action(.:format))"
uri = '/books/list' uri = '/books/list'
match = path =~ uri match = path =~ uri
...@@ -274,7 +270,7 @@ def test_match_literal_with_action ...@@ -274,7 +270,7 @@ def test_match_literal_with_action
end end
def test_match_literal_with_action_and_format def test_match_literal_with_action_and_format
path = Path::Pattern.new "/books(/:action(.:format))" path = Path::Pattern.from_string "/books(/:action(.:format))"
uri = '/books/list.rss' uri = '/books/list.rss'
match = path =~ uri match = path =~ uri
......
...@@ -5,7 +5,7 @@ module Journey ...@@ -5,7 +5,7 @@ module Journey
class TestRoute < ActiveSupport::TestCase class TestRoute < ActiveSupport::TestCase
def test_initialize def test_initialize
app = Object.new app = Object.new
path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))' path = Path::Pattern.from_string '/:controller(/:action(/:id(.:format)))'
defaults = {} defaults = {}
route = Route.new("name", app, path, {}, defaults) route = Route.new("name", app, path, {}, defaults)
...@@ -16,7 +16,7 @@ def test_initialize ...@@ -16,7 +16,7 @@ def test_initialize
def test_route_adds_itself_as_memo def test_route_adds_itself_as_memo
app = Object.new app = Object.new
path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))' path = Path::Pattern.from_string '/:controller(/:action(/:id(.:format)))'
defaults = {} defaults = {}
route = Route.new("name", app, path, {}, defaults) route = Route.new("name", app, path, {}, defaults)
...@@ -26,21 +26,21 @@ def test_route_adds_itself_as_memo ...@@ -26,21 +26,21 @@ def test_route_adds_itself_as_memo
end end
def test_ip_address def test_ip_address
path = Path::Pattern.new '/messages/:id(.:format)' path = Path::Pattern.from_string '/messages/:id(.:format)'
route = Route.new("name", nil, path, {:ip => '192.168.1.1'}, route = Route.new("name", nil, path, {:ip => '192.168.1.1'},
{ :controller => 'foo', :action => 'bar' }) { :controller => 'foo', :action => 'bar' })
assert_equal '192.168.1.1', route.ip assert_equal '192.168.1.1', route.ip
end end
def test_default_ip def test_default_ip
path = Path::Pattern.new '/messages/:id(.:format)' path = Path::Pattern.from_string '/messages/:id(.:format)'
route = Route.new("name", nil, path, {}, route = Route.new("name", nil, path, {},
{ :controller => 'foo', :action => 'bar' }) { :controller => 'foo', :action => 'bar' })
assert_equal(//, route.ip) assert_equal(//, route.ip)
end end
def test_format_with_star def test_format_with_star
path = Path::Pattern.new '/:controller/*extra' path = Path::Pattern.from_string '/:controller/*extra'
route = Route.new("name", nil, path, {}, route = Route.new("name", nil, path, {},
{ :controller => 'foo', :action => 'bar' }) { :controller => 'foo', :action => 'bar' })
assert_equal '/foo/himom', route.format({ assert_equal '/foo/himom', route.format({
...@@ -50,7 +50,7 @@ def test_format_with_star ...@@ -50,7 +50,7 @@ def test_format_with_star
end end
def test_connects_all_match def test_connects_all_match
path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))' path = Path::Pattern.from_string '/:controller(/:action(/:id(.:format)))'
route = Route.new("name", nil, path, {:action => 'bar'}, { :controller => 'foo' }) route = Route.new("name", nil, path, {:action => 'bar'}, { :controller => 'foo' })
assert_equal '/foo/bar/10', route.format({ assert_equal '/foo/bar/10', route.format({
...@@ -61,21 +61,21 @@ def test_connects_all_match ...@@ -61,21 +61,21 @@ def test_connects_all_match
end end
def test_extras_are_not_included_if_optional def test_extras_are_not_included_if_optional
path = Path::Pattern.new '/page/:id(/:action)' path = Path::Pattern.from_string '/page/:id(/:action)'
route = Route.new("name", nil, path, { }, { :action => 'show' }) route = Route.new("name", nil, path, { }, { :action => 'show' })
assert_equal '/page/10', route.format({ :id => 10 }) assert_equal '/page/10', route.format({ :id => 10 })
end end
def test_extras_are_not_included_if_optional_with_parameter def test_extras_are_not_included_if_optional_with_parameter
path = Path::Pattern.new '(/sections/:section)/pages/:id' path = Path::Pattern.from_string '(/sections/:section)/pages/:id'
route = Route.new("name", nil, path, { }, { :action => 'show' }) route = Route.new("name", nil, path, { }, { :action => 'show' })
assert_equal '/pages/10', route.format({:id => 10}) assert_equal '/pages/10', route.format({:id => 10})
end end
def test_extras_are_not_included_if_optional_parameter_is_nil def test_extras_are_not_included_if_optional_parameter_is_nil
path = Path::Pattern.new '(/sections/:section)/pages/:id' path = Path::Pattern.from_string '(/sections/:section)/pages/:id'
route = Route.new("name", nil, path, { }, { :action => 'show' }) route = Route.new("name", nil, path, { }, { :action => 'show' })
assert_equal '/pages/10', route.format({:id => 10, :section => nil}) assert_equal '/pages/10', route.format({:id => 10, :section => nil})
...@@ -85,10 +85,10 @@ def test_score ...@@ -85,10 +85,10 @@ def test_score
constraints = {:required_defaults => [:controller, :action]} constraints = {:required_defaults => [:controller, :action]}
defaults = {:controller=>"pages", :action=>"show"} defaults = {:controller=>"pages", :action=>"show"}
path = Path::Pattern.new "/page/:id(/:action)(.:format)" path = Path::Pattern.from_string "/page/:id(/:action)(.:format)"
specific = Route.new "name", nil, path, constraints, defaults specific = Route.new "name", nil, path, constraints, defaults
path = Path::Pattern.new "/:controller(/:action(/:id))(.:format)" path = Path::Pattern.from_string "/:controller(/:action(/:id))(.:format)"
generic = Route.new "name", nil, path, constraints generic = Route.new "name", nil, path, constraints
knowledge = {:id=>20, :controller=>"pages", :action=>"show"} knowledge = {:id=>20, :controller=>"pages", :action=>"show"}
......
require 'abstract_unit'
module ActionDispatch
module Journey
class Router
class TestStrexp < ActiveSupport::TestCase
def test_many_names
exp = Strexp.new(
"/:controller(/:action(/:id(.:format)))",
{:controller=>/.+?/},
["/", ".", "?"],
true)
assert_equal ["controller", "action", "id", "format"], exp.names
end
def test_names
{
"/bar(.:format)" => %w{ format },
":format" => %w{ format },
":format-" => %w{ format },
":format0" => %w{ format0 },
":format1,:format2" => %w{ format1 format2 },
}.each do |string, expected|
exp = Strexp.new(string, {}, ["/", ".", "?"])
assert_equal expected, exp.names
end
end
end
end
end
end
...@@ -32,7 +32,7 @@ def ip; env['REMOTE_ADDR']; end ...@@ -32,7 +32,7 @@ def ip; env['REMOTE_ADDR']; end
def test_dashes def test_dashes
router = Router.new(routes) router = Router.new(routes)
exp = Router::Strexp.new '/foo-bar-baz', {}, ['/.?'] exp = Router::Strexp.build '/foo-bar-baz', {}, ['/.?']
path = Path::Pattern.new exp path = Path::Pattern.new exp
routes.add_route nil, path, {}, {:id => nil}, {} routes.add_route nil, path, {}, {:id => nil}, {}
...@@ -49,7 +49,7 @@ def test_unicode ...@@ -49,7 +49,7 @@ def test_unicode
router = Router.new(routes) router = Router.new(routes)
#match the escaped version of /ほげ #match the escaped version of /ほげ
exp = Router::Strexp.new '/%E3%81%BB%E3%81%92', {}, ['/.?'] exp = Router::Strexp.build '/%E3%81%BB%E3%81%92', {}, ['/.?']
path = Path::Pattern.new exp path = Path::Pattern.new exp
routes.add_route nil, path, {}, {:id => nil}, {} routes.add_route nil, path, {}, {:id => nil}, {}
...@@ -68,7 +68,7 @@ def test_request_class_and_requirements_success ...@@ -68,7 +68,7 @@ def test_request_class_and_requirements_success
requirements = { :hello => /world/ } requirements = { :hello => /world/ }
exp = Router::Strexp.new '/foo(/:id)', {}, ['/.?'] exp = Router::Strexp.build '/foo(/:id)', {}, ['/.?']
path = Path::Pattern.new exp path = Path::Pattern.new exp
routes.add_route nil, path, requirements, {:id => nil}, {} routes.add_route nil, path, requirements, {:id => nil}, {}
...@@ -88,7 +88,7 @@ def test_request_class_and_requirements_fail ...@@ -88,7 +88,7 @@ def test_request_class_and_requirements_fail
requirements = { :hello => /mom/ } requirements = { :hello => /mom/ }
exp = Router::Strexp.new '/foo(/:id)', {}, ['/.?'] exp = Router::Strexp.build '/foo(/:id)', {}, ['/.?']
path = Path::Pattern.new exp path = Path::Pattern.new exp
router.routes.add_route nil, path, requirements, {:id => nil}, {} router.routes.add_route nil, path, requirements, {:id => nil}, {}
...@@ -115,7 +115,7 @@ def path_info=(x) ...@@ -115,7 +115,7 @@ def path_info=(x)
def test_request_class_overrides_path_info def test_request_class_overrides_path_info
router = Router.new(routes) router = Router.new(routes)
exp = Router::Strexp.new '/bar', {}, ['/.?'] exp = Router::Strexp.build '/bar', {}, ['/.?']
path = Path::Pattern.new exp path = Path::Pattern.new exp
routes.add_route nil, path, {}, {}, {} routes.add_route nil, path, {}, {}, {}
...@@ -133,8 +133,8 @@ def test_request_class_overrides_path_info ...@@ -133,8 +133,8 @@ def test_request_class_overrides_path_info
def test_regexp_first_precedence def test_regexp_first_precedence
add_routes @router, [ add_routes @router, [
Router::Strexp.new("/whois/:domain", {:domain => /\w+\.[\w\.]+/}, ['/', '.', '?']), Router::Strexp.build("/whois/:domain", {:domain => /\w+\.[\w\.]+/}, ['/', '.', '?']),
Router::Strexp.new("/whois/:id(.:format)", {}, ['/', '.', '?']) Router::Strexp.build("/whois/:id(.:format)", {}, ['/', '.', '?'])
] ]
env = rails_env 'PATH_INFO' => '/whois/example.com' env = rails_env 'PATH_INFO' => '/whois/example.com'
...@@ -152,7 +152,7 @@ def test_regexp_first_precedence ...@@ -152,7 +152,7 @@ def test_regexp_first_precedence
def test_required_parts_verified_are_anchored def test_required_parts_verified_are_anchored
add_routes @router, [ add_routes @router, [
Router::Strexp.new("/foo/:id", { :id => /\d/ }, ['/', '.', '?'], false) Router::Strexp.build("/foo/:id", { :id => /\d/ }, ['/', '.', '?'], false)
] ]
assert_raises(ActionController::UrlGenerationError) do assert_raises(ActionController::UrlGenerationError) do
...@@ -162,7 +162,7 @@ def test_required_parts_verified_are_anchored ...@@ -162,7 +162,7 @@ def test_required_parts_verified_are_anchored
def test_required_parts_are_verified_when_building def test_required_parts_are_verified_when_building
add_routes @router, [ add_routes @router, [
Router::Strexp.new("/foo/:id", { :id => /\d+/ }, ['/', '.', '?'], false) Router::Strexp.build("/foo/:id", { :id => /\d+/ }, ['/', '.', '?'], false)
] ]
path, _ = @formatter.generate(nil, { :id => '10' }, { }) path, _ = @formatter.generate(nil, { :id => '10' }, { })
...@@ -175,7 +175,7 @@ def test_required_parts_are_verified_when_building ...@@ -175,7 +175,7 @@ def test_required_parts_are_verified_when_building
def test_only_required_parts_are_verified def test_only_required_parts_are_verified
add_routes @router, [ add_routes @router, [
Router::Strexp.new("/foo(/:id)", {:id => /\d/}, ['/', '.', '?'], false) Router::Strexp.build("/foo(/:id)", {:id => /\d/}, ['/', '.', '?'], false)
] ]
path, _ = @formatter.generate(nil, { :id => '10' }, { }) path, _ = @formatter.generate(nil, { :id => '10' }, { })
...@@ -190,7 +190,7 @@ def test_only_required_parts_are_verified ...@@ -190,7 +190,7 @@ def test_only_required_parts_are_verified
def test_knows_what_parts_are_missing_from_named_route def test_knows_what_parts_are_missing_from_named_route
route_name = "gorby_thunderhorse" route_name = "gorby_thunderhorse"
pattern = Router::Strexp.new("/foo/:id", { :id => /\d+/ }, ['/', '.', '?'], false) pattern = Router::Strexp.build("/foo/:id", { :id => /\d+/ }, ['/', '.', '?'], false)
path = Path::Pattern.new pattern path = Path::Pattern.new pattern
@router.routes.add_route nil, path, {}, {}, route_name @router.routes.add_route nil, path, {}, {}, route_name
...@@ -213,7 +213,7 @@ def test_clear_trailing_slash_from_script_name_on_root_unanchored_routes ...@@ -213,7 +213,7 @@ def test_clear_trailing_slash_from_script_name_on_root_unanchored_routes
route_set = Routing::RouteSet.new route_set = Routing::RouteSet.new
mapper = Routing::Mapper.new route_set mapper = Routing::Mapper.new route_set
strexp = Router::Strexp.new("/", {}, ['/', '.', '?'], false) strexp = Router::Strexp.build("/", {}, ['/', '.', '?'], false)
path = Path::Pattern.new strexp path = Path::Pattern.new strexp
app = lambda { |env| [200, {}, ['success!']] } app = lambda { |env| [200, {}, ['success!']] }
mapper.get '/weblog', :to => app mapper.get '/weblog', :to => app
...@@ -225,7 +225,7 @@ def test_clear_trailing_slash_from_script_name_on_root_unanchored_routes ...@@ -225,7 +225,7 @@ def test_clear_trailing_slash_from_script_name_on_root_unanchored_routes
end end
def test_defaults_merge_correctly def test_defaults_merge_correctly
path = Path::Pattern.new '/foo(/:id)' path = Path::Pattern.from_string '/foo(/:id)'
@router.routes.add_route nil, path, {}, {:id => nil}, {} @router.routes.add_route nil, path, {}, {:id => nil}, {}
env = rails_env 'PATH_INFO' => '/foo/10' env = rails_env 'PATH_INFO' => '/foo/10'
...@@ -241,7 +241,7 @@ def test_defaults_merge_correctly ...@@ -241,7 +241,7 @@ def test_defaults_merge_correctly
def test_recognize_with_unbound_regexp def test_recognize_with_unbound_regexp
add_routes @router, [ add_routes @router, [
Router::Strexp.new("/foo", { }, ['/', '.', '?'], false) Router::Strexp.build("/foo", { }, ['/', '.', '?'], false)
] ]
env = rails_env 'PATH_INFO' => '/foo/bar' env = rails_env 'PATH_INFO' => '/foo/bar'
...@@ -254,7 +254,7 @@ def test_recognize_with_unbound_regexp ...@@ -254,7 +254,7 @@ def test_recognize_with_unbound_regexp
def test_bound_regexp_keeps_path_info def test_bound_regexp_keeps_path_info
add_routes @router, [ add_routes @router, [
Router::Strexp.new("/foo", { }, ['/', '.', '?'], true) Router::Strexp.build("/foo", { }, ['/', '.', '?'], true)
] ]
env = rails_env 'PATH_INFO' => '/foo' env = rails_env 'PATH_INFO' => '/foo'
...@@ -308,7 +308,7 @@ def test_recall_should_be_used_when_scoring ...@@ -308,7 +308,7 @@ def test_recall_should_be_used_when_scoring
end end
def test_nil_path_parts_are_ignored def test_nil_path_parts_are_ignored
path = Path::Pattern.new "/:controller(/:action(.:format))" path = Path::Pattern.from_string "/:controller(/:action(.:format))"
@router.routes.add_route @app, path, {}, {}, {} @router.routes.add_route @app, path, {}, {}, {}
params = { :controller => "tasks", :format => nil } params = { :controller => "tasks", :format => nil }
...@@ -321,7 +321,7 @@ def test_nil_path_parts_are_ignored ...@@ -321,7 +321,7 @@ def test_nil_path_parts_are_ignored
def test_generate_slash def test_generate_slash
params = [ [:controller, "tasks"], params = [ [:controller, "tasks"],
[:action, "show"] ] [:action, "show"] ]
str = Router::Strexp.new("/", Hash[params], ['/', '.', '?'], true) str = Router::Strexp.build("/", Hash[params], ['/', '.', '?'], true)
path = Path::Pattern.new str path = Path::Pattern.new str
@router.routes.add_route @app, path, {}, {}, {} @router.routes.add_route @app, path, {}, {}, {}
...@@ -331,7 +331,7 @@ def test_generate_slash ...@@ -331,7 +331,7 @@ def test_generate_slash
end end
def test_generate_calls_param_proc def test_generate_calls_param_proc
path = Path::Pattern.new '/:controller(/:action)' path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {} @router.routes.add_route @app, path, {}, {}, {}
parameterized = [] parameterized = []
...@@ -348,7 +348,7 @@ def test_generate_calls_param_proc ...@@ -348,7 +348,7 @@ def test_generate_calls_param_proc
end end
def test_generate_id def test_generate_id
path = Path::Pattern.new '/:controller(/:action)' path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {} @router.routes.add_route @app, path, {}, {}, {}
path, params = @formatter.generate( path, params = @formatter.generate(
...@@ -358,7 +358,7 @@ def test_generate_id ...@@ -358,7 +358,7 @@ def test_generate_id
end end
def test_generate_escapes def test_generate_escapes
path = Path::Pattern.new '/:controller(/:action)' path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {} @router.routes.add_route @app, path, {}, {}, {}
path, _ = @formatter.generate(nil, path, _ = @formatter.generate(nil,
...@@ -369,7 +369,7 @@ def test_generate_escapes ...@@ -369,7 +369,7 @@ def test_generate_escapes
end end
def test_generate_escapes_with_namespaced_controller def test_generate_escapes_with_namespaced_controller
path = Path::Pattern.new '/:controller(/:action)' path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {} @router.routes.add_route @app, path, {}, {}, {}
path, _ = @formatter.generate( path, _ = @formatter.generate(
...@@ -380,7 +380,7 @@ def test_generate_escapes_with_namespaced_controller ...@@ -380,7 +380,7 @@ def test_generate_escapes_with_namespaced_controller
end end
def test_generate_extra_params def test_generate_extra_params
path = Path::Pattern.new '/:controller(/:action)' path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {} @router.routes.add_route @app, path, {}, {}, {}
path, params = @formatter.generate( path, params = @formatter.generate(
...@@ -394,7 +394,7 @@ def test_generate_extra_params ...@@ -394,7 +394,7 @@ def test_generate_extra_params
end end
def test_generate_uses_recall_if_needed def test_generate_uses_recall_if_needed
path = Path::Pattern.new '/:controller(/:action(/:id))' path = Path::Pattern.from_string '/:controller(/:action(/:id))'
@router.routes.add_route @app, path, {}, {}, {} @router.routes.add_route @app, path, {}, {}, {}
path, params = @formatter.generate( path, params = @formatter.generate(
...@@ -406,7 +406,7 @@ def test_generate_uses_recall_if_needed ...@@ -406,7 +406,7 @@ def test_generate_uses_recall_if_needed
end end
def test_generate_with_name def test_generate_with_name
path = Path::Pattern.new '/:controller(/:action)' path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {} @router.routes.add_route @app, path, {}, {}, {}
path, params = @formatter.generate( path, params = @formatter.generate(
...@@ -423,7 +423,7 @@ def test_generate_with_name ...@@ -423,7 +423,7 @@ def test_generate_with_name
'/content/show/10' => { :controller => 'content', :action => 'show', :id => "10" }, '/content/show/10' => { :controller => 'content', :action => 'show', :id => "10" },
}.each do |request_path, expected| }.each do |request_path, expected|
define_method("test_recognize_#{expected.keys.map(&:to_s).join('_')}") do define_method("test_recognize_#{expected.keys.map(&:to_s).join('_')}") do
path = Path::Pattern.new "/:controller(/:action(/:id))" path = Path::Pattern.from_string "/:controller(/:action(/:id))"
app = Object.new app = Object.new
route = @router.routes.add_route(app, path, {}, {}, {}) route = @router.routes.add_route(app, path, {}, {}, {})
...@@ -445,7 +445,7 @@ def test_generate_with_name ...@@ -445,7 +445,7 @@ def test_generate_with_name
:splat => ['/segment/a/b%20c+d', { :segment => 'segment', :splat => 'a/b c+d' }] :splat => ['/segment/a/b%20c+d', { :segment => 'segment', :splat => 'a/b c+d' }]
}.each do |name, (request_path, expected)| }.each do |name, (request_path, expected)|
define_method("test_recognize_#{name}") do define_method("test_recognize_#{name}") do
path = Path::Pattern.new '/:segment/*splat' path = Path::Pattern.from_string '/:segment/*splat'
app = Object.new app = Object.new
route = @router.routes.add_route(app, path, {}, {}, {}) route = @router.routes.add_route(app, path, {}, {}, {})
...@@ -463,7 +463,7 @@ def test_generate_with_name ...@@ -463,7 +463,7 @@ def test_generate_with_name
end end
def test_namespaced_controller def test_namespaced_controller
strexp = Router::Strexp.new( strexp = Router::Strexp.build(
"/:controller(/:action(/:id))", "/:controller(/:action(/:id))",
{ :controller => /.+?/ }, { :controller => /.+?/ },
["/", ".", "?"] ["/", ".", "?"]
...@@ -489,7 +489,7 @@ def test_namespaced_controller ...@@ -489,7 +489,7 @@ def test_namespaced_controller
end end
def test_recognize_literal def test_recognize_literal
path = Path::Pattern.new "/books(/:action(.:format))" path = Path::Pattern.from_string "/books(/:action(.:format))"
app = Object.new app = Object.new
route = @router.routes.add_route(app, path, {}, {:controller => 'books'}) route = @router.routes.add_route(app, path, {}, {:controller => 'books'})
...@@ -506,7 +506,7 @@ def test_recognize_literal ...@@ -506,7 +506,7 @@ def test_recognize_literal
end end
def test_recognize_head_request_as_get_route def test_recognize_head_request_as_get_route
path = Path::Pattern.new "/books(/:action(.:format))" path = Path::Pattern.from_string "/books(/:action(.:format))"
app = Object.new app = Object.new
conditions = { conditions = {
:request_method => 'GET' :request_method => 'GET'
...@@ -525,7 +525,7 @@ def test_recognize_head_request_as_get_route ...@@ -525,7 +525,7 @@ def test_recognize_head_request_as_get_route
end end
def test_recognize_cares_about_verbs def test_recognize_cares_about_verbs
path = Path::Pattern.new "/books(/:action(.:format))" path = Path::Pattern.from_string "/books(/:action(.:format))"
app = Object.new app = Object.new
conditions = { conditions = {
:request_method => 'GET' :request_method => 'GET'
...@@ -553,7 +553,11 @@ def test_recognize_cares_about_verbs ...@@ -553,7 +553,11 @@ def test_recognize_cares_about_verbs
def add_routes router, paths def add_routes router, paths
paths.each do |path| paths.each do |path|
path = Path::Pattern.new path if String === path
path = Path::Pattern.from_string path
else
path = Path::Pattern.new path
end
router.routes.add_route @app, path, {}, {}, {} router.routes.add_route @app, path, {}, {}, {}
end end
end end
......
...@@ -5,7 +5,7 @@ module Journey ...@@ -5,7 +5,7 @@ module Journey
class TestRoutes < ActiveSupport::TestCase class TestRoutes < ActiveSupport::TestCase
def test_clear def test_clear
routes = Routes.new routes = Routes.new
exp = Router::Strexp.new '/foo(/:id)', {}, ['/.?'] exp = Router::Strexp.build '/foo(/:id)', {}, ['/.?']
path = Path::Pattern.new exp path = Path::Pattern.new exp
requirements = { :hello => /world/ } requirements = { :hello => /world/ }
...@@ -18,7 +18,7 @@ def test_clear ...@@ -18,7 +18,7 @@ def test_clear
def test_ast def test_ast
routes = Routes.new routes = Routes.new
path = Path::Pattern.new '/hello' path = Path::Pattern.from_string '/hello'
routes.add_route nil, path, {}, {}, {} routes.add_route nil, path, {}, {}, {}
ast = routes.ast ast = routes.ast
...@@ -28,7 +28,7 @@ def test_ast ...@@ -28,7 +28,7 @@ def test_ast
def test_simulator_changes def test_simulator_changes
routes = Routes.new routes = Routes.new
path = Path::Pattern.new '/hello' path = Path::Pattern.from_string '/hello'
routes.add_route nil, path, {}, {}, {} routes.add_route nil, path, {}, {}, {}
sim = routes.simulator sim = routes.simulator
...@@ -40,8 +40,8 @@ def test_first_name_wins ...@@ -40,8 +40,8 @@ def test_first_name_wins
#def add_route app, path, conditions, defaults, name = nil #def add_route app, path, conditions, defaults, name = nil
routes = Routes.new routes = Routes.new
one = Path::Pattern.new '/hello' one = Path::Pattern.from_string '/hello'
two = Path::Pattern.new '/aaron' two = Path::Pattern.from_string '/aaron'
routes.add_route nil, one, {}, {}, 'aaron' routes.add_route nil, one, {}, {}, 'aaron'
routes.add_route nil, two, {}, {}, 'aaron' routes.add_route nil, two, {}, {}, 'aaron'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册