2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'controller/fake_controllers'
|
2010-08-05 17:34:39 -04:00
|
|
|
require 'active_support/core_ext/object/with_options'
|
2013-11-05 23:05:58 -05:00
|
|
|
require 'active_support/core_ext/object/json'
|
2005-02-14 20:45:35 -05:00
|
|
|
|
2007-12-14 21:29:04 -05:00
|
|
|
class MilestonesController < ActionController::Base
|
|
|
|
def index() head :ok end
|
|
|
|
alias_method :show, :index
|
|
|
|
end
|
|
|
|
|
2007-05-14 07:14:30 -04:00
|
|
|
# See RFC 3986, section 3.3 for allowed path characters.
|
2012-01-05 20:01:45 -05:00
|
|
|
class UriReservedCharactersRoutingTest < ActiveSupport::TestCase
|
2010-11-30 10:36:01 -05:00
|
|
|
include RoutingTestHelpers
|
|
|
|
|
2007-02-21 05:05:07 -05:00
|
|
|
def setup
|
2010-09-24 20:15:52 -04:00
|
|
|
@set = ActionDispatch::Routing::RouteSet.new
|
2010-08-05 09:44:23 -04:00
|
|
|
@set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:variable/*additional'
|
2007-02-21 05:05:07 -05:00
|
|
|
end
|
2007-05-14 07:14:30 -04:00
|
|
|
|
2009-10-19 23:41:58 -04:00
|
|
|
safe, unsafe = %w(: @ & = + $ , ;), %w(^ ? # [ ])
|
2007-05-14 07:14:30 -04:00
|
|
|
hex = unsafe.map { |char| '%' + char.unpack('H2').first.upcase }
|
|
|
|
|
2008-01-07 03:12:34 -05:00
|
|
|
@segment = "#{safe.join}#{unsafe.join}".freeze
|
|
|
|
@escaped = "#{safe.join}#{hex.join}".freeze
|
2007-02-21 05:05:07 -05:00
|
|
|
end
|
2007-05-14 07:14:30 -04:00
|
|
|
|
|
|
|
def test_route_generation_escapes_unsafe_path_characters
|
2009-10-18 01:26:36 -04:00
|
|
|
assert_equal "/content/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2",
|
2010-11-30 10:36:01 -05:00
|
|
|
url_for(@set, {
|
|
|
|
:controller => "content",
|
|
|
|
:action => "act#{@segment}ion",
|
|
|
|
:variable => "var#{@segment}iable",
|
|
|
|
:additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"]
|
|
|
|
})
|
2007-02-21 05:05:07 -05:00
|
|
|
end
|
2007-05-14 07:14:30 -04:00
|
|
|
|
|
|
|
def test_route_recognition_unescapes_path_components
|
2009-08-26 00:34:48 -04:00
|
|
|
options = { :controller => "content",
|
2007-05-14 07:14:30 -04:00
|
|
|
:action => "act#{@segment}ion",
|
2008-05-09 22:55:11 -04:00
|
|
|
:variable => "var#{@segment}iable",
|
2010-08-06 04:27:41 -04:00
|
|
|
:additional => "add#{@segment}itional-1/add#{@segment}itional-2" }
|
2009-08-26 00:34:48 -04:00
|
|
|
assert_equal options, @set.recognize_path("/content/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2")
|
2007-02-21 05:05:07 -05:00
|
|
|
end
|
2008-05-13 16:04:20 -04:00
|
|
|
|
|
|
|
def test_route_generation_allows_passing_non_string_values_to_generated_helper
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal "/content/action/variable/1/2",
|
|
|
|
url_for(@set, {
|
|
|
|
:controller => "content",
|
|
|
|
:action => "action",
|
|
|
|
:variable => "variable",
|
|
|
|
:additional => [1, 2]
|
|
|
|
})
|
2008-05-13 16:04:20 -04:00
|
|
|
end
|
2007-02-21 05:05:07 -05:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
class MockController
|
2013-01-02 15:11:16 -05:00
|
|
|
def self.build(helpers, additional_options = {})
|
2010-02-26 21:25:56 -05:00
|
|
|
Class.new do
|
2013-01-02 15:11:16 -05:00
|
|
|
define_method :url_options do
|
|
|
|
options = super()
|
2010-02-26 21:25:56 -05:00
|
|
|
options[:protocol] ||= "http"
|
|
|
|
options[:host] ||= "test.host"
|
2013-01-02 15:11:16 -05:00
|
|
|
options.merge(additional_options)
|
2010-02-26 21:25:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
include helpers
|
|
|
|
end
|
2006-06-01 11:42:08 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-08-05 18:12:50 -04:00
|
|
|
|
2012-01-05 20:01:45 -05:00
|
|
|
class LegacyRouteSetTests < ActiveSupport::TestCase
|
2010-11-30 10:36:01 -05:00
|
|
|
include RoutingTestHelpers
|
2012-01-23 13:18:36 -05:00
|
|
|
include ActionDispatch::RoutingVerbs
|
2010-11-30 10:36:01 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
attr_reader :rs
|
2014-07-02 18:02:36 -04:00
|
|
|
attr_accessor :controller
|
2012-01-23 13:18:36 -05:00
|
|
|
alias :routes :rs
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def setup
|
2014-07-02 18:02:36 -04:00
|
|
|
@rs = make_set
|
2011-12-22 14:53:14 -05:00
|
|
|
@response = nil
|
|
|
|
end
|
|
|
|
|
2012-01-23 20:14:30 -05:00
|
|
|
def test_symbols_with_dashes
|
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/:artist/:song-omg', :to => lambda { |env|
|
2014-05-27 17:46:04 -04:00
|
|
|
resp = ActiveSupport::JSON.encode ActionDispatch::Request.new(env).path_parameters
|
2012-01-23 20:14:30 -05:00
|
|
|
[200, {}, [resp]]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-11-05 23:05:58 -05:00
|
|
|
hash = ActiveSupport::JSON.decode get(URI('http://example.org/journey/faithfully-omg'))
|
2012-01-23 20:14:30 -05:00
|
|
|
assert_equal({"artist"=>"journey", "song"=>"faithfully"}, hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_id_with_dash
|
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/journey/:id', :to => lambda { |env|
|
2014-05-27 17:46:04 -04:00
|
|
|
resp = ActiveSupport::JSON.encode ActionDispatch::Request.new(env).path_parameters
|
2012-01-23 20:14:30 -05:00
|
|
|
[200, {}, [resp]]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-11-05 23:05:58 -05:00
|
|
|
hash = ActiveSupport::JSON.decode get(URI('http://example.org/journey/faithfully-omg'))
|
2012-01-23 20:14:30 -05:00
|
|
|
assert_equal({"id"=>"faithfully-omg"}, hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dash_with_custom_regexp
|
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/:artist/:song-omg', :constraints => { :song => /\d+/ }, :to => lambda { |env|
|
2014-05-27 17:46:04 -04:00
|
|
|
resp = ActiveSupport::JSON.encode ActionDispatch::Request.new(env).path_parameters
|
2012-01-23 20:14:30 -05:00
|
|
|
[200, {}, [resp]]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-11-05 23:05:58 -05:00
|
|
|
hash = ActiveSupport::JSON.decode get(URI('http://example.org/journey/123-omg'))
|
2012-01-23 20:14:30 -05:00
|
|
|
assert_equal({"artist"=>"journey", "song"=>"123"}, hash)
|
|
|
|
assert_equal 'Not Found', get(URI('http://example.org/journey/faithfully-omg'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pre_dash
|
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/:artist/omg-:song', :to => lambda { |env|
|
2014-05-27 17:46:04 -04:00
|
|
|
resp = ActiveSupport::JSON.encode ActionDispatch::Request.new(env).path_parameters
|
2012-01-23 20:14:30 -05:00
|
|
|
[200, {}, [resp]]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-11-05 23:05:58 -05:00
|
|
|
hash = ActiveSupport::JSON.decode get(URI('http://example.org/journey/omg-faithfully'))
|
2012-01-23 20:14:30 -05:00
|
|
|
assert_equal({"artist"=>"journey", "song"=>"faithfully"}, hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pre_dash_with_custom_regexp
|
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/:artist/omg-:song', :constraints => { :song => /\d+/ }, :to => lambda { |env|
|
2014-05-27 17:46:04 -04:00
|
|
|
resp = ActiveSupport::JSON.encode ActionDispatch::Request.new(env).path_parameters
|
2012-01-23 20:14:30 -05:00
|
|
|
[200, {}, [resp]]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-11-05 23:05:58 -05:00
|
|
|
hash = ActiveSupport::JSON.decode get(URI('http://example.org/journey/omg-123'))
|
2012-01-23 20:14:30 -05:00
|
|
|
assert_equal({"artist"=>"journey", "song"=>"123"}, hash)
|
|
|
|
assert_equal 'Not Found', get(URI('http://example.org/journey/omg-faithfully'))
|
|
|
|
end
|
|
|
|
|
2012-02-14 19:51:22 -05:00
|
|
|
def test_star_paths_are_greedy
|
2012-02-25 19:33:15 -05:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "/*path", :to => lambda { |env|
|
2012-02-25 19:33:15 -05:00
|
|
|
x = env["action_dispatch.request.path_parameters"][:path]
|
|
|
|
[200, {}, [x]]
|
|
|
|
}, :format => false
|
|
|
|
end
|
|
|
|
|
|
|
|
u = URI('http://example.org/foo/bar.html')
|
|
|
|
assert_equal u.path.sub(/^\//, ''), get(u)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_star_paths_are_greedy_but_not_too_much
|
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "/*path", :to => lambda { |env|
|
2013-11-05 23:05:58 -05:00
|
|
|
x = ActiveSupport::JSON.encode env["action_dispatch.request.path_parameters"]
|
2012-02-25 19:33:15 -05:00
|
|
|
[200, {}, [x]]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
expected = { "path" => "foo/bar", "format" => "html" }
|
|
|
|
u = URI('http://example.org/foo/bar.html')
|
2013-11-05 23:05:58 -05:00
|
|
|
assert_equal expected, ActiveSupport::JSON.decode(get(u))
|
2012-02-25 19:33:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_optional_star_paths_are_greedy
|
2012-02-14 19:51:22 -05:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "/(*filters)", :to => lambda { |env|
|
2012-02-14 19:51:22 -05:00
|
|
|
x = env["action_dispatch.request.path_parameters"][:filters]
|
|
|
|
[200, {}, [x]]
|
|
|
|
}, :format => false
|
|
|
|
end
|
|
|
|
|
|
|
|
u = URI('http://example.org/ne_27.065938,-80.6092/sw_25.489856,-82.542794')
|
|
|
|
assert_equal u.path.sub(/^\//, ''), get(u)
|
|
|
|
end
|
|
|
|
|
2012-02-25 19:33:15 -05:00
|
|
|
def test_optional_star_paths_are_greedy_but_not_too_much
|
2012-02-14 19:51:22 -05:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "/(*filters)", :to => lambda { |env|
|
2013-11-05 23:05:58 -05:00
|
|
|
x = ActiveSupport::JSON.encode env["action_dispatch.request.path_parameters"]
|
2012-02-14 19:51:22 -05:00
|
|
|
[200, {}, [x]]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
expected = { "filters" => "ne_27.065938,-80.6092/sw_25.489856,-82",
|
|
|
|
"format" => "542794" }
|
|
|
|
u = URI('http://example.org/ne_27.065938,-80.6092/sw_25.489856,-82.542794')
|
2013-11-05 23:05:58 -05:00
|
|
|
assert_equal expected, ActiveSupport::JSON.decode(get(u))
|
2012-02-14 19:51:22 -05:00
|
|
|
end
|
|
|
|
|
2011-12-21 13:45:41 -05:00
|
|
|
def test_regexp_precidence
|
2012-08-20 08:47:00 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/whois/:domain', :constraints => {
|
2011-12-21 13:45:41 -05:00
|
|
|
:domain => /\w+\.[\w\.]+/ },
|
2011-12-22 15:09:43 -05:00
|
|
|
:to => lambda { |env| [200, {}, %w{regexp}] }
|
2011-12-21 13:45:41 -05:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/whois/:id', :to => lambda { |env| [200, {}, %w{id}] }
|
2011-12-21 13:45:41 -05:00
|
|
|
end
|
|
|
|
|
2011-12-22 14:53:14 -05:00
|
|
|
assert_equal 'regexp', get(URI('http://example.org/whois/example.org'))
|
|
|
|
assert_equal 'id', get(URI('http://example.org/whois/123'))
|
2011-12-21 13:45:41 -05:00
|
|
|
end
|
|
|
|
|
2011-12-19 16:34:54 -05:00
|
|
|
def test_class_and_lambda_constraints
|
|
|
|
subdomain = Class.new {
|
|
|
|
def matches? request
|
|
|
|
request.subdomain.present? and request.subdomain != 'clients'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2012-08-20 08:47:00 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/', :constraints => subdomain.new,
|
2011-12-22 15:09:43 -05:00
|
|
|
:to => lambda { |env| [200, {}, %w{default}] }
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/', :constraints => { :subdomain => 'clients' },
|
2011-12-22 15:09:43 -05:00
|
|
|
:to => lambda { |env| [200, {}, %w{clients}] }
|
2011-12-19 16:34:54 -05:00
|
|
|
end
|
|
|
|
|
2011-12-22 14:53:14 -05:00
|
|
|
assert_equal 'default', get(URI('http://www.example.org/'))
|
|
|
|
assert_equal 'clients', get(URI('http://clients.example.org/'))
|
2011-12-19 16:34:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_lambda_constraints
|
2012-08-20 08:47:00 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/', :constraints => lambda { |req|
|
2011-12-19 16:34:54 -05:00
|
|
|
req.subdomain.present? and req.subdomain != "clients" },
|
2011-12-22 15:09:43 -05:00
|
|
|
:to => lambda { |env| [200, {}, %w{default}] }
|
2011-12-19 16:34:54 -05:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/', :constraints => lambda { |req|
|
2011-12-19 16:34:54 -05:00
|
|
|
req.subdomain.present? && req.subdomain == "clients" },
|
2011-12-22 15:09:43 -05:00
|
|
|
:to => lambda { |env| [200, {}, %w{clients}] }
|
2011-12-19 16:34:54 -05:00
|
|
|
end
|
|
|
|
|
2011-12-22 14:53:14 -05:00
|
|
|
assert_equal 'default', get(URI('http://www.example.org/'))
|
|
|
|
assert_equal 'clients', get(URI('http://clients.example.org/'))
|
2011-12-19 16:34:54 -05:00
|
|
|
end
|
2014-05-30 18:54:56 -04:00
|
|
|
|
|
|
|
def test_scoped_lambda
|
|
|
|
scope_called = false
|
|
|
|
rs.draw do
|
|
|
|
scope '/foo', :constraints => lambda { |req| scope_called = true } do
|
|
|
|
get '/', :to => lambda { |env| [200, {}, %w{default}] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'default', get(URI('http://www.example.org/foo/'))
|
|
|
|
assert scope_called, "scope constraint should be called"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_scoped_lambda_with_get_lambda
|
|
|
|
inner_called = false
|
|
|
|
|
|
|
|
rs.draw do
|
|
|
|
scope '/foo', :constraints => lambda { |req| flunk "should not be called" } do
|
|
|
|
get '/', :constraints => lambda { |req| inner_called = true },
|
|
|
|
:to => lambda { |env| [200, {}, %w{default}] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'default', get(URI('http://www.example.org/foo/'))
|
|
|
|
assert inner_called, "inner constraint should be called"
|
|
|
|
end
|
2011-12-19 16:34:54 -05:00
|
|
|
|
2011-12-22 20:04:22 -05:00
|
|
|
def test_empty_string_match
|
|
|
|
rs.draw do
|
|
|
|
get '/:username', :constraints => { :username => /[^\/]+/ },
|
|
|
|
:to => lambda { |e| [200, {}, ['foo']] }
|
|
|
|
end
|
|
|
|
assert_equal 'Not Found', get(URI('http://example.org/'))
|
|
|
|
assert_equal 'foo', get(URI('http://example.org/hello'))
|
|
|
|
end
|
|
|
|
|
2011-12-22 20:52:42 -05:00
|
|
|
def test_non_greedy_glob_regexp
|
|
|
|
params = nil
|
|
|
|
rs.draw do
|
|
|
|
get '/posts/:id(/*filters)', :constraints => { :filters => /.+?/ },
|
|
|
|
:to => lambda { |e|
|
|
|
|
params = e["action_dispatch.request.path_parameters"]
|
|
|
|
[200, {}, ['foo']]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
assert_equal 'foo', get(URI('http://example.org/posts/1/foo.js'))
|
|
|
|
assert_equal({:id=>"1", :filters=>"foo", :format=>"js"}, params)
|
|
|
|
end
|
|
|
|
|
2011-05-03 10:07:25 -04:00
|
|
|
def test_draw_with_block_arity_one_raises
|
|
|
|
assert_raise(RuntimeError) do
|
2012-08-20 08:47:00 -04:00
|
|
|
rs.draw { |map| map.match '/:controller(/:action(/:id))' }
|
2011-05-03 10:07:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-14 13:55:30 -04:00
|
|
|
def test_specific_controller_action_failure
|
2012-08-20 08:47:00 -04:00
|
|
|
rs.draw do
|
2012-06-14 13:55:30 -04:00
|
|
|
mount lambda {} => "/foo"
|
|
|
|
end
|
|
|
|
|
2012-08-01 16:33:15 -04:00
|
|
|
assert_raises(ActionController::UrlGenerationError) do
|
2012-08-20 08:47:00 -04:00
|
|
|
url_for(rs, :controller => "omg", :action => "lol")
|
2012-06-14 13:55:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_default_setup
|
2012-08-20 08:47:00 -04:00
|
|
|
rs.draw { get '/:controller(/:action(/:id))' }
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
|
|
|
|
|
|
|
|
assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/admin/user/show/10', url_for(rs, { :controller => 'admin/user', :action => 'show', :id => 10 })
|
2009-02-03 21:25:37 -05:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/admin/user/list/10')
|
2009-02-03 21:25:37 -05:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
assert_equal({ :controller => 'admin/user', :action => 'list', :id => '10' },
|
|
|
|
controller.request.path_parameters)
|
|
|
|
|
|
|
|
assert_equal '/admin/user/show', controller.url_for({ :action => 'show', :only_path => true })
|
|
|
|
assert_equal '/admin/user/list/10', controller.url_for({:only_path => true})
|
|
|
|
|
|
|
|
assert_equal '/admin/stuff', controller.url_for({ :controller => 'stuff', :only_path => true })
|
|
|
|
assert_equal '/stuff', controller.url_for({ :controller => '/stuff', :only_path => true })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ignores_leading_slash
|
2012-08-20 08:47:00 -04:00
|
|
|
rs.clear!
|
|
|
|
rs.draw { get '/:controller(/:action(/:id))'}
|
2009-02-03 21:25:37 -05:00
|
|
|
test_default_setup
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_with_colon_first
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/:controller/:action/:id', :action => 'index', :id => nil
|
|
|
|
get ':url', :controller => 'tiny_url', :action => 'translate'
|
2006-09-04 16:09:15 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_with_regexp_for_controller
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:admintoken(/:action(/:id))', :controller => /admin\/.+/
|
|
|
|
get '/:controller(/:action(/:id))'
|
2008-11-26 09:57:36 -05:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
|
|
|
|
rs.recognize_path("/admin/user/foo"))
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal({:controller => "content", :action => "foo"},
|
|
|
|
rs.recognize_path("/content/foo"))
|
|
|
|
|
|
|
|
assert_equal '/admin/user/foo', url_for(rs, { :controller => "admin/user", :admintoken => "foo", :action => "index" })
|
|
|
|
assert_equal '/content/foo', url_for(rs, { :controller => "content", :action => "foo" })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-09-04 16:09:15 -04:00
|
|
|
|
2009-02-07 09:44:50 -05:00
|
|
|
def test_route_with_regexp_and_captures_for_controller
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/:controller(/:action(/:id))', :controller => /admin\/(accounts|users)/
|
2009-02-07 09:44:50 -05:00
|
|
|
end
|
|
|
|
assert_equal({:controller => "admin/accounts", :action => "index"}, rs.recognize_path("/admin/accounts"))
|
|
|
|
assert_equal({:controller => "admin/users", :action => "index"}, rs.recognize_path("/admin/users"))
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActionController::RoutingError) { rs.recognize_path("/admin/products") }
|
2009-02-07 09:44:50 -05:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_route_with_regexp_and_dot
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:file',
|
2010-08-05 09:44:23 -04:00
|
|
|
:controller => /admin|user/,
|
|
|
|
:action => /upload|download/,
|
|
|
|
:defaults => {:file => nil},
|
|
|
|
:constraints => {:file => %r{[^/]+(\.[^/]+)?}}
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
# Without a file extension
|
|
|
|
assert_equal '/user/download/file',
|
2010-11-30 10:36:01 -05:00
|
|
|
url_for(rs, { :controller => "user", :action => "download", :file => "file" })
|
|
|
|
|
|
|
|
assert_equal({:controller => "user", :action => "download", :file => "file"},
|
2009-02-03 21:25:37 -05:00
|
|
|
rs.recognize_path("/user/download/file"))
|
2006-08-05 18:12:50 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
# Now, let's try a file with an extension, really a dot (.)
|
|
|
|
assert_equal '/user/download/file.jpg',
|
2010-11-30 10:36:01 -05:00
|
|
|
url_for(rs, { :controller => "user", :action => "download", :file => "file.jpg" })
|
|
|
|
|
|
|
|
assert_equal({:controller => "user", :action => "download", :file => "file.jpg"},
|
2009-02-03 21:25:37 -05:00
|
|
|
rs.recognize_path("/user/download/file.jpg"))
|
|
|
|
end
|
2006-08-05 18:12:50 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_basic_named_route
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2010-08-06 04:27:41 -04:00
|
|
|
root :to => 'content#list', :as => 'home'
|
2009-09-07 00:21:04 -04:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal("http://test.host/", setup_for_named_route.send(:home_url))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_with_option
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:title' => 'content#show_page', :as => 'page'
|
2009-09-07 00:21:04 -04:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal("http://test.host/page/new%20stuff",
|
2010-11-30 10:36:01 -05:00
|
|
|
setup_for_named_route.send(:page_url, :title => 'new stuff'))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-06-01 11:42:08 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_with_default
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:title' => 'content#show_page', :title => 'AboutPage', :as => 'page'
|
2009-09-07 00:21:04 -04:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal("http://test.host/page/AboutRails",
|
|
|
|
setup_for_named_route.send(:page_url, :title => "AboutRails"))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_with_path_prefix
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2010-08-05 17:34:39 -04:00
|
|
|
scope "my" do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page' => 'content#show_page', :as => 'page'
|
2010-08-05 17:34:39 -04:00
|
|
|
end
|
2009-09-07 00:21:04 -04:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal("http://test.host/my/page",
|
2010-11-30 10:36:01 -05:00
|
|
|
setup_for_named_route.send(:page_url))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-03-09 11:27:13 -04:00
|
|
|
def test_named_route_with_blank_path_prefix
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2010-08-05 17:34:39 -04:00
|
|
|
scope "" do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page' => 'content#show_page', :as => 'page'
|
2010-08-05 17:34:39 -04:00
|
|
|
end
|
2009-09-07 00:21:04 -04:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
|
2009-03-09 11:27:13 -04:00
|
|
|
assert_equal("http://test.host/page",
|
2010-11-30 10:36:01 -05:00
|
|
|
setup_for_named_route.send(:page_url))
|
2009-03-09 11:27:13 -04:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_with_nested_controller
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'admin/user' => 'admin/user#index', :as => "users"
|
2009-09-07 00:21:04 -04:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal("http://test.host/admin/user",
|
2010-11-30 10:36:01 -05:00
|
|
|
setup_for_named_route.send(:users_url))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_optimised_named_route_with_host
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page' => 'content#show_page', :as => 'pages', :host => 'foo.com'
|
2009-09-07 00:21:04 -04:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
routes = setup_for_named_route
|
2014-05-09 15:05:24 -04:00
|
|
|
assert_equal "http://foo.com/page", routes.pages_url
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2013-01-02 15:11:16 -05:00
|
|
|
def setup_for_named_route(options = {})
|
|
|
|
MockController.build(rs.url_helpers, options).new
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_without_hash
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:id', :as => 'normal'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_root
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
|
|
|
root :to => "hello#index"
|
2006-08-05 18:12:50 -04:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
routes = setup_for_named_route
|
|
|
|
assert_equal("http://test.host/", routes.send(:root_url))
|
|
|
|
assert_equal("/", routes.send(:root_path))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-03-28 16:01:21 -04:00
|
|
|
|
2012-03-03 22:17:34 -05:00
|
|
|
def test_named_route_root_without_hash
|
|
|
|
rs.draw do
|
|
|
|
root "hello#index"
|
|
|
|
end
|
|
|
|
routes = setup_for_named_route
|
|
|
|
assert_equal("http://test.host/", routes.send(:root_url))
|
|
|
|
assert_equal("/", routes.send(:root_path))
|
|
|
|
end
|
|
|
|
|
2013-02-26 11:50:48 -05:00
|
|
|
def test_named_route_root_with_hash
|
|
|
|
rs.draw do
|
|
|
|
root "hello#index", as: :index
|
|
|
|
end
|
|
|
|
|
|
|
|
routes = setup_for_named_route
|
|
|
|
assert_equal("http://test.host/", routes.send(:index_url))
|
|
|
|
assert_equal("/", routes.send(:index_path))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_root_without_path_raises_argument_error
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
rs.draw { root nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-02 15:11:16 -05:00
|
|
|
def test_named_route_root_with_trailing_slash
|
|
|
|
rs.draw do
|
|
|
|
root "hello#index"
|
|
|
|
end
|
|
|
|
|
|
|
|
routes = setup_for_named_route(trailing_slash: true)
|
|
|
|
assert_equal("http://test.host/", routes.send(:root_url))
|
|
|
|
assert_equal("http://test.host/?foo=bar", routes.send(:root_url, foo: :bar))
|
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_with_regexps
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:year/:month/:day/:title' => 'page#show', :as => 'article',
|
2009-02-03 21:25:37 -05:00
|
|
|
:year => /\d+/, :month => /\d+/, :day => /\d+/
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:id'
|
2006-06-05 11:30:09 -04:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
|
|
|
|
routes = setup_for_named_route
|
|
|
|
|
|
|
|
assert_equal "http://test.host/page/2005/6/10/hi",
|
|
|
|
routes.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-08-05 18:12:50 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_changing_controller
|
2012-08-20 08:47:00 -04:00
|
|
|
rs.draw { get ':controller/:action/:id' }
|
2006-06-01 11:42:08 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/admin/user/index/10')
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/admin/stuff/show/10',
|
2014-07-02 18:02:36 -04:00
|
|
|
controller.url_for({:controller => 'stuff', :action => 'show', :id => 10, :only_path => true})
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-09-02 16:41:40 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_paths_escaped
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'file/*path' => 'content#show_file', :as => 'path'
|
|
|
|
get ':controller/:action/:id'
|
2007-05-26 16:07:34 -04:00
|
|
|
end
|
2006-06-01 11:42:08 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
# No + to space in URI escaping, only for query params.
|
|
|
|
results = rs.recognize_path "/file/hello+world/how+are+you%3F"
|
|
|
|
assert results, "Recognition should have succeeded"
|
2010-08-06 04:27:41 -04:00
|
|
|
assert_equal 'hello+world/how+are+you?', results[:path]
|
2008-10-08 04:31:00 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
# Use %20 for space instead.
|
|
|
|
results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F"
|
|
|
|
assert results, "Recognition should have succeeded"
|
2010-08-06 04:27:41 -04:00
|
|
|
assert_equal 'hello world/how are you?', results[:path]
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-06-01 11:42:08 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_paths_slashes_unescaped_with_ordered_parameters
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/file/*path' => 'content#index', :as => 'path'
|
2009-09-07 00:21:04 -04:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
# No / to %2F in URI, only for query params.
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal("/file/hello/world", setup_for_named_route.send(:path_path, ['hello', 'world']))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_non_controllers_cannot_be_matched
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:id'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") }
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
def test_should_list_options_diff_when_routing_constraints_dont_match
|
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'post/:id' => 'post#show', :constraints => { :id => /\d+/ }, :as => 'post'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2012-08-01 16:33:15 -04:00
|
|
|
assert_raise(ActionController::UrlGenerationError) do
|
2010-11-30 10:36:01 -05:00
|
|
|
url_for(rs, { :controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post" })
|
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dynamic_path_allowed
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '*path' => 'content#show_file'
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/pages/boo',
|
|
|
|
url_for(rs, { :controller => 'content', :action => 'show_file', :path => %w(pages boo) })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_dynamic_recall_paths_allowed
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '*path' => 'content#show_file'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/pages/boo')
|
|
|
|
assert_equal({:controller=>"content", :action=>"show_file", :path=>"pages/boo"},
|
|
|
|
controller.request.path_parameters)
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/pages/boo',
|
2014-07-02 18:02:36 -04:00
|
|
|
controller.url_for(:only_path => true)
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_backwards
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id(/:action)' => 'pages#show'
|
|
|
|
get ':controller(/:action(/:id))'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/pages/show')
|
|
|
|
assert_equal '/page/20', controller.url_for({ :id => 20, :only_path => true })
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/page/20', url_for(rs, { :controller => 'pages', :id => 20, :action => 'show' })
|
|
|
|
assert_equal '/pages/boo', url_for(rs, { :controller => 'pages', :action => 'boo' })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_with_fixnum_default
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page(/:id)' => 'content#show_page', :id => 1
|
|
|
|
get ':controller/:action/:id'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/page', url_for(rs, { :controller => 'content', :action => 'show_page' })
|
|
|
|
assert_equal '/page', url_for(rs, { :controller => 'content', :action => 'show_page', :id => 1 })
|
|
|
|
assert_equal '/page', url_for(rs, { :controller => 'content', :action => 'show_page', :id => '1' })
|
|
|
|
assert_equal '/page/10', url_for(rs, { :controller => 'content', :action => 'show_page', :id => 10 })
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-08-06 04:27:41 -04:00
|
|
|
assert_equal({:controller => "content", :action => 'show_page', :id => 1 }, rs.recognize_path("/page"))
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
|
|
|
|
assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10"))
|
|
|
|
end
|
|
|
|
|
|
|
|
# For newer revision
|
|
|
|
def test_route_with_text_default
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id' => 'content#show_page', :id => 1
|
|
|
|
get ':controller/:action/:id'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/page/foo', url_for(rs, { :controller => 'content', :action => 'show_page', :id => 'foo' })
|
|
|
|
assert_equal({ :controller => "content", :action => 'show_page', :id => 'foo' }, rs.recognize_path("/page/foo"))
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2011-03-04 00:09:26 -05:00
|
|
|
token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in Russian
|
2011-12-25 06:34:58 -05:00
|
|
|
token.force_encoding(Encoding::BINARY)
|
2009-02-03 21:25:37 -05:00
|
|
|
escaped_token = CGI::escape(token)
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/page/' + escaped_token, url_for(rs, { :controller => 'content', :action => 'show_page', :id => token })
|
|
|
|
assert_equal({ :controller => "content", :action => 'show_page', :id => token }, rs.recognize_path("/page/#{escaped_token}"))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_action_expiry
|
2012-08-20 08:47:00 -04:00
|
|
|
rs.draw { get ':controller(/:action(/:id))' }
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/content/show')
|
|
|
|
assert_equal '/content', controller.url_for(:controller => 'content', :only_path => true)
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_requirement_should_prevent_optional_id
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'post/:id' => 'post#show', :constraints => {:id => /\d+/}, :as => 'post'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/post/10', url_for(rs, { :controller => 'post', :action => 'show', :id => 10 })
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2012-08-01 16:33:15 -04:00
|
|
|
assert_raise(ActionController::UrlGenerationError) do
|
2010-11-30 10:36:01 -05:00
|
|
|
url_for(rs, { :controller => 'post', :action => 'show' })
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_both_requirement_and_optional
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get('test(/:year)' => 'post#show', :as => 'blog',
|
2009-02-03 21:25:37 -05:00
|
|
|
:defaults => { :year => nil },
|
2010-08-05 09:44:23 -04:00
|
|
|
:constraints => { :year => /\d{4}/ }
|
2009-02-03 21:25:37 -05:00
|
|
|
)
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:id'
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/test', url_for(rs, { :controller => 'post', :action => 'show' })
|
|
|
|
assert_equal '/test', url_for(rs, { :controller => 'post', :action => 'show', :year => nil })
|
2009-02-03 21:25:37 -05:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal("http://test.host/test", setup_for_named_route.send(:blog_url))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_set_to_nil_forgets
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'pages(/:year(/:month(/:day)))' => 'content#list_pages', :month => nil, :day => nil
|
|
|
|
get ':controller/:action/:id'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal '/pages/2005',
|
2010-11-30 10:36:01 -05:00
|
|
|
url_for(rs, { :controller => 'content', :action => 'list_pages', :year => 2005 })
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal '/pages/2005/6',
|
2010-11-30 10:36:01 -05:00
|
|
|
url_for(rs, { :controller => 'content', :action => 'list_pages', :year => 2005, :month => 6 })
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal '/pages/2005/6/12',
|
2010-11-30 10:36:01 -05:00
|
|
|
url_for(rs, { :controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12 })
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/pages/2005/6/12')
|
|
|
|
assert_equal({ :controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12' },
|
|
|
|
controller.request.path_parameters)
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal '/pages/2005/6/4',
|
2014-07-02 18:02:36 -04:00
|
|
|
controller.url_for({ :day => 4, :only_path => true })
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal '/pages/2005/6',
|
2014-07-02 18:02:36 -04:00
|
|
|
controller.url_for({ :day => nil, :only_path => true })
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal '/pages/2005',
|
2014-07-02 18:02:36 -04:00
|
|
|
controller.url_for({ :day => nil, :month => nil, :only_path => true })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-08-05 17:34:39 -04:00
|
|
|
def test_root_url_generation_with_controller_and_action
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2010-08-05 17:34:39 -04:00
|
|
|
root :to => "content#index"
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/', url_for(rs, { :controller => 'content', :action => 'index' })
|
|
|
|
assert_equal '/', url_for(rs, { :controller => 'content' })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-08-05 17:34:39 -04:00
|
|
|
def test_named_root_url_generation_with_controller_and_action
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2010-08-05 17:34:39 -04:00
|
|
|
root :to => "content#index", :as => 'home'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/', url_for(rs, { :controller => 'content', :action => 'index' })
|
|
|
|
assert_equal '/', url_for(rs, { :controller => 'content' })
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal("http://test.host/", setup_for_named_route.send(:home_url))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_method
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'categories' => 'content#categories', :as => 'categories'
|
|
|
|
get ':controller(/:action(/:id))'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/categories', url_for(rs, { :controller => 'content', :action => 'categories' })
|
|
|
|
assert_equal '/content/hi', url_for(rs, { :controller => 'content', :action => 'hi' })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_named_routes_array
|
|
|
|
test_named_route_method
|
|
|
|
assert_equal [:categories], rs.named_routes.names
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_nil_defaults
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'journal' => 'content#list_journal',
|
2009-02-03 21:25:37 -05:00
|
|
|
:date => nil, :user_id => nil
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:id'
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/journal', url_for(rs, {
|
|
|
|
:controller => 'content',
|
|
|
|
:action => 'list_journal',
|
|
|
|
:date => nil,
|
|
|
|
:user_id => nil
|
|
|
|
})
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def setup_request_method_routes_for(method)
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2013-03-31 08:00:12 -04:00
|
|
|
match '/match' => "books##{method}", :via => method.to_sym
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
%w(GET PATCH POST PUT DELETE).each do |request_method|
|
2009-02-03 21:25:37 -05:00
|
|
|
define_method("test_request_method_recognized_with_#{request_method}") do
|
2013-03-31 08:00:12 -04:00
|
|
|
setup_request_method_routes_for(request_method.downcase)
|
2009-12-11 00:45:04 -05:00
|
|
|
params = rs.recognize_path("/match", :method => request_method)
|
|
|
|
assert_equal request_method.downcase, params[:action]
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_recognize_array_of_methods
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2010-08-06 04:27:41 -04:00
|
|
|
match '/match' => 'books#get_or_post', :via => [:get, :post]
|
2012-04-24 23:32:09 -04:00
|
|
|
put '/match' => 'books#not_get_or_post'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2009-12-11 00:45:04 -05:00
|
|
|
params = rs.recognize_path("/match", :method => :post)
|
|
|
|
assert_equal 'get_or_post', params[:action]
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-12-11 00:45:04 -05:00
|
|
|
params = rs.recognize_path("/match", :method => :put)
|
|
|
|
assert_equal 'not_get_or_post', params[:action]
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_subpath_recognized
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/books/:id/edit' => 'subpath_books#edit'
|
|
|
|
get '/items/:id/:action' => 'subpath_books'
|
|
|
|
get '/posts/new/:action' => 'subpath_books'
|
|
|
|
get '/posts/:id' => 'subpath_books#show'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
hash = rs.recognize_path "/books/17/edit"
|
|
|
|
assert_not_nil hash
|
|
|
|
assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
hash = rs.recognize_path "/items/3/complete"
|
|
|
|
assert_not_nil hash
|
|
|
|
assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]]
|
|
|
|
|
|
|
|
hash = rs.recognize_path "/posts/new/preview"
|
|
|
|
assert_not_nil hash
|
|
|
|
assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]]
|
|
|
|
|
|
|
|
hash = rs.recognize_path "/posts/7"
|
|
|
|
assert_not_nil hash
|
|
|
|
assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]]
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_subpath_generated
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/books/:id/edit' => 'subpath_books#edit'
|
|
|
|
get '/items/:id/:action' => 'subpath_books'
|
|
|
|
get '/posts/new/:action' => 'subpath_books'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal "/books/7/edit", url_for(rs, { :controller => "subpath_books", :id => 7, :action => "edit" })
|
|
|
|
assert_equal "/items/15/complete", url_for(rs, { :controller => "subpath_books", :id => 15, :action => "complete" })
|
|
|
|
assert_equal "/posts/new/preview", url_for(rs, { :controller => "subpath_books", :action => "preview" })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
def test_failed_constraints_raises_exception_with_violated_constraints
|
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'foos/:id' => 'foos#show', :as => 'foo_with_requirement', :constraints => { :id => /\d+/ }
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2012-08-01 16:33:15 -04:00
|
|
|
assert_raise(ActionController::UrlGenerationError) do
|
2010-11-30 10:36:01 -05:00
|
|
|
setup_for_named_route.send(:foo_with_requirement_url, "I am Against the constraints")
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_routes_changed_correctly_after_clear
|
2010-09-24 20:15:52 -04:00
|
|
|
rs = ::ActionDispatch::Routing::RouteSet.new
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'ca' => 'ca#aa'
|
|
|
|
get 'cb' => 'cb#ab'
|
|
|
|
get 'cc' => 'cc#ac'
|
|
|
|
get ':controller/:action/:id'
|
|
|
|
get ':controller/:action/:id.:format'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
hash = rs.recognize_path "/cc"
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_not_nil hash
|
|
|
|
assert_equal %w(cc ac), [hash[:controller], hash[:action]]
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'cb' => 'cb#ab'
|
|
|
|
get 'cc' => 'cc#ac'
|
|
|
|
get ':controller/:action/:id'
|
|
|
|
get ':controller/:action/:id.:format'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
hash = rs.recognize_path "/cc"
|
2008-08-28 07:53:29 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_not_nil hash
|
|
|
|
assert_equal %w(cc ac), [hash[:controller], hash[:action]]
|
|
|
|
end
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-08-09 23:38:50 -04:00
|
|
|
class RouteSetTest < ActiveSupport::TestCase
|
2010-11-30 10:36:01 -05:00
|
|
|
include RoutingTestHelpers
|
2014-07-02 18:02:36 -04:00
|
|
|
include ActionDispatch::RoutingVerbs
|
2010-11-30 10:36:01 -05:00
|
|
|
|
2014-07-01 18:41:13 -04:00
|
|
|
attr_reader :set
|
2014-07-02 18:02:36 -04:00
|
|
|
alias :routes :set
|
|
|
|
attr_accessor :controller
|
2014-07-01 18:41:13 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
2014-07-02 18:02:36 -04:00
|
|
|
@set = make_set
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def request
|
|
|
|
@request ||= ActionController::TestRequest.new
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-08-14 17:16:29 -04:00
|
|
|
def default_route_set
|
|
|
|
@default_route_set ||= begin
|
2015-04-24 19:42:28 -04:00
|
|
|
set = ActionDispatch::Routing::RouteSet.new
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/:controller(/:action(/:id))'
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
set
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_generate_extras
|
2012-04-24 23:32:09 -04:00
|
|
|
set.draw { get ':controller/(:action(/:id))' }
|
2009-02-03 21:25:37 -05:00
|
|
|
path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
|
|
|
assert_equal "/foo/bar/15", path
|
2014-10-27 12:28:53 -04:00
|
|
|
assert_equal %w(that this), extras.map(&:to_s).sort
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_extra_keys
|
2012-04-24 23:32:09 -04:00
|
|
|
set.draw { get ':controller/:action/:id' }
|
2009-02-03 21:25:37 -05:00
|
|
|
extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
2014-10-27 12:28:53 -04:00
|
|
|
assert_equal %w(that this), extras.map(&:to_s).sort
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_generate_extras_not_first
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:id.:format'
|
|
|
|
get ':controller/:action/:id'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
|
|
|
assert_equal "/foo/bar/15", path
|
2014-10-27 12:28:53 -04:00
|
|
|
assert_equal %w(that this), extras.map(&:to_s).sort
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_generate_not_first
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:id.:format'
|
|
|
|
get ':controller/:action/:id'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal "/foo/bar/15?this=hello",
|
|
|
|
url_for(set, { :controller => "foo", :action => "bar", :id => 15, :this => "hello" })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_extra_keys_not_first
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:id.:format'
|
|
|
|
get ':controller/:action/:id'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
2014-10-27 12:28:53 -04:00
|
|
|
assert_equal %w(that this), extras.map(&:to_s).sort
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_draw
|
|
|
|
assert_equal 0, set.routes.size
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/hello/world' => 'a#b'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal 1, set.routes.size
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-07-01 14:16:18 -04:00
|
|
|
def test_draw_symbol_controller_name
|
|
|
|
assert_equal 0, set.routes.size
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/users/index' => 'users#index'
|
2009-07-01 14:16:18 -04:00
|
|
|
end
|
2011-02-07 19:44:27 -05:00
|
|
|
set.recognize_path('/users/index', :method => :get)
|
2009-07-01 14:16:18 -04:00
|
|
|
assert_equal 1, set.routes.size
|
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_draw
|
|
|
|
assert_equal 0, set.routes.size
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/hello/world' => 'a#b', :as => 'hello'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal 1, set.routes.size
|
|
|
|
assert_equal set.routes.first, set.named_routes[:hello]
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2013-03-25 02:26:11 -04:00
|
|
|
def test_duplicate_named_route_raises_rather_than_pick_precedence
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
set.draw do
|
|
|
|
get '/hello/world' => 'a#b', :as => 'hello'
|
|
|
|
get '/hello' => 'a#b', :as => 'hello'
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def setup_named_route_test
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/people(/:id)' => 'people#show', :as => 'show'
|
|
|
|
get '/people' => 'people#index', :as => 'index'
|
|
|
|
get '/people/go/:foo/:bar/joe(/:id)' => 'people#multi', :as => 'multi'
|
|
|
|
get '/admin/users' => 'admin/users#index', :as => "users"
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/people')
|
|
|
|
controller
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_url_method
|
|
|
|
controller = setup_named_route_test
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "http://test.host/people/5", controller.send(:show_url, :id => 5)
|
|
|
|
assert_equal "/people/5", controller.send(:show_path, :id => 5)
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "http://test.host/people", controller.send(:index_url)
|
|
|
|
assert_equal "/people", controller.send(:index_path)
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "http://test.host/admin/users", controller.send(:users_url)
|
|
|
|
assert_equal '/admin/users', controller.send(:users_path)
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_url_method_with_anchor
|
|
|
|
controller = setup_named_route_test
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "http://test.host/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
|
|
|
|
assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "http://test.host/people#location", controller.send(:index_url, :anchor => 'location')
|
|
|
|
assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "http://test.host/admin/users#location", controller.send(:users_url, :anchor => 'location')
|
|
|
|
assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "http://test.host/people/go/7/hello/joe/5#location",
|
|
|
|
controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar#location",
|
|
|
|
controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "http://test.host/people?baz=bar#location",
|
|
|
|
controller.send(:index_url, :baz => "bar", :anchor => 'location')
|
2014-11-22 08:42:19 -05:00
|
|
|
|
|
|
|
assert_equal "http://test.host/people", controller.send(:index_url, anchor: nil)
|
|
|
|
assert_equal "http://test.host/people", controller.send(:index_url, anchor: false)
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_url_method_with_port
|
|
|
|
controller = setup_named_route_test
|
|
|
|
assert_equal "http://test.host:8080/people/5", controller.send(:show_url, 5, :port=>8080)
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_url_method_with_host
|
|
|
|
controller = setup_named_route_test
|
|
|
|
assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_url_method_with_protocol
|
|
|
|
controller = setup_named_route_test
|
|
|
|
assert_equal "https://test.host/people/5", controller.send(:show_url, 5, :protocol => "https")
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_url_method_with_ordered_parameters
|
|
|
|
controller = setup_named_route_test
|
|
|
|
assert_equal "http://test.host/people/go/7/hello/joe/5",
|
|
|
|
controller.send(:multi_url, 7, "hello", 5)
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_url_method_with_ordered_parameters_and_hash
|
|
|
|
controller = setup_named_route_test
|
|
|
|
assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar",
|
|
|
|
controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_url_method_with_ordered_parameters_and_empty_hash
|
|
|
|
controller = setup_named_route_test
|
|
|
|
assert_equal "http://test.host/people/go/7/hello/joe/5",
|
|
|
|
controller.send(:multi_url, 7, "hello", 5, {})
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_url_method_with_no_positional_arguments
|
|
|
|
controller = setup_named_route_test
|
|
|
|
assert_equal "http://test.host/people?baz=bar",
|
|
|
|
controller.send(:index_url, :baz => "bar")
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_draw_default_route
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/:controller/:action/:id'
|
2009-10-17 18:17:23 -04:00
|
|
|
end
|
2008-11-14 04:14:02 -05:00
|
|
|
|
2009-10-17 18:17:23 -04:00
|
|
|
assert_equal 1, set.routes.size
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/users/show/10', url_for(set, { :controller => 'users', :action => 'show', :id => 10 })
|
|
|
|
assert_equal '/users/index/10', url_for(set, { :controller => 'users', :id => 10 })
|
2008-06-24 22:50:14 -04:00
|
|
|
|
2009-10-17 18:17:23 -04:00
|
|
|
assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
|
|
|
|
assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_route_with_parameter_shell
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id' => 'pages#show', :id => /\d+/
|
|
|
|
get '/:controller(/:action(/:id))'
|
2009-10-17 18:17:23 -04:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
assert_equal({:controller => 'pages', :action => 'index'}, request_path_params('/pages'))
|
|
|
|
assert_equal({:controller => 'pages', :action => 'index'}, request_path_params('/pages/index'))
|
|
|
|
assert_equal({:controller => 'pages', :action => 'list'}, request_path_params('/pages/list'))
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, request_path_params('/pages/show/10'))
|
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, request_path_params('/page/10'))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-11-17 00:46:15 -05:00
|
|
|
def test_route_constraints_on_request_object_with_anchors_are_valid
|
|
|
|
assert_nothing_raised do
|
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id' => 'pages#show', :constraints => { :host => /^foo$/ }
|
2010-11-17 00:46:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
def test_route_constraints_with_anchor_chars_are_invalid
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ArgumentError do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id' => 'pages#show', :id => /^\d+/
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ArgumentError do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id' => 'pages#show', :id => /\A\d+/
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ArgumentError do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id' => 'pages#show', :id => /\d+$/
|
2008-07-18 21:19:03 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ArgumentError do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id' => 'pages#show', :id => /\d+\Z/
|
2008-07-18 21:19:03 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ArgumentError do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id' => 'pages#show', :id => /\d+\z/
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
def test_route_constraints_with_options_method_condition_is_valid
|
2009-03-08 08:31:27 -04:00
|
|
|
assert_nothing_raised do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2010-08-06 04:27:41 -04:00
|
|
|
match 'valid/route' => 'pages#show', :via => :options
|
2009-03-08 08:31:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-16 16:27:07 -04:00
|
|
|
def test_route_error_with_missing_controller
|
|
|
|
set.draw do
|
|
|
|
get "/people" => "missing#index"
|
|
|
|
end
|
2012-08-01 16:33:15 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
assert_raises(ActionController::RoutingError) { request_path_params '/people' }
|
2011-08-16 16:27:07 -04:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_recognize_with_encoded_id_and_regex
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:id' => 'pages#show', :id => /[a-zA-Z0-9\+]+/
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, request_path_params('/page/10'))
|
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, request_path_params('/page/hello+world'))
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
2010-08-06 03:28:07 -04:00
|
|
|
def test_recognize_with_http_methods
|
|
|
|
set.draw do
|
|
|
|
get "/people" => "people#index", :as => "people"
|
|
|
|
post "/people" => "people#create"
|
|
|
|
get "/people/:id" => "people#show", :as => "person"
|
|
|
|
put "/people/:id" => "people#update"
|
2011-05-06 17:03:55 -04:00
|
|
|
patch "/people/:id" => "people#update"
|
2010-08-06 03:28:07 -04:00
|
|
|
delete "/people/:id" => "people#destroy"
|
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people", :method => :get)
|
2010-08-06 03:28:07 -04:00
|
|
|
assert_equal("index", params[:action])
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people", :method => :post)
|
2010-08-06 03:28:07 -04:00
|
|
|
assert_equal("create", params[:action])
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5", :method => :put)
|
2010-08-06 03:28:07 -04:00
|
|
|
assert_equal("update", params[:action])
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5", :method => :patch)
|
2011-05-06 17:03:55 -04:00
|
|
|
assert_equal("update", params[:action])
|
|
|
|
|
2010-09-04 11:04:57 -04:00
|
|
|
assert_raise(ActionController::UnknownHttpMethod) {
|
2014-07-02 18:02:36 -04:00
|
|
|
request_path_params("/people", :method => :bacon)
|
2010-08-06 03:28:07 -04:00
|
|
|
}
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5", :method => :get)
|
2010-08-06 03:28:07 -04:00
|
|
|
assert_equal("show", params[:action])
|
|
|
|
assert_equal("5", params[:id])
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5", :method => :put)
|
2010-08-06 03:28:07 -04:00
|
|
|
assert_equal("update", params[:action])
|
|
|
|
assert_equal("5", params[:id])
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5", :method => :patch)
|
2011-05-06 17:03:55 -04:00
|
|
|
assert_equal("update", params[:action])
|
|
|
|
assert_equal("5", params[:id])
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5", :method => :delete)
|
2010-08-06 03:28:07 -04:00
|
|
|
assert_equal("destroy", params[:action])
|
|
|
|
assert_equal("5", params[:id])
|
|
|
|
|
|
|
|
assert_raise(ActionController::RoutingError) {
|
2014-07-02 18:02:36 -04:00
|
|
|
request_path_params("/people/5", :method => :post)
|
2010-08-06 03:28:07 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_recognize_with_alias_in_conditions
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2010-08-06 04:27:41 -04:00
|
|
|
match "/people" => 'people#index', :as => 'people', :via => :get
|
|
|
|
root :to => "people#index"
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2006-06-01 11:42:08 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("people", params[:controller])
|
|
|
|
assert_equal("index", params[:action])
|
2006-06-01 11:42:08 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("people", params[:controller])
|
|
|
|
assert_equal("index", params[:action])
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_typo_recognition
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'articles/:year/:month/:day/:title' => 'articles#permalink',
|
2009-02-03 21:25:37 -05:00
|
|
|
:year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2006-06-01 11:42:08 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/articles/2005/11/05/a-very-interesting-article", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("permalink", params[:action])
|
|
|
|
assert_equal("2005", params[:year])
|
|
|
|
assert_equal("11", params[:month])
|
|
|
|
assert_equal("05", params[:day])
|
|
|
|
assert_equal("a-very-interesting-article", params[:title])
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-06-01 11:42:08 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_routing_traversal_does_not_load_extra_classes
|
|
|
|
assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/profile' => 'profile#index'
|
2006-06-01 11:42:08 -04:00
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
request_path_params("/profile") rescue nil
|
2006-12-03 19:12:00 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_recognize_with_conditions_and_format
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2010-08-06 04:27:41 -04:00
|
|
|
get "people/:id" => "people#show", :as => "person"
|
|
|
|
put "people/:id" => "people#update"
|
2011-05-06 17:03:55 -04:00
|
|
|
patch "people/:id" => "people#update"
|
2010-09-04 11:04:57 -04:00
|
|
|
get "people/:id(.:format)" => "people#show"
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("show", params[:action])
|
|
|
|
assert_equal("5", params[:id])
|
2009-02-03 21:25:37 -05:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5", :method => :put)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("update", params[:action])
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5", :method => :patch)
|
2011-05-06 17:03:55 -04:00
|
|
|
assert_equal("update", params[:action])
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/people/5.png", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("show", params[:action])
|
|
|
|
assert_equal("5", params[:id])
|
2010-09-04 11:04:57 -04:00
|
|
|
assert_equal("png", params[:format])
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_generate_with_default_action
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "/people", :controller => "people", :action => "index"
|
|
|
|
get "/people/list", :controller => "people", :action => "list"
|
2007-05-19 12:26:44 -04:00
|
|
|
end
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
url = url_for(set, { :controller => "people", :action => "list" })
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "/people/list", url
|
|
|
|
end
|
2007-05-19 12:26:44 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_root_map
|
2010-08-05 17:34:39 -04:00
|
|
|
set.draw { root :to => 'people#index' }
|
2007-05-19 12:26:44 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("people", params[:controller])
|
|
|
|
assert_equal("index", params[:action])
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_namespace
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2009-02-03 21:25:37 -05:00
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
namespace 'api' do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'inventory' => 'products#inventory'
|
2007-05-19 12:26:44 -04:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2007-05-19 12:26:44 -04:00
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/api/inventory", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("api/products", params[:controller])
|
|
|
|
assert_equal("inventory", params[:action])
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-07-03 10:49:12 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_namespaced_root_map
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
|
|
|
namespace 'api' do
|
2010-08-05 17:34:39 -04:00
|
|
|
root :to => 'products#index'
|
2008-07-03 10:49:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/api", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("api/products", params[:controller])
|
|
|
|
assert_equal("index", params[:action])
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-12-03 19:12:00 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_namespace_with_path_prefix
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2010-08-05 17:34:39 -04:00
|
|
|
scope :module => "api", :path => "prefix" do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'inventory' => 'products#inventory'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2006-06-06 13:59:54 -04:00
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/prefix/inventory", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("api/products", params[:controller])
|
|
|
|
assert_equal("inventory", params[:action])
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-06-06 13:59:54 -04:00
|
|
|
|
2009-03-09 11:27:13 -04:00
|
|
|
def test_namespace_with_blank_path_prefix
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2010-08-05 17:34:39 -04:00
|
|
|
scope :module => "api", :path => "" do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'inventory' => 'products#inventory'
|
2009-03-09 11:27:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
params = request_path_params("/inventory", :method => :get)
|
2009-12-11 00:45:04 -05:00
|
|
|
assert_equal("api/products", params[:controller])
|
|
|
|
assert_equal("inventory", params[:action])
|
2009-03-09 11:27:13 -04:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_id_is_sticky_when_it_ought_to_be
|
2014-07-02 18:02:36 -04:00
|
|
|
@set = make_set false
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:id/:action'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2006-06-06 15:09:56 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/people/7/show')
|
|
|
|
|
|
|
|
assert_equal "/people/7/destroy", controller.url_for(:action => 'destroy', :only_path => true)
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-10-08 04:31:00 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_use_static_path_when_possible
|
2014-07-02 18:02:36 -04:00
|
|
|
@set = make_set false
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'about' => "welcome#about"
|
|
|
|
get ':controller/:action/:id'
|
2008-10-08 04:31:00 -04:00
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/welcom/get/7')
|
2010-11-30 10:36:01 -05:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
assert_equal "/about", controller.url_for(:controller => 'welcome',
|
|
|
|
:action => 'about',
|
|
|
|
:only_path => true)
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-06-06 15:09:56 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_generate
|
2012-04-24 23:32:09 -04:00
|
|
|
set.draw { get ':controller/:action/:id' }
|
2006-06-06 15:09:56 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal "/foo/bar/7?x=y", url_for(set, args)
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
|
|
|
|
assert_equal [:x], set.extra_keys(args)
|
|
|
|
end
|
2006-06-06 15:09:56 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_generate_with_path_prefix
|
2010-08-05 17:34:39 -04:00
|
|
|
set.draw do
|
|
|
|
scope "my" do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller(/:action(/:id))'
|
2010-08-05 17:34:39 -04:00
|
|
|
end
|
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal "/my/foo/bar/7?x=y", url_for(set, args)
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
2009-03-09 11:27:13 -04:00
|
|
|
def test_generate_with_blank_path_prefix
|
2010-08-05 17:34:39 -04:00
|
|
|
set.draw do
|
|
|
|
scope "" do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller(/:action(/:id))'
|
2010-08-05 17:34:39 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-09 11:27:13 -04:00
|
|
|
|
|
|
|
args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal "/foo/bar/7?x=y", url_for(set, args)
|
2009-03-09 11:27:13 -04:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_routes_are_never_relative_to_modules
|
2014-07-02 18:02:36 -04:00
|
|
|
@set = make_set false
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "/connection/manage(/:action)" => 'connection/manage#index'
|
|
|
|
get "/connection/connection" => "connection/connection#index"
|
|
|
|
get '/connection' => 'connection#index', :as => 'family_connection'
|
2006-12-08 23:42:32 -05:00
|
|
|
end
|
2007-03-26 15:02:19 -04:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
assert_equal({ :controller => 'connection/manage',
|
|
|
|
:action => 'index', }, request_path_params('/connection/manage'))
|
|
|
|
|
|
|
|
url = controller.url_for({ :controller => "connection", :only_path => true })
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "/connection/connection", url
|
|
|
|
|
2014-11-23 04:08:53 -05:00
|
|
|
url = controller.url_for({ :use_route => "family_connection",
|
2014-07-02 18:02:36 -04:00
|
|
|
:controller => "connection", :only_path => true })
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal "/connection", url
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_action_left_off_when_id_is_recalled
|
2014-07-02 18:02:36 -04:00
|
|
|
@set = make_set false
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller(/:action(/:id))'
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2014-07-02 18:02:36 -04:00
|
|
|
|
|
|
|
get URI('http://test.host/books/show/10')
|
|
|
|
|
|
|
|
assert_equal '/books', controller.url_for(:controller => 'books',
|
|
|
|
:only_path => true,
|
|
|
|
:action => 'index')
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2007-03-26 15:02:19 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_query_params_will_be_shown_when_recalled
|
2014-07-02 18:02:36 -04:00
|
|
|
@set = make_set false
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'show_weblog/:parameter' => 'weblog#show'
|
|
|
|
get ':controller(/:action(/:id))'
|
2007-01-28 12:29:51 -05:00
|
|
|
end
|
2014-07-02 18:02:36 -04:00
|
|
|
|
|
|
|
get URI('http://test.host/weblog/show/1')
|
|
|
|
|
|
|
|
assert_equal '/weblog/edit?parameter=1', controller.url_for(
|
|
|
|
{:action => 'edit', :parameter => 1, :only_path => true})
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-03-04 15:37:59 -05:00
|
|
|
def test_format_is_not_inherit
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/posts(.:format)' => 'posts#index'
|
2009-03-04 15:37:59 -05:00
|
|
|
end
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
get URI('http://test.host/posts.xml')
|
|
|
|
assert_equal({:controller => 'posts', :action => 'index', :format => 'xml'},
|
|
|
|
controller.request.path_parameters)
|
|
|
|
|
|
|
|
assert_equal '/posts', controller.url_for(
|
|
|
|
{:controller => 'posts', :only_path => true})
|
2009-03-04 15:37:59 -05:00
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
assert_equal '/posts.xml', controller.url_for(
|
|
|
|
{:controller => 'posts', :format => 'xml', :only_path => true})
|
2009-03-04 15:37:59 -05:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_expiry_determination_should_consider_values_with_to_param
|
2014-07-02 18:02:36 -04:00
|
|
|
@set = make_set false
|
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
set.draw { get 'projects/:project_id/:controller/:action' }
|
2014-07-02 18:02:36 -04:00
|
|
|
|
|
|
|
get URI('http://test.host/projects/1/weblog/show')
|
|
|
|
|
|
|
|
assert_equal(
|
|
|
|
{ :controller => 'weblog', :action => 'show', :project_id => '1' },
|
|
|
|
controller.request.path_parameters)
|
|
|
|
|
|
|
|
assert_equal '/projects/1/weblog/show',
|
|
|
|
controller.url_for({ :action => 'show', :project_id => 1, :only_path => true })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_named_route_in_nested_resource
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
|
|
|
resources :projects do
|
2010-08-06 04:27:41 -04:00
|
|
|
member do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'milestones' => 'milestones#index', :as => 'milestones'
|
2010-08-06 04:27:41 -04:00
|
|
|
end
|
2007-11-26 17:41:28 -05:00
|
|
|
end
|
|
|
|
end
|
2008-03-28 16:01:21 -04:00
|
|
|
|
2009-12-11 00:45:04 -05:00
|
|
|
params = set.recognize_path("/projects/1/milestones", :method => :get)
|
|
|
|
assert_equal("milestones", params[:controller])
|
|
|
|
assert_equal("index", params[:action])
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_setting_root_in_namespace_using_symbol
|
|
|
|
assert_nothing_raised do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
|
|
|
namespace :admin do
|
2010-08-05 17:34:39 -04:00
|
|
|
root :to => "home#index"
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_setting_root_in_namespace_using_string
|
|
|
|
assert_nothing_raised do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
|
|
|
namespace 'admin' do
|
2010-08-05 17:34:39 -04:00
|
|
|
root :to => "home#index"
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-08 00:42:05 -04:00
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
def test_route_constraints_with_unsupported_regexp_options_must_error
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ArgumentError do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:name' => 'pages#show',
|
2010-11-30 10:36:01 -05:00
|
|
|
:constraints => { :name => /(david|jamis)/m }
|
2008-06-08 00:42:05 -04:00
|
|
|
end
|
2008-03-28 16:01:21 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-03-28 16:01:21 -04:00
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
def test_route_constraints_with_supported_options_must_not_error
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_nothing_raised do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:name' => 'pages#show',
|
2010-11-30 10:36:01 -05:00
|
|
|
:constraints => { :name => /(david|jamis)/i }
|
2008-03-28 16:01:21 -04:00
|
|
|
end
|
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_nothing_raised do
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:name' => 'pages#show',
|
2010-11-30 10:36:01 -05:00
|
|
|
:constraints => { :name => / # Desperately overcommented regexp
|
2008-03-28 16:01:21 -04:00
|
|
|
( #Either
|
|
|
|
david #The Creator
|
|
|
|
| #Or
|
|
|
|
jamis #The Deployer
|
2010-11-30 10:36:01 -05:00
|
|
|
)/x }
|
2008-03-28 16:01:21 -04:00
|
|
|
end
|
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2012-05-03 22:26:54 -04:00
|
|
|
|
2011-11-27 13:55:50 -05:00
|
|
|
def test_route_with_subdomain_and_constraints_must_receive_params
|
|
|
|
name_param = nil
|
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:name' => 'pages#show', :constraints => lambda {|request|
|
2011-11-27 13:55:50 -05:00
|
|
|
name_param = request.params[:name]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
end
|
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :name => 'mypage'},
|
|
|
|
set.recognize_path('http://subdomain.example.org/page/mypage'))
|
|
|
|
assert_equal(name_param, 'mypage')
|
|
|
|
end
|
2012-05-03 22:26:54 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_route_requirement_recognize_with_ignore_case
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:name' => 'pages#show',
|
2010-08-05 09:44:23 -04:00
|
|
|
:constraints => {:name => /(david|jamis)/i}
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ActionController::RoutingError do
|
2009-02-03 21:25:37 -05:00
|
|
|
set.recognize_path('/page/davidjamis')
|
2008-03-28 16:01:21 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID'))
|
|
|
|
end
|
2008-03-28 16:01:21 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_route_requirement_generate_with_ignore_case
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:name' => 'pages#show',
|
2010-08-05 09:44:23 -04:00
|
|
|
:constraints => {:name => /(david|jamis)/i}
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2009-11-19 20:57:20 -05:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
url = url_for(set, { :controller => 'pages', :action => 'show', :name => 'david' })
|
2009-11-23 17:05:30 -05:00
|
|
|
assert_equal "/page/david", url
|
2012-08-01 16:33:15 -04:00
|
|
|
assert_raise(ActionController::UrlGenerationError) do
|
2010-11-30 10:36:01 -05:00
|
|
|
url_for(set, { :controller => 'pages', :action => 'show', :name => 'davidjamis' })
|
2006-11-12 20:17:10 -05:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
url = url_for(set, { :controller => 'pages', :action => 'show', :name => 'JAMIS' })
|
2009-11-23 17:05:30 -05:00
|
|
|
assert_equal "/page/JAMIS", url
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2006-11-12 20:17:10 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_route_requirement_recognize_with_extended_syntax
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:name' => 'pages#show',
|
2010-08-05 09:44:23 -04:00
|
|
|
:constraints => {:name => / # Desperately overcommented regexp
|
2009-02-03 21:25:37 -05:00
|
|
|
( #Either
|
|
|
|
david #The Creator
|
|
|
|
| #Or
|
|
|
|
jamis #The Deployer
|
|
|
|
)/x}
|
|
|
|
end
|
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
|
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david'))
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ActionController::RoutingError do
|
2009-02-03 21:25:37 -05:00
|
|
|
set.recognize_path('/page/david #The Creator')
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ActionController::RoutingError do
|
2009-02-03 21:25:37 -05:00
|
|
|
set.recognize_path('/page/David')
|
2006-06-01 11:42:08 -04:00
|
|
|
end
|
|
|
|
end
|
2006-06-07 12:16:37 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
def test_route_requirement_with_xi_modifiers
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'page/:name' => 'pages#show',
|
2010-08-05 09:44:23 -04:00
|
|
|
:constraints => {:name => / # Desperately overcommented regexp
|
2009-02-03 21:25:37 -05:00
|
|
|
( #Either
|
|
|
|
david #The Creator
|
|
|
|
| #Or
|
|
|
|
jamis #The Deployer
|
|
|
|
)/xi}
|
2007-10-12 23:28:35 -04:00
|
|
|
end
|
2009-11-19 20:57:20 -05:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'},
|
|
|
|
set.recognize_path('/page/JAMIS'))
|
2007-10-12 23:28:35 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal "/page/JAMIS",
|
|
|
|
url_for(set, { :controller => 'pages', :action => 'show', :name => 'JAMIS' })
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2009-06-23 16:54:32 -04:00
|
|
|
|
|
|
|
def test_routes_with_symbols
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'unnamed', :controller => :pages, :action => :show, :name => :as_symbol
|
|
|
|
get 'named' , :controller => :pages, :action => :show, :name => :as_symbol, :as => :named
|
2009-06-23 16:54:32 -04:00
|
|
|
end
|
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, set.recognize_path('/unnamed'))
|
|
|
|
assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, set.recognize_path('/named'))
|
|
|
|
end
|
|
|
|
|
2009-08-14 17:16:29 -04:00
|
|
|
def test_regexp_chunk_should_add_question_mark_for_optionals
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/' => 'foo#index'
|
|
|
|
get '/hello' => 'bar#index'
|
2009-10-17 18:17:23 -04:00
|
|
|
end
|
2009-08-14 17:16:29 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/', url_for(set, { :controller => 'foo' })
|
|
|
|
assert_equal '/hello', url_for(set, { :controller => 'bar' })
|
2009-08-14 17:16:29 -04:00
|
|
|
|
2009-10-17 18:17:23 -04:00
|
|
|
assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/'))
|
|
|
|
assert_equal({:controller => "bar", :action => "index"}, set.recognize_path('/hello'))
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_route_options_with_anchor_chars
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/cars/:action/:person/:car/', :controller => 'cars'
|
2009-10-17 18:17:23 -04:00
|
|
|
end
|
2009-08-14 17:16:29 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/cars/buy/1/2', url_for(set, { :controller => 'cars', :action => 'buy', :person => '1', :car => '2' })
|
2009-08-14 17:16:29 -04:00
|
|
|
|
2009-10-17 18:17:23 -04:00
|
|
|
assert_equal({:controller => "cars", :action => "buy", :person => "1", :car => "2"}, set.recognize_path('/cars/buy/1/2'))
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_segmentation_of_dot_path
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/books/:action.rss', :controller => 'books'
|
2009-10-17 18:17:23 -04:00
|
|
|
end
|
2009-08-14 17:16:29 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/books/list.rss', url_for(set, { :controller => 'books', :action => 'list' })
|
2009-08-14 17:16:29 -04:00
|
|
|
|
2009-10-17 18:17:23 -04:00
|
|
|
assert_equal({:controller => "books", :action => "list"}, set.recognize_path('/books/list.rss'))
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_segmentation_of_dynamic_dot_path
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/books(/:action(.:format))', :controller => 'books'
|
2009-10-17 18:17:23 -04:00
|
|
|
end
|
2009-08-14 17:16:29 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/books/list.rss', url_for(set, { :controller => 'books', :action => 'list', :format => 'rss' })
|
|
|
|
assert_equal '/books/list.xml', url_for(set, { :controller => 'books', :action => 'list', :format => 'xml' })
|
|
|
|
assert_equal '/books/list', url_for(set, { :controller => 'books', :action => 'list' })
|
|
|
|
assert_equal '/books', url_for(set, { :controller => 'books', :action => 'index' })
|
2009-08-14 17:16:29 -04:00
|
|
|
|
2009-10-17 18:17:23 -04:00
|
|
|
assert_equal({:controller => "books", :action => "list", :format => "rss"}, set.recognize_path('/books/list.rss'))
|
|
|
|
assert_equal({:controller => "books", :action => "list", :format => "xml"}, set.recognize_path('/books/list.xml'))
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal({:controller => "books", :action => "list"}, set.recognize_path('/books/list'))
|
2009-10-17 18:17:23 -04:00
|
|
|
assert_equal({:controller => "books", :action => "index"}, set.recognize_path('/books'))
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_slashes_are_implied
|
2012-04-24 23:32:09 -04:00
|
|
|
set.draw { get("/:controller(/:action(/:id))") }
|
2009-08-26 00:34:48 -04:00
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/content', url_for(set, { :controller => 'content', :action => 'index' })
|
|
|
|
assert_equal '/content/list', url_for(set, { :controller => 'content', :action => 'list' })
|
|
|
|
assert_equal '/content/show/1', url_for(set, { :controller => 'content', :action => 'show', :id => '1' })
|
2009-08-26 00:34:48 -04:00
|
|
|
|
2010-08-05 17:34:39 -04:00
|
|
|
assert_equal({:controller => "content", :action => "index"}, set.recognize_path('/content'))
|
|
|
|
assert_equal({:controller => "content", :action => "index"}, set.recognize_path('/content/index'))
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal({:controller => "content", :action => "list"}, set.recognize_path('/content/list'))
|
2010-08-05 17:34:39 -04:00
|
|
|
assert_equal({:controller => "content", :action => "show", :id => "1"}, set.recognize_path('/content/show/1'))
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_route_recognition
|
2009-09-05 20:39:33 -04:00
|
|
|
expected = {:controller => 'pages', :action => 'show', :id => '10'}
|
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages/show/10')
|
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages/show/10/')
|
2009-08-14 17:16:29 -04:00
|
|
|
|
|
|
|
expected[:id] = 'jamis'
|
2009-09-05 20:39:33 -04:00
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages/show/jamis/')
|
2009-08-14 17:16:29 -04:00
|
|
|
|
|
|
|
expected.delete :id
|
2009-09-05 20:39:33 -04:00
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages/show')
|
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages/show/')
|
2009-08-14 17:16:29 -04:00
|
|
|
|
|
|
|
expected[:action] = 'index'
|
2009-09-05 20:39:33 -04:00
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages/')
|
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages')
|
2009-08-14 17:16:29 -04:00
|
|
|
|
|
|
|
assert_raise(ActionController::RoutingError) { default_route_set.recognize_path('/') }
|
2009-09-05 20:39:33 -04:00
|
|
|
assert_raise(ActionController::RoutingError) { default_route_set.recognize_path('/pages/how/goood/it/is/to/be/free') }
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_route_should_omit_default_action
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/accounts', url_for(default_route_set, { :controller => 'accounts', :action => 'index' })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_route_should_include_default_action_when_id_present
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/accounts/index/20', url_for(default_route_set, { :controller => 'accounts', :action => 'index', :id => '20' })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_route_should_work_with_action_but_no_id
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/accounts/list_all', url_for(default_route_set, { :controller => 'accounts', :action => 'list_all' })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_route_should_uri_escape_pluses
|
2009-09-05 20:39:33 -04:00
|
|
|
expected = { :controller => 'pages', :action => 'show', :id => 'hello world' }
|
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages/show/hello%20world')
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/pages/show/hello%20world', url_for(default_route_set, expected)
|
2009-08-14 17:16:29 -04:00
|
|
|
|
|
|
|
expected[:id] = 'hello+world'
|
2009-09-05 20:39:33 -04:00
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages/show/hello+world')
|
|
|
|
assert_equal expected, default_route_set.recognize_path('/pages/show/hello%2Bworld')
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/pages/show/hello+world', url_for(default_route_set, expected)
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_build_empty_query_string
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_uri_equal '/foo', url_for(default_route_set, { :controller => 'foo' })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_build_query_string_with_nil_value
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_uri_equal '/foo', url_for(default_route_set, { :controller => 'foo', :x => nil })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_simple_build_query_string
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_uri_equal '/foo?x=1&y=2', url_for(default_route_set, { :controller => 'foo', :x => '1', :y => '2' })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_convert_ints_build_query_string
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_uri_equal '/foo?x=1&y=2', url_for(default_route_set, { :controller => 'foo', :x => 1, :y => 2 })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_escape_spaces_build_query_string
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_uri_equal '/foo?x=hello+world&y=goodbye+world', url_for(default_route_set, { :controller => 'foo', :x => 'hello world', :y => 'goodbye world' })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_array_build_query_string
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_uri_equal '/foo?x%5B%5D=1&x%5B%5D=2', url_for(default_route_set, { :controller => 'foo', :x => [1, 2] })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_escape_spaces_build_query_string_selected_keys
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_uri_equal '/foo?x=hello+world', url_for(default_route_set, { :controller => 'foo', :x => 'hello world' })
|
2009-08-14 17:16:29 -04:00
|
|
|
end
|
2009-10-17 18:34:00 -04:00
|
|
|
|
2009-11-09 22:54:50 -05:00
|
|
|
def test_generate_with_default_params
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'dummy/page/:page' => 'dummy#show'
|
|
|
|
get 'dummy/dots/page.:page' => 'dummy#dots'
|
|
|
|
get 'ibocorp(/:page)' => 'ibocorp#show',
|
2010-08-05 09:44:23 -04:00
|
|
|
:constraints => { :page => /\d+/ },
|
|
|
|
:defaults => { :page => 1 }
|
2009-11-09 22:54:50 -05:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action/:id'
|
2009-11-09 22:54:50 -05:00
|
|
|
end
|
|
|
|
|
2010-11-30 10:36:01 -05:00
|
|
|
assert_equal '/ibocorp', url_for(set, { :controller => 'ibocorp', :action => "show", :page => 1 })
|
2009-11-09 22:54:50 -05:00
|
|
|
end
|
|
|
|
|
2014-07-01 19:28:05 -04:00
|
|
|
include ActionDispatch::RoutingVerbs
|
|
|
|
|
2015-04-24 19:42:28 -04:00
|
|
|
class TestSet < ActionDispatch::Routing::RouteSet
|
2014-07-01 19:28:05 -04:00
|
|
|
def initialize(block)
|
|
|
|
@block = block
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2015-04-24 19:42:28 -04:00
|
|
|
class Dispatcher < ActionDispatch::Routing::RouteSet::Dispatcher
|
2014-07-01 19:28:05 -04:00
|
|
|
def initialize(defaults, set, block)
|
|
|
|
super(defaults)
|
|
|
|
@block = block
|
|
|
|
@set = set
|
|
|
|
end
|
|
|
|
|
|
|
|
def controller_reference(controller_param)
|
|
|
|
block = @block
|
|
|
|
set = @set
|
|
|
|
Class.new(ActionController::Base) {
|
|
|
|
include set.url_helpers
|
|
|
|
define_method(:process) { |name| block.call(self) }
|
|
|
|
def to_a; [200, {}, []]; end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def dispatcher defaults
|
|
|
|
TestSet::Dispatcher.new defaults, self, @block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias :routes :set
|
|
|
|
|
2009-11-09 23:17:38 -05:00
|
|
|
def test_generate_with_optional_params_recalls_last_request
|
2014-07-01 19:28:05 -04:00
|
|
|
controller = nil
|
|
|
|
@set = TestSet.new ->(c) { controller = c }
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "blog/", :controller => "blog", :action => "index"
|
2009-11-09 23:17:38 -05:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get "blog(/:year(/:month(/:day)))",
|
2010-08-05 09:44:23 -04:00
|
|
|
:controller => "blog",
|
|
|
|
:action => "show_date",
|
|
|
|
:constraints => { :year => /(19|20)\d\d/, :month => /[01]?\d/, :day => /[0-3]?\d/ },
|
|
|
|
:day => nil, :month => nil
|
2009-11-09 23:17:38 -05:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get "blog/show/:id", :controller => "blog", :action => "show", :id => /\d+/
|
|
|
|
get "blog/:controller/:action(/:id)"
|
|
|
|
get "*anything", :controller => "blog", :action => "unknown_request"
|
2009-11-09 23:17:38 -05:00
|
|
|
end
|
|
|
|
|
2014-07-01 19:28:05 -04:00
|
|
|
recognize_path = ->(path) {
|
|
|
|
get(URI("http://example.org" + path))
|
|
|
|
controller.request.path_parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal({:controller => "blog", :action => "index"}, recognize_path.("/blog"))
|
|
|
|
assert_equal({:controller => "blog", :action => "show", :id => "123"}, recognize_path.("/blog/show/123"))
|
|
|
|
assert_equal({:controller => "blog", :action => "show_date", :year => "2004", :day => nil, :month => nil }, recognize_path.("/blog/2004"))
|
|
|
|
assert_equal({:controller => "blog", :action => "show_date", :year => "2004", :month => "12", :day => nil }, recognize_path.("/blog/2004/12"))
|
|
|
|
assert_equal({:controller => "blog", :action => "show_date", :year => "2004", :month => "12", :day => "25"}, recognize_path.("/blog/2004/12/25"))
|
|
|
|
assert_equal({:controller => "articles", :action => "edit", :id => "123"}, recognize_path.("/blog/articles/edit/123"))
|
|
|
|
assert_equal({:controller => "articles", :action => "show_stats"}, recognize_path.("/blog/articles/show_stats"))
|
|
|
|
assert_equal({:controller => "blog", :action => "unknown_request", :anything => "blog/wibble"}, recognize_path.("/blog/wibble"))
|
|
|
|
assert_equal({:controller => "blog", :action => "unknown_request", :anything => "junk"}, recognize_path.("/junk"))
|
|
|
|
|
|
|
|
get URI('http://example.org/blog/2006/07/28')
|
|
|
|
|
|
|
|
assert_equal({:controller => "blog", :action => "show_date", :year => "2006", :month => "07", :day => "28"}, controller.request.path_parameters)
|
|
|
|
assert_equal("/blog/2006/07/25", controller.url_for({ :day => 25, :only_path => true }))
|
|
|
|
assert_equal("/blog/2005", controller.url_for({ :year => 2005, :only_path => true }))
|
|
|
|
assert_equal("/blog/show/123", controller.url_for({ :action => "show" , :id => 123, :only_path => true }))
|
|
|
|
assert_equal("/blog/2006", controller.url_for({ :year => 2006, :only_path => true }))
|
|
|
|
assert_equal("/blog/2006", controller.url_for({ :year => 2006, :month => nil, :only_path => true }))
|
2009-11-09 23:17:38 -05:00
|
|
|
end
|
|
|
|
|
2009-10-17 18:34:00 -04:00
|
|
|
private
|
|
|
|
def assert_uri_equal(expected, actual)
|
|
|
|
assert_equal(sort_query_string_params(expected), sort_query_string_params(actual))
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_query_string_params(uri)
|
|
|
|
path, qs = uri.split('?')
|
|
|
|
qs = qs.split('&').sort.join('&') if qs
|
|
|
|
qs ? "#{path}?#{qs}" : path
|
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2007-10-12 23:28:35 -04:00
|
|
|
|
2009-10-20 10:52:59 -04:00
|
|
|
class RackMountIntegrationTests < ActiveSupport::TestCase
|
2010-11-30 10:36:01 -05:00
|
|
|
include RoutingTestHelpers
|
|
|
|
|
2009-10-20 10:52:59 -04:00
|
|
|
Model = Struct.new(:to_param)
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
Mapping = lambda {
|
|
|
|
namespace :admin do
|
2010-08-06 04:27:41 -04:00
|
|
|
resources :users, :posts
|
2009-10-20 10:52:59 -04:00
|
|
|
end
|
|
|
|
|
2010-08-05 09:44:23 -04:00
|
|
|
namespace 'api' do
|
2010-08-06 04:27:41 -04:00
|
|
|
root :to => 'users#index'
|
2009-10-20 10:52:59 -04:00
|
|
|
end
|
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/blog(/:year(/:month(/:day)))' => 'posts#show_date',
|
2010-09-24 20:15:52 -04:00
|
|
|
:constraints => {
|
2010-08-06 04:27:41 -04:00
|
|
|
:year => /(19|20)\d\d/,
|
|
|
|
:month => /[01]?\d/,
|
|
|
|
:day => /[0-3]?\d/
|
|
|
|
},
|
|
|
|
:day => nil,
|
|
|
|
:month => nil
|
2009-10-20 10:52:59 -04:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'archive/:year', :controller => 'archive', :action => 'index',
|
2009-10-20 10:52:59 -04:00
|
|
|
:defaults => { :year => nil },
|
2010-08-05 17:34:39 -04:00
|
|
|
:constraints => { :year => /\d{4}/ },
|
|
|
|
:as => "blog"
|
2009-10-20 10:52:59 -04:00
|
|
|
|
2010-08-05 17:34:39 -04:00
|
|
|
resources :people
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'legacy/people' => "people#index", :legacy => "true"
|
2009-10-20 10:52:59 -04:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'symbols', :controller => :symbols, :action => :show, :name => :as_symbol
|
|
|
|
get 'id_default(/:id)' => "foo#id_default", :id => 1
|
2010-08-06 04:27:41 -04:00
|
|
|
match 'get_or_post' => "foo#get_or_post", :via => [:get, :post]
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'optional/:optional' => "posts#index"
|
|
|
|
get 'projects/:project_id' => "project#index", :as => "project"
|
|
|
|
get 'clients' => "projects#index"
|
2010-08-06 04:27:41 -04:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'ignorecase/geocode/:postalcode' => 'geocode#show', :postalcode => /hx\d\d-\d[a-z]{2}/i
|
|
|
|
get 'extended/geocode/:postalcode' => 'geocode#show',:constraints => {
|
2009-10-20 10:52:59 -04:00
|
|
|
:postalcode => /# Postcode format
|
|
|
|
\d{5} #Prefix
|
|
|
|
(-\d{4})? #Suffix
|
|
|
|
/x
|
2010-08-06 04:27:41 -04:00
|
|
|
}, :as => "geocode"
|
2009-10-20 10:52:59 -04:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'news(.:format)' => "news#index"
|
2010-08-06 03:28:07 -04:00
|
|
|
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'comment/:id(/:action)' => "comments#show"
|
|
|
|
get 'ws/:controller(/:action(/:id))', :ws => true
|
|
|
|
get 'account(/:action)' => "account#subscription"
|
|
|
|
get 'pages/:page_id/:controller(/:action(/:id))'
|
|
|
|
get ':controller/ping', :action => 'ping'
|
2012-06-05 11:59:01 -04:00
|
|
|
get 'こんにちは/世界', :controller => 'news', :action => 'index'
|
2012-04-24 23:32:09 -04:00
|
|
|
match ':controller(/:action(/:id))(.:format)', :via => :all
|
2010-08-06 04:27:41 -04:00
|
|
|
root :to => "news#index"
|
2009-10-20 10:52:59 -04:00
|
|
|
}
|
|
|
|
|
2014-07-02 18:02:36 -04:00
|
|
|
attr_reader :routes
|
|
|
|
attr_reader :controller
|
|
|
|
|
2009-10-20 10:52:59 -04:00
|
|
|
def setup
|
2010-09-24 20:15:52 -04:00
|
|
|
@routes = ActionDispatch::Routing::RouteSet.new
|
2009-10-20 10:52:59 -04:00
|
|
|
@routes.draw(&Mapping)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_recognize_path
|
|
|
|
assert_equal({:controller => 'admin/users', :action => 'index'}, @routes.recognize_path('/admin/users', :method => :get))
|
|
|
|
assert_equal({:controller => 'admin/users', :action => 'create'}, @routes.recognize_path('/admin/users', :method => :post))
|
|
|
|
assert_equal({:controller => 'admin/users', :action => 'new'}, @routes.recognize_path('/admin/users/new', :method => :get))
|
|
|
|
assert_equal({:controller => 'admin/users', :action => 'show', :id => '1'}, @routes.recognize_path('/admin/users/1', :method => :get))
|
|
|
|
assert_equal({:controller => 'admin/users', :action => 'update', :id => '1'}, @routes.recognize_path('/admin/users/1', :method => :put))
|
|
|
|
assert_equal({:controller => 'admin/users', :action => 'destroy', :id => '1'}, @routes.recognize_path('/admin/users/1', :method => :delete))
|
|
|
|
assert_equal({:controller => 'admin/users', :action => 'edit', :id => '1'}, @routes.recognize_path('/admin/users/1/edit', :method => :get))
|
|
|
|
|
|
|
|
assert_equal({:controller => 'admin/posts', :action => 'index'}, @routes.recognize_path('/admin/posts', :method => :get))
|
|
|
|
assert_equal({:controller => 'admin/posts', :action => 'new'}, @routes.recognize_path('/admin/posts/new', :method => :get))
|
|
|
|
|
|
|
|
assert_equal({:controller => 'api/users', :action => 'index'}, @routes.recognize_path('/api', :method => :get))
|
|
|
|
assert_equal({:controller => 'api/users', :action => 'index'}, @routes.recognize_path('/api/', :method => :get))
|
|
|
|
|
2010-08-06 04:27:41 -04:00
|
|
|
assert_equal({:controller => 'posts', :action => 'show_date', :year => '2009', :month => nil, :day => nil }, @routes.recognize_path('/blog/2009', :method => :get))
|
|
|
|
assert_equal({:controller => 'posts', :action => 'show_date', :year => '2009', :month => '01', :day => nil }, @routes.recognize_path('/blog/2009/01', :method => :get))
|
2009-10-20 10:52:59 -04:00
|
|
|
assert_equal({:controller => 'posts', :action => 'show_date', :year => '2009', :month => '01', :day => '01'}, @routes.recognize_path('/blog/2009/01/01', :method => :get))
|
|
|
|
|
|
|
|
assert_equal({:controller => 'archive', :action => 'index', :year => '2010'}, @routes.recognize_path('/archive/2010'))
|
|
|
|
assert_equal({:controller => 'archive', :action => 'index'}, @routes.recognize_path('/archive'))
|
|
|
|
|
|
|
|
assert_equal({:controller => 'people', :action => 'index'}, @routes.recognize_path('/people', :method => :get))
|
|
|
|
assert_equal({:controller => 'people', :action => 'index', :format => 'xml'}, @routes.recognize_path('/people.xml', :method => :get))
|
|
|
|
assert_equal({:controller => 'people', :action => 'create'}, @routes.recognize_path('/people', :method => :post))
|
|
|
|
assert_equal({:controller => 'people', :action => 'new'}, @routes.recognize_path('/people/new', :method => :get))
|
|
|
|
assert_equal({:controller => 'people', :action => 'show', :id => '1'}, @routes.recognize_path('/people/1', :method => :get))
|
|
|
|
assert_equal({:controller => 'people', :action => 'show', :id => '1', :format => 'xml'}, @routes.recognize_path('/people/1.xml', :method => :get))
|
|
|
|
assert_equal({:controller => 'people', :action => 'update', :id => '1'}, @routes.recognize_path('/people/1', :method => :put))
|
|
|
|
assert_equal({:controller => 'people', :action => 'destroy', :id => '1'}, @routes.recognize_path('/people/1', :method => :delete))
|
|
|
|
assert_equal({:controller => 'people', :action => 'edit', :id => '1'}, @routes.recognize_path('/people/1/edit', :method => :get))
|
|
|
|
assert_equal({:controller => 'people', :action => 'edit', :id => '1', :format => 'xml'}, @routes.recognize_path('/people/1/edit.xml', :method => :get))
|
|
|
|
|
|
|
|
assert_equal({:controller => 'symbols', :action => 'show', :name => :as_symbol}, @routes.recognize_path('/symbols'))
|
|
|
|
assert_equal({:controller => 'foo', :action => 'id_default', :id => '1'}, @routes.recognize_path('/id_default/1'))
|
|
|
|
assert_equal({:controller => 'foo', :action => 'id_default', :id => '2'}, @routes.recognize_path('/id_default/2'))
|
2010-08-06 04:27:41 -04:00
|
|
|
assert_equal({:controller => 'foo', :action => 'id_default', :id => 1 }, @routes.recognize_path('/id_default'))
|
2009-10-20 10:52:59 -04:00
|
|
|
assert_equal({:controller => 'foo', :action => 'get_or_post'}, @routes.recognize_path('/get_or_post', :method => :get))
|
|
|
|
assert_equal({:controller => 'foo', :action => 'get_or_post'}, @routes.recognize_path('/get_or_post', :method => :post))
|
2013-12-19 06:03:39 -05:00
|
|
|
assert_raise(ActionController::RoutingError) { @routes.recognize_path('/get_or_post', :method => :put) }
|
|
|
|
assert_raise(ActionController::RoutingError) { @routes.recognize_path('/get_or_post', :method => :delete) }
|
2009-10-20 10:52:59 -04:00
|
|
|
|
|
|
|
assert_equal({:controller => 'posts', :action => 'index', :optional => 'bar'}, @routes.recognize_path('/optional/bar'))
|
2013-12-19 06:03:39 -05:00
|
|
|
assert_raise(ActionController::RoutingError) { @routes.recognize_path('/optional') }
|
2009-10-20 10:52:59 -04:00
|
|
|
|
|
|
|
assert_equal({:controller => 'posts', :action => 'show', :id => '1', :ws => true}, @routes.recognize_path('/ws/posts/show/1', :method => :get))
|
|
|
|
assert_equal({:controller => 'posts', :action => 'list', :ws => true}, @routes.recognize_path('/ws/posts/list', :method => :get))
|
|
|
|
assert_equal({:controller => 'posts', :action => 'index', :ws => true}, @routes.recognize_path('/ws/posts', :method => :get))
|
|
|
|
|
|
|
|
assert_equal({:controller => 'account', :action => 'subscription'}, @routes.recognize_path('/account', :method => :get))
|
|
|
|
assert_equal({:controller => 'account', :action => 'subscription'}, @routes.recognize_path('/account/subscription', :method => :get))
|
|
|
|
assert_equal({:controller => 'account', :action => 'billing'}, @routes.recognize_path('/account/billing', :method => :get))
|
|
|
|
|
|
|
|
assert_equal({:page_id => '1', :controller => 'notes', :action => 'index'}, @routes.recognize_path('/pages/1/notes', :method => :get))
|
|
|
|
assert_equal({:page_id => '1', :controller => 'notes', :action => 'list'}, @routes.recognize_path('/pages/1/notes/list', :method => :get))
|
|
|
|
assert_equal({:page_id => '1', :controller => 'notes', :action => 'show', :id => '2'}, @routes.recognize_path('/pages/1/notes/show/2', :method => :get))
|
|
|
|
|
|
|
|
assert_equal({:controller => 'posts', :action => 'ping'}, @routes.recognize_path('/posts/ping', :method => :get))
|
|
|
|
assert_equal({:controller => 'posts', :action => 'index'}, @routes.recognize_path('/posts', :method => :get))
|
|
|
|
assert_equal({:controller => 'posts', :action => 'index'}, @routes.recognize_path('/posts/index', :method => :get))
|
|
|
|
assert_equal({:controller => 'posts', :action => 'show'}, @routes.recognize_path('/posts/show', :method => :get))
|
|
|
|
assert_equal({:controller => 'posts', :action => 'show', :id => '1'}, @routes.recognize_path('/posts/show/1', :method => :get))
|
|
|
|
assert_equal({:controller => 'posts', :action => 'create'}, @routes.recognize_path('/posts/create', :method => :post))
|
|
|
|
|
|
|
|
assert_equal({:controller => 'geocode', :action => 'show', :postalcode => 'hx12-1az'}, @routes.recognize_path('/ignorecase/geocode/hx12-1az'))
|
|
|
|
assert_equal({:controller => 'geocode', :action => 'show', :postalcode => 'hx12-1AZ'}, @routes.recognize_path('/ignorecase/geocode/hx12-1AZ'))
|
|
|
|
assert_equal({:controller => 'geocode', :action => 'show', :postalcode => '12345-1234'}, @routes.recognize_path('/extended/geocode/12345-1234'))
|
|
|
|
assert_equal({:controller => 'geocode', :action => 'show', :postalcode => '12345'}, @routes.recognize_path('/extended/geocode/12345'))
|
|
|
|
|
2010-08-06 04:27:41 -04:00
|
|
|
assert_equal({:controller => 'news', :action => 'index' }, @routes.recognize_path('/', :method => :get))
|
2009-10-20 10:52:59 -04:00
|
|
|
assert_equal({:controller => 'news', :action => 'index', :format => 'rss'}, @routes.recognize_path('/news.rss', :method => :get))
|
|
|
|
|
|
|
|
assert_raise(ActionController::RoutingError) { @routes.recognize_path('/none', :method => :get) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_generate_extras
|
|
|
|
assert_equal ['/people', []], @routes.generate_extras(:controller => 'people')
|
|
|
|
assert_equal ['/people', [:foo]], @routes.generate_extras(:controller => 'people', :foo => 'bar')
|
|
|
|
assert_equal ['/people', []], @routes.generate_extras(:controller => 'people', :action => 'index')
|
|
|
|
assert_equal ['/people', [:foo]], @routes.generate_extras(:controller => 'people', :action => 'index', :foo => 'bar')
|
|
|
|
assert_equal ['/people/new', []], @routes.generate_extras(:controller => 'people', :action => 'new')
|
|
|
|
assert_equal ['/people/new', [:foo]], @routes.generate_extras(:controller => 'people', :action => 'new', :foo => 'bar')
|
|
|
|
assert_equal ['/people/1', []], @routes.generate_extras(:controller => 'people', :action => 'show', :id => '1')
|
|
|
|
assert_equal ['/people/1', [:bar, :foo]], sort_extras!(@routes.generate_extras(:controller => 'people', :action => 'show', :id => '1', :foo => '2', :bar => '3'))
|
|
|
|
assert_equal ['/people', [:person]], @routes.generate_extras(:controller => 'people', :action => 'create', :person => { :first_name => 'Josh', :last_name => 'Peek' })
|
|
|
|
assert_equal ['/people', [:people]], @routes.generate_extras(:controller => 'people', :action => 'create', :people => ['Josh', 'Dave'])
|
|
|
|
|
|
|
|
assert_equal ['/posts/show/1', []], @routes.generate_extras(:controller => 'posts', :action => 'show', :id => '1')
|
|
|
|
assert_equal ['/posts/show/1', [:bar, :foo]], sort_extras!(@routes.generate_extras(:controller => 'posts', :action => 'show', :id => '1', :foo => '2', :bar => '3'))
|
|
|
|
assert_equal ['/posts', []], @routes.generate_extras(:controller => 'posts', :action => 'index')
|
|
|
|
assert_equal ['/posts', [:foo]], @routes.generate_extras(:controller => 'posts', :action => 'index', :foo => 'bar')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extras
|
|
|
|
params = {:controller => 'people'}
|
|
|
|
assert_equal [], @routes.extra_keys(params)
|
|
|
|
assert_equal({:controller => 'people'}, params)
|
|
|
|
|
|
|
|
params = {:controller => 'people', :foo => 'bar'}
|
|
|
|
assert_equal [:foo], @routes.extra_keys(params)
|
|
|
|
assert_equal({:controller => 'people', :foo => 'bar'}, params)
|
|
|
|
|
|
|
|
params = {:controller => 'people', :action => 'create', :person => { :name => 'Josh'}}
|
|
|
|
assert_equal [:person], @routes.extra_keys(params)
|
|
|
|
assert_equal({:controller => 'people', :action => 'create', :person => { :name => 'Josh'}}, params)
|
|
|
|
end
|
|
|
|
|
2012-06-05 11:59:01 -04:00
|
|
|
def test_unicode_path
|
|
|
|
assert_equal({:controller => 'news', :action => 'index'}, @routes.recognize_path(URI.parser.escape('こんにちは/世界'), :method => :get))
|
|
|
|
end
|
|
|
|
|
2013-09-18 13:40:03 -04:00
|
|
|
def test_downcased_unicode_path
|
|
|
|
assert_equal({:controller => 'news', :action => 'index'}, @routes.recognize_path(URI.parser.escape('こんにちは/世界').downcase, :method => :get))
|
|
|
|
end
|
|
|
|
|
2009-10-20 10:52:59 -04:00
|
|
|
private
|
|
|
|
def sort_extras!(extras)
|
|
|
|
if extras.length == 2
|
|
|
|
extras[1].sort! { |a, b| a.to_s <=> b.to_s }
|
|
|
|
end
|
|
|
|
extras
|
|
|
|
end
|
|
|
|
end
|