提交 f2c707a6 编写于 作者: A Andrew White

Merge pull request #23980 from rails/deprecate-controller-action-segments

Deprecate :controller and :action path parameters
......@@ -513,6 +513,15 @@ def add_route(mapping, path_ast, name, anchor)
route = @set.add_route(name, mapping)
named_routes[name] = route if name
if route.segment_keys.include?(:controller)
ActiveSupport::Deprecation.warn("Using a dynamic :controller segment in a route is deprecated and will be remove in Rails 5.1")
end
if route.segment_keys.include?(:action)
ActiveSupport::Deprecation.warn("Using a dynamic :action segment in a route is deprecated and will be remove in Rails 5.1")
end
route
end
......
......@@ -69,7 +69,9 @@ def root; end;
SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
SharedTestRoutes.draw do
get ':controller(/:action)'
ActiveSupport::Deprecation.silence do
get ':controller(/:action)'
end
end
module ActionDispatch
......@@ -118,7 +120,9 @@ def self.build_app(routes = nil)
self.app = build_app
app.routes.draw do
get ':controller(/:action)'
ActiveSupport::Deprecation.silence do
get ':controller(/:action)'
end
end
class DeadEndRoutes < ActionDispatch::Routing::RouteSet
......
......@@ -177,7 +177,10 @@ def test_assert_redirect_to_named_route_failure
set.draw do
get 'route_one', :to => 'action_pack_assertions#nothing', :as => :route_one
get 'route_two', :to => 'action_pack_assertions#nothing', :id => 'two', :as => :route_two
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
process :redirect_to_named_route
assert_raise(ActiveSupport::TestCase::Assertion) do
......@@ -201,7 +204,10 @@ def test_assert_redirect_to_nested_named_route
with_routing do |set|
set.draw do
get 'admin/inner_module', :to => 'admin/inner_module#index', :as => :admin_inner_module
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
process :redirect_to_index
# redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
......@@ -215,7 +221,10 @@ def test_assert_redirected_to_top_level_named_route_from_nested_controller
with_routing do |set|
set.draw do
get '/action_pack_assertions/:id', :to => 'action_pack_assertions#index', :as => :top_level
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
process :redirect_to_top_level_named_route
# assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return
......@@ -231,7 +240,10 @@ def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in
set.draw do
# this controller exists in the admin namespace as well which is the only difference from previous test
get '/user/:id', :to => 'user#index', :as => :top_level
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
process :redirect_to_top_level_named_route
# assert_redirected_to top_level_url('foo') would pass because of exact match early return
......
......@@ -175,7 +175,10 @@ def test_url_options_override
with_routing do |set|
set.draw do
get 'from_view', :to => 'url_options#from_view', :as => :from_view
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
get :from_view, params: { route: "from_view_url" }
......@@ -209,7 +212,10 @@ def test_default_url_options_override
with_routing do |set|
set.draw do
get 'from_view', :to => 'default_url_options#from_view', :as => :from_view
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
get :from_view, params: { route: "from_view_url" }
......@@ -226,7 +232,10 @@ def test_default_url_options_are_used_in_non_positional_parameters
scope("/:locale") do
resources :descriptions
end
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
get :from_view, params: { route: "description_path(1)" }
......
......@@ -323,7 +323,9 @@ def get(path, *args)
def with_test_route_set
with_routing do |set|
set.draw do
get ':action', :to => FlashIntegrationTest::TestController
ActiveSupport::Deprecation.silence do
get ':action', :to => FlashIntegrationTest::TestController
end
end
@app = self.class.build_app(set) do |middleware|
......
......@@ -730,8 +730,10 @@ def with_test_route_set
set.draw do
get 'moved' => redirect('/method')
match ':action', :to => controller, :via => [:get, :post], :as => :action
get 'get/:action', :to => controller, :as => :get_action
ActiveSupport::Deprecation.silence do
match ':action', :to => controller, :via => [:get, :post], :as => :action
get 'get/:action', :to => controller, :as => :get_action
end
end
self.singleton_class.include(set.url_helpers)
......@@ -1105,7 +1107,12 @@ def ok
def test_request
with_routing do |routes|
routes.draw { get ':action' => FooController }
routes.draw do
ActiveSupport::Deprecation.silence do
get ':action' => FooController
end
end
get '/ok'
assert_response 200
......@@ -1173,7 +1180,11 @@ def test_registering_custom_encoder
def test_parsed_body_without_as_option
with_routing do |routes|
routes.draw { get ':action' => FooController }
routes.draw do
ActiveSupport::Deprecation.silence do
get ':action' => FooController
end
end
get '/foos_json.json', params: { foo: 'heyo' }
......@@ -1184,7 +1195,11 @@ def test_parsed_body_without_as_option
private
def post_to_foos(as:)
with_routing do |routes|
routes.draw { post ':action' => FooController }
routes.draw do
ActiveSupport::Deprecation.silence do
post ':action' => FooController
end
end
post "/foos_#{as}", params: { foo: 'fighters' }, as: as
......
......@@ -43,7 +43,9 @@ class ExplicitContentTypeTest < Rack::TestCase
test "default response is text/plain and UTF8" do
with_routing do |set|
set.draw do
get ':controller', :action => 'index'
ActiveSupport::Deprecation.silence do
get ':controller', :action => 'index'
end
end
get "/content_type/base"
......
......@@ -85,7 +85,7 @@ class RenderBodyTest < Rack::TestCase
test "rendering body from an action with default options renders the body with the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
set.draw { ActiveSupport::Deprecation.silence { get ':controller', action: 'index' } }
get "/render_body/simple"
assert_body "hello david"
......@@ -95,7 +95,7 @@ class RenderBodyTest < Rack::TestCase
test "rendering body from an action with default options renders the body without the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
set.draw { ActiveSupport::Deprecation.silence { get ':controller', action: 'index' } }
get "/render_body/with_layout"
......
......@@ -88,7 +88,7 @@ class RenderHtmlTest < Rack::TestCase
test "rendering text from an action with default options renders the text with the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
set.draw { ActiveSupport::Deprecation.silence { get ':controller', action: 'index' } }
get "/render_html/simple"
assert_body "hello david"
......@@ -98,7 +98,7 @@ class RenderHtmlTest < Rack::TestCase
test "rendering text from an action with default options renders the text without the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
set.draw { ActiveSupport::Deprecation.silence { get ':controller', action: 'index' } }
get "/render_html/with_layout"
......
......@@ -80,7 +80,7 @@ class RenderPlainTest < Rack::TestCase
test "rendering text from an action with default options renders the text with the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
set.draw { ActiveSupport::Deprecation.silence { get ':controller', action: 'index' } }
get "/render_plain/simple"
assert_body "hello david"
......@@ -90,7 +90,7 @@ class RenderPlainTest < Rack::TestCase
test "rendering text from an action with default options renders the text without the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
set.draw { ActiveSupport::Deprecation.silence { get ':controller', action: 'index' } }
get "/render_plain/with_layout"
......
......@@ -177,7 +177,7 @@ def with_custom_layout
class TestWithLayout < Rack::TestCase
test "rendering with implicit layout" do
with_routing do |set|
set.draw { get ':controller', :action => :index }
set.draw { ActiveSupport::Deprecation.silence { get ':controller', :action => :index } }
get "/render_template/with_layout"
......
......@@ -57,7 +57,9 @@ class RenderTest < Rack::TestCase
test "render with blank" do
with_routing do |set|
set.draw do
get ":controller", :action => 'index'
ActiveSupport::Deprecation.silence do
get ":controller", :action => 'index'
end
end
get "/render/blank_render"
......@@ -70,7 +72,9 @@ class RenderTest < Rack::TestCase
test "rendering more than once raises an exception" do
with_routing do |set|
set.draw do
get ":controller", :action => 'index'
ActiveSupport::Deprecation.silence do
get ":controller", :action => 'index'
end
end
assert_raises(AbstractController::DoubleRenderError) do
......
......@@ -83,7 +83,7 @@ class RenderTextTest < Rack::TestCase
test "rendering text from an action with default options renders the text with the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
set.draw { ActiveSupport::Deprecation.silence { get ':controller', action: 'index' } }
ActiveSupport::Deprecation.silence do
get "/render_text/simple"
......@@ -96,7 +96,7 @@ class RenderTextTest < Rack::TestCase
test "rendering text from an action with default options renders the text without the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
set.draw { ActiveSupport::Deprecation.silence { get ':controller', action: 'index' } }
ActiveSupport::Deprecation.silence do
get "/render_text/with_layout"
......
......@@ -286,7 +286,10 @@ def test_redirect_to_record
with_routing do |set|
set.draw do
resources :workshops
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
get :redirect_to_existing_record
......@@ -328,7 +331,9 @@ def test_redirect_to_with_block_and_assigns
def test_redirect_to_with_block_and_accepted_options
with_routing do |set|
set.draw do
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
get :redirect_to_with_block_and_options
......
......@@ -615,7 +615,10 @@ def test_head_with_location_object
with_routing do |set|
set.draw do
resources :customers
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
get :head_with_location_object
......
......@@ -72,7 +72,10 @@ def test_rendering_with_object_location_should_set_header_with_url_for
with_routing do |set|
set.draw do
resources :customers
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
get :render_with_object_location
......
......@@ -167,7 +167,9 @@ def setup
@request.delete_header 'PATH_INFO'
@routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
r.draw do
get ':controller(/:action(/:id))'
ActiveSupport::Deprecation.silence do
get ':controller(/:action(/:id))'
end
end
end
end
......@@ -672,7 +674,10 @@ def test_array_path_parameter_handled_properly
with_routing do |set|
set.draw do
get 'file/*path', to: 'test_case_test/test#test_params'
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
get :test_params, params: { path: ['hello', 'world'] }
......@@ -1008,7 +1013,9 @@ def setup
@request.env['PATH_INFO'] = nil
@routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
r.draw do
get ':controller(/:action(/:id))'
ActiveSupport::Deprecation.silence do
get ':controller(/:action(/:id))'
end
end
end
end
......@@ -1135,7 +1142,9 @@ def index
@routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
r.draw do
get ':controller(/:action(/:id))'
ActiveSupport::Deprecation.silence do
get ':controller(/:action(/:id))'
end
end
end
end
......
......@@ -52,12 +52,15 @@ class URLForIntegrationTest < ActiveSupport::TestCase
get 'news(.:format)' => "news#index"
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'
get ':controller(/:action(/:id))(.:format)'
ActiveSupport::Deprecation.silence {
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'
get ':controller(/:action(/:id))(.:format)'
}
root :to => "news#index"
}
......
......@@ -4,7 +4,13 @@ module AbstractController
module Testing
class UrlForTest < ActionController::TestCase
class W
include ActionDispatch::Routing::RouteSet.new.tap { |r| r.draw { get ':controller(/:action(/:id(.:format)))' } }.url_helpers
include ActionDispatch::Routing::RouteSet.new.tap { |r|
r.draw {
ActiveSupport::Deprecation.silence {
get ':controller(/:action(/:id(.:format)))'
}
}
}.url_helpers
end
def teardown
......@@ -260,7 +266,7 @@ def test_relative_url_root_is_respected_with_environment_variable
w = Class.new {
config = ActionDispatch::Routing::RouteSet::Config.new '/subdir'
r = ActionDispatch::Routing::RouteSet.new(config)
r.draw { get ':controller(/:action(/:id(.:format)))' }
r.draw { ActiveSupport::Deprecation.silence { get ':controller(/:action(/:id(.:format)))' } }
include r.url_helpers
}
add_host!(w)
......@@ -315,7 +321,10 @@ def test_only_path
with_routing do |set|
set.draw do
get 'home/sweet/home/:user', :to => 'home#index', :as => :home
get ':controller/:action/:id'
ActiveSupport::Deprecation.silence do
get ':controller/:action/:id'
end
end
# We need to create a new class in order to install the new named route.
......
......@@ -20,7 +20,9 @@ def setup
@rewriter = Rewriter.new(@request) #.new(@request, @params)
@routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
r.draw do
get ':controller(/:action(/:id))'
ActiveSupport::Deprecation.silence do
get ':controller(/:action(/:id))'
end
end
end
end
......
......@@ -103,7 +103,9 @@ def assert_parses(expected, actual, headers = {})
def with_test_routing
with_routing do |set|
set.draw do
post ':action', :to => ::JsonParamsParsingTest::TestController
ActiveSupport::Deprecation.silence do
post ':action', :to => ::JsonParamsParsingTest::TestController
end
end
yield
end
......@@ -191,7 +193,9 @@ def assert_parses(expected, actual, headers = {})
def with_test_routing(controller)
with_routing do |set|
set.draw do
post ':action', :to => controller
ActiveSupport::Deprecation.silence do
post ':action', :to => controller
end
end
yield
end
......
......@@ -159,7 +159,9 @@ def teardown
test "does not raise EOFError on GET request with multipart content-type" do
with_routing do |set|
set.draw do
get ':action', controller: 'multipart_params_parsing_test/test'
ActiveSupport::Deprecation.silence do
get ':action', controller: 'multipart_params_parsing_test/test'
end
end
headers = { "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x" }
get "/parse", headers: headers
......@@ -188,7 +190,9 @@ def parse_multipart(name)
def with_test_routing
with_routing do |set|
set.draw do
post ':action', :controller => 'multipart_params_parsing_test/test'
ActiveSupport::Deprecation.silence do
post ':action', :controller => 'multipart_params_parsing_test/test'
end
end
yield
end
......
......@@ -144,7 +144,9 @@ def test_array_parses_without_nil
test "ambiguous query string returns a bad request" do
with_routing do |set|
set.draw do
get ':action', :to => ::QueryStringParsingTest::TestController
ActiveSupport::Deprecation.silence do
get ':action', :to => ::QueryStringParsingTest::TestController
end
end
get "/parse", headers: { "QUERY_STRING" => "foo[]=bar&foo[4]=bar" }
......@@ -156,7 +158,9 @@ def test_array_parses_without_nil
def assert_parses(expected, actual)
with_routing do |set|
set.draw do
get ':action', :to => ::QueryStringParsingTest::TestController
ActiveSupport::Deprecation.silence do
get ':action', :to => ::QueryStringParsingTest::TestController
end
end
@app = self.class.build_app(set) do |middleware|
middleware.use(EarlyParse)
......
......@@ -140,7 +140,9 @@ def teardown
def with_test_routing
with_routing do |set|
set.draw do
post ':action', to: ::UrlEncodedParamsParsingTest::TestController
ActiveSupport::Deprecation.silence do
post ':action', to: ::UrlEncodedParamsParsingTest::TestController
end
end
yield
end
......
......@@ -133,7 +133,9 @@ def test_inspect_routes_shows_root_route
def test_inspect_routes_shows_dynamic_action_route
output = draw do
get 'api/:action' => 'api'
ActiveSupport::Deprecation.silence do
get 'api/:action' => 'api'
end
end
assert_equal [
......@@ -144,7 +146,9 @@ def test_inspect_routes_shows_dynamic_action_route
def test_inspect_routes_shows_controller_and_action_only_route
output = draw do
get ':controller/:action'
ActiveSupport::Deprecation.silence do
get ':controller/:action'
end
end
assert_equal [
......@@ -155,7 +159,9 @@ def test_inspect_routes_shows_controller_and_action_only_route
def test_inspect_routes_shows_controller_and_action_route_with_constraints
output = draw do
get ':controller(/:action(/:id))', :id => /\d+/
ActiveSupport::Deprecation.silence do
get ':controller(/:action(/:id))', :id => /\d+/
end
end
assert_equal [
......@@ -335,7 +341,9 @@ def test_routes_can_be_filtered_with_namespaced_controllers
def test_regression_route_with_controller_regexp
output = draw do
get ':controller(/:action)', controller: /api\/[^\/]+/, format: false
ActiveSupport::Deprecation.silence do
get ':controller(/:action)', controller: /api\/[^\/]+/, format: false
end
end
assert_equal ["Prefix Verb URI Pattern Controller#Action",
......
......@@ -116,7 +116,9 @@ def test_namespace_with_controller_segment
assert_raise(ArgumentError) do
draw do
namespace :admin do
get '/:controller(/:action(/:id(.:format)))'
ActiveSupport::Deprecation.silence do
get '/:controller(/:action(/:id(.:format)))'
end
end
end
end
......@@ -125,7 +127,9 @@ def test_namespace_with_controller_segment
def test_namespace_without_controller_segment
draw do
namespace :admin do
get 'hello/:controllers/:action'
ActiveSupport::Deprecation.silence do
get 'hello/:controllers/:action'
end
end
end
get '/admin/hello/foo/new'
......@@ -427,7 +431,10 @@ def test_global
get 'global/hide_notice'
get 'global/export', :action => :export, :as => :export_request
get '/export/:id/:file', :action => :export, :as => :export_download, :constraints => { :file => /.*/ }
get 'global/:action'
ActiveSupport::Deprecation.silence do
get 'global/:action'
end
end
end
......@@ -450,7 +457,9 @@ def test_global
def test_local
draw do
get "/local/:action", :controller => "local"
ActiveSupport::Deprecation.silence do
get "/local/:action", :controller => "local"
end
end
get '/local/dashboard'
......@@ -1506,7 +1515,9 @@ def test_match_shorthand_inside_nested_namespaces_and_scopes_with_controller
def test_not_matching_shorthand_with_dynamic_parameters
draw do
get ':controller/:action/admin'
ActiveSupport::Deprecation.silence do
get ':controller/:action/admin'
end
end
get '/finances/overview/admin'
......@@ -1542,7 +1553,9 @@ def test_dynamically_generated_helpers_on_collection_do_not_clobber_resources_ur
def test_scoped_controller_with_namespace_and_action
draw do
namespace :account do
get ':action/callback', :action => /twitter|github/, :controller => "callbacks", :as => :callback
ActiveSupport::Deprecation.silence do
get ':action/callback', :action => /twitter|github/, :controller => "callbacks", :as => :callback
end
end
end
......@@ -1837,7 +1850,9 @@ def test_symbol_scope
def test_url_generator_for_generic_route
draw do
get "whatever/:controller(/:action(/:id))"
ActiveSupport::Deprecation.silence do
get "whatever/:controller(/:action(/:id))"
end
end
get '/whatever/foo/bar'
......@@ -1849,7 +1864,9 @@ def test_url_generator_for_generic_route
def test_url_generator_for_namespaced_generic_route
draw do
get "whatever/:controller(/:action(/:id))", :id => /\d+/
ActiveSupport::Deprecation.silence do
get "whatever/:controller(/:action(/:id))", :id => /\d+/
end
end
get '/whatever/foo/bar/show'
......@@ -3124,12 +3141,6 @@ def test_controller_name_with_leading_slash_raise_error
draw { get '/api/feeds/:service', :to => '/api/feeds#show' }
end
assert_raise(ArgumentError) do
assert_deprecated do
draw { controller("/feeds") { get '/feeds/:service', :to => :show } }
end
end
assert_raise(ArgumentError) do
draw { resources :feeds, :controller => '/feeds' }
end
......@@ -3599,6 +3610,22 @@ def test_passing_action_parameters_to_url_helpers_is_allowed_if_parameters_are_p
assert_equal '/?id=1', root_path(params)
end
def test_dynamic_controller_segments_are_deprecated
assert_deprecated do
draw do
get '/:controller', action: 'index'
end
end
end
def test_dynamic_action_segments_are_deprecated
assert_deprecated do
draw do
get '/pages/:action', controller: 'pages'
end
end
end
private
def draw(&block)
......@@ -4122,7 +4149,11 @@ class TestOptimizedNamedRoutes < ActionDispatch::IntegrationTest
app.draw do
ok = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, []] }
get '/foo' => ok, as: :foo
get '/post(/:action(/:id))' => ok, as: :posts
ActiveSupport::Deprecation.silence do
get '/post(/:action(/:id))' => ok, as: :posts
end
get '/:foo/:foo_type/bars/:id' => ok, as: :bar
get '/projects/:id.:format' => ok, as: :project
get '/pages/:id' => ok, as: :page
......@@ -4292,11 +4323,16 @@ def show
test "invalid UTF-8 encoding returns a 400 Bad Request" do
with_routing do |set|
set.draw do
get "/bar/:id", :to => redirect("/foo/show/%{id}")
get "/foo/show(/:id)", :to => "test_invalid_urls/foo#show"
get "/foo(/:action(/:id))", :controller => "test_invalid_urls/foo"
get "/:controller(/:action(/:id))"
ActiveSupport::Deprecation.silence do
set.draw do
get "/bar/:id", :to => redirect("/foo/show/%{id}")
get "/foo/show(/:id)", :to => "test_invalid_urls/foo#show"
ActiveSupport::Deprecation.silence do
get "/foo(/:action(/:id))", :controller => "test_invalid_urls/foo"
get "/:controller(/:action(/:id))"
end
end
end
get "/%E2%EF%BF%BD%A6"
......@@ -4627,7 +4663,9 @@ def bar
Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do
get '/:controller(/:action)'
ActiveSupport::Deprecation.silence do
get '/:controller(/:action)'
end
end
APP = build_app Routes
......
......@@ -164,7 +164,9 @@ def test_prevents_session_fixation
def with_test_route_set
with_routing do |set|
set.draw do
get ':action', :to => ::CacheStoreTest::TestController
ActiveSupport::Deprecation.silence do
get ':action', :to => ::CacheStoreTest::TestController
end
end
@app = self.class.build_app(set) do |middleware|
......
......@@ -345,7 +345,9 @@ def get(path, *args)
def with_test_route_set(options = {})
with_routing do |set|
set.draw do
get ':action', :to => ::CookieStoreTest::TestController
ActiveSupport::Deprecation.silence do
get ':action', :to => ::CookieStoreTest::TestController
end
end
options = { :key => SessionKey }.merge!(options)
......
......@@ -187,7 +187,9 @@ def test_prevents_session_fixation
def with_test_route_set
with_routing do |set|
set.draw do
get ':action', :to => ::MemCacheStoreTest::TestController
ActiveSupport::Deprecation.silence do
get ':action', :to => ::MemCacheStoreTest::TestController
end
end
@app = self.class.build_app(set) do |middleware|
......
......@@ -3,7 +3,7 @@
module ActionDispatch
module Journey
class TestRouter < ActiveSupport::TestCase
attr_reader :routes, :mapper
attr_reader :mapper, :routes, :route_set, :router
def setup
@app = Routing::RouteSet::Dispatcher.new({})
......@@ -15,36 +15,36 @@ def setup
end
def test_dashes
mapper.get '/foo-bar-baz', to: 'foo#bar'
get '/foo-bar-baz', to: 'foo#bar'
env = rails_env 'PATH_INFO' => '/foo-bar-baz'
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
called = true
end
assert called
end
def test_unicode
mapper.get '/ほげ', to: 'foo#bar'
get '/ほげ', to: 'foo#bar'
#match the escaped version of /ほげ
env = rails_env 'PATH_INFO' => '/%E3%81%BB%E3%81%92'
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
called = true
end
assert called
end
def test_regexp_first_precedence
mapper.get "/whois/:domain", :domain => /\w+\.[\w\.]+/, to: "foo#bar"
mapper.get "/whois/:id(.:format)", to: "foo#baz"
get "/whois/:domain", :domain => /\w+\.[\w\.]+/, to: "foo#bar"
get "/whois/:id(.:format)", to: "foo#baz"
env = rails_env 'PATH_INFO' => '/whois/example.com'
list = []
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
list << r
end
assert_equal 2, list.length
......@@ -55,7 +55,7 @@ def test_regexp_first_precedence
end
def test_required_parts_verified_are_anchored
mapper.get "/foo/:id", :id => /\d/, anchor: false, to: "foo#bar"
get "/foo/:id", :id => /\d/, anchor: false, to: "foo#bar"
assert_raises(ActionController::UrlGenerationError) do
@formatter.generate(nil, { :controller => "foo", :action => "bar", :id => '10' }, { })
......@@ -63,7 +63,7 @@ def test_required_parts_verified_are_anchored
end
def test_required_parts_are_verified_when_building
mapper.get "/foo/:id", :id => /\d+/, anchor: false, to: "foo#bar"
get "/foo/:id", :id => /\d+/, anchor: false, to: "foo#bar"
path, _ = @formatter.generate(nil, { :controller => "foo", :action => "bar", :id => '10' }, { })
assert_equal '/foo/10', path
......@@ -74,7 +74,7 @@ def test_required_parts_are_verified_when_building
end
def test_only_required_parts_are_verified
mapper.get "/foo(/:id)", :id => /\d/, :to => "foo#bar"
get "/foo(/:id)", :id => /\d/, :to => "foo#bar"
path, _ = @formatter.generate(nil, { :controller => "foo", :action => "bar", :id => '10' }, { })
assert_equal '/foo/10', path
......@@ -88,8 +88,7 @@ def test_only_required_parts_are_verified
def test_knows_what_parts_are_missing_from_named_route
route_name = "gorby_thunderhorse"
mapper = ActionDispatch::Routing::Mapper.new @route_set
mapper.get "/foo/:id", :as => route_name, :id => /\d+/, :to => "foo#bar"
get "/foo/:id", :as => route_name, :id => /\d+/, :to => "foo#bar"
error = assert_raises(ActionController::UrlGenerationError) do
@formatter.generate(route_name, { }, { })
......@@ -109,19 +108,16 @@ def test_does_not_include_missing_keys_message
end
def test_X_Cascade
mapper.get "/messages(.:format)", to: "foo#bar"
resp = @router.serve(rails_env({ 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/lol' }))
get "/messages(.:format)", to: "foo#bar"
resp = router.serve(rails_env({ 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/lol' }))
assert_equal ['Not Found'], resp.last
assert_equal 'pass', resp[1]['X-Cascade']
assert_equal 404, resp.first
end
def test_clear_trailing_slash_from_script_name_on_root_unanchored_routes
route_set = Routing::RouteSet.new
mapper = Routing::Mapper.new route_set
app = lambda { |env| [200, {}, ['success!']] }
mapper.get '/weblog', :to => app
get '/weblog', :to => app
env = rack_env('SCRIPT_NAME' => '', 'PATH_INFO' => '/weblog')
resp = route_set.call env
......@@ -130,38 +126,38 @@ def test_clear_trailing_slash_from_script_name_on_root_unanchored_routes
end
def test_defaults_merge_correctly
mapper.get '/foo(/:id)', to: "foo#bar", id: nil
get '/foo(/:id)', to: "foo#bar", id: nil
env = rails_env 'PATH_INFO' => '/foo/10'
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
assert_equal({:id => '10', :controller => "foo", :action => "bar"}, params)
end
env = rails_env 'PATH_INFO' => '/foo'
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
assert_equal({:id => nil, :controller => "foo", :action => "bar"}, params)
end
end
def test_recognize_with_unbound_regexp
mapper.get "/foo", anchor: false, to: "foo#bar"
get "/foo", anchor: false, to: "foo#bar"
env = rails_env 'PATH_INFO' => '/foo/bar'
@router.recognize(env) { |*_| }
router.recognize(env) { |*_| }
assert_equal '/foo', env.env['SCRIPT_NAME']
assert_equal '/bar', env.env['PATH_INFO']
end
def test_bound_regexp_keeps_path_info
mapper.get "/foo", to: "foo#bar"
get "/foo", to: "foo#bar"
env = rails_env 'PATH_INFO' => '/foo'
before = env.env['SCRIPT_NAME']
@router.recognize(env) { |*_| }
router.recognize(env) { |*_| }
assert_equal before, env.env['SCRIPT_NAME']
assert_equal '/foo', env.env['PATH_INFO']
......@@ -174,41 +170,41 @@ def test_path_not_found
"/messages/:id/edit(.:format)",
"/messages/:id(.:format)"
].each do |path|
mapper.get path, to: "foo#bar"
get path, to: "foo#bar"
end
env = rails_env 'PATH_INFO' => '/messages/unknown/path'
yielded = false
@router.recognize(env) do |*whatever|
router.recognize(env) do |*whatever|
yielded = true
end
assert_not yielded
end
def test_required_part_in_recall
mapper.get "/messages/:a/:b", to: "foo#bar"
get "/messages/:a/:b", to: "foo#bar"
path, _ = @formatter.generate(nil, { :controller => "foo", :action => "bar", :a => 'a' }, { :b => 'b' })
assert_equal "/messages/a/b", path
end
def test_splat_in_recall
mapper.get "/*path", to: "foo#bar"
get "/*path", to: "foo#bar"
path, _ = @formatter.generate(nil, { :controller => "foo", :action => "bar" }, { :path => 'b' })
assert_equal "/b", path
end
def test_recall_should_be_used_when_scoring
mapper.get "/messages/:action(/:id(.:format))", to: 'foo#bar'
mapper.get "/messages/:id(.:format)", to: 'bar#baz'
get "/messages/:action(/:id(.:format))", to: 'foo#bar'
get "/messages/:id(.:format)", to: 'bar#baz'
path, _ = @formatter.generate(nil, { :controller => "foo", :id => 10 }, { :action => 'index' })
assert_equal "/messages/index/10", path
end
def test_nil_path_parts_are_ignored
mapper.get "/:controller(/:action(.:format))", to: "tasks#lol"
get "/:controller(/:action(.:format))", to: "tasks#lol"
params = { :controller => "tasks", :format => nil }
extras = { :action => 'lol' }
......@@ -220,14 +216,14 @@ def test_nil_path_parts_are_ignored
def test_generate_slash
params = [ [:controller, "tasks"],
[:action, "show"] ]
mapper.get "/", Hash[params]
get "/", Hash[params]
path, _ = @formatter.generate(nil, Hash[params], {})
assert_equal '/', path
end
def test_generate_calls_param_proc
mapper.get '/:controller(/:action)', to: "foo#bar"
get '/:controller(/:action)', to: "foo#bar"
parameterized = []
params = [ [:controller, "tasks"],
......@@ -243,7 +239,7 @@ def test_generate_calls_param_proc
end
def test_generate_id
mapper.get '/:controller(/:action)', to: 'foo#bar'
get '/:controller(/:action)', to: 'foo#bar'
path, params = @formatter.generate(
nil, {:id=>1, :controller=>"tasks", :action=>"show"}, {})
......@@ -252,7 +248,7 @@ def test_generate_id
end
def test_generate_escapes
mapper.get '/:controller(/:action)', to: "foo#bar"
get '/:controller(/:action)', to: "foo#bar"
path, _ = @formatter.generate(nil,
{ :controller => "tasks",
......@@ -262,7 +258,7 @@ def test_generate_escapes
end
def test_generate_escapes_with_namespaced_controller
mapper.get '/:controller(/:action)', to: "foo#bar"
get '/:controller(/:action)', to: "foo#bar"
path, _ = @formatter.generate(
nil, { :controller => "admin/tasks",
......@@ -272,7 +268,7 @@ def test_generate_escapes_with_namespaced_controller
end
def test_generate_extra_params
mapper.get '/:controller(/:action)', to: "foo#bar"
get '/:controller(/:action)', to: "foo#bar"
path, params = @formatter.generate(
nil, { :id => 1,
......@@ -285,7 +281,7 @@ def test_generate_extra_params
end
def test_generate_missing_keys_no_matches_different_format_keys
mapper.get '/:controller/:action/:name', to: "foo#bar"
get '/:controller/:action/:name', to: "foo#bar"
primarty_parameters = {
:id => 1,
:controller => "tasks",
......@@ -311,7 +307,7 @@ def test_generate_missing_keys_no_matches_different_format_keys
end
def test_generate_uses_recall_if_needed
mapper.get '/:controller(/:action(/:id))', to: "foo#bar"
get '/:controller(/:action(/:id))', to: "foo#bar"
path, params = @formatter.generate(
nil,
......@@ -322,7 +318,7 @@ def test_generate_uses_recall_if_needed
end
def test_generate_with_name
mapper.get '/:controller(/:action)', to: 'foo#bar', as: 'tasks'
get '/:controller(/:action)', to: 'foo#bar', as: 'tasks'
path, params = @formatter.generate(
"tasks",
......@@ -338,13 +334,13 @@ def test_generate_with_name
'/content/show/10' => { :controller => 'content', :action => 'show', :id => "10" },
}.each do |request_path, expected|
define_method("test_recognize_#{expected.keys.map(&:to_s).join('_')}") do
mapper.get "/:controller(/:action(/:id))", to: 'foo#bar'
get "/:controller(/:action(/:id))", to: 'foo#bar'
route = @routes.first
env = rails_env 'PATH_INFO' => request_path
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
assert_equal route, r
assert_equal({ :action => "bar" }.merge(expected), params)
called = true
......@@ -359,13 +355,13 @@ def test_generate_with_name
:splat => ['/segment/a/b%20c+d', { :segment => 'segment', :splat => 'a/b c+d' }]
}.each do |name, (request_path, expected)|
define_method("test_recognize_#{name}") do
mapper.get '/:segment/*splat', to: 'foo#bar'
get '/:segment/*splat', to: 'foo#bar'
env = rails_env 'PATH_INFO' => request_path
called = false
route = @routes.first
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
assert_equal route, r
assert_equal(expected.merge(:controller=>"foo", :action=>"bar"), params)
called = true
......@@ -376,7 +372,7 @@ def test_generate_with_name
end
def test_namespaced_controller
mapper.get "/:controller(/:action(/:id))", { :controller => /.+?/ }
get "/:controller(/:action(/:id))", { :controller => /.+?/ }
route = @routes.first
env = rails_env 'PATH_INFO' => '/admin/users/show/10'
......@@ -387,7 +383,7 @@ def test_namespaced_controller
:id => '10'
}
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
assert_equal route, r
assert_equal(expected, params)
called = true
......@@ -396,13 +392,13 @@ def test_namespaced_controller
end
def test_recognize_literal
mapper.get "/books(/:action(.:format))", controller: "books"
get "/books(/:action(.:format))", controller: "books"
route = @routes.first
env = rails_env 'PATH_INFO' => '/books/list.rss'
expected = { :controller => 'books', :action => 'list', :format => 'rss' }
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
assert_equal route, r
assert_equal(expected, params)
called = true
......@@ -412,7 +408,7 @@ def test_recognize_literal
end
def test_recognize_head_route
mapper.match "/books(/:action(.:format))", via: 'head', to: 'foo#bar'
match "/books(/:action(.:format))", via: 'head', to: 'foo#bar'
env = rails_env(
'PATH_INFO' => '/books/list.rss',
......@@ -420,7 +416,7 @@ def test_recognize_head_route
)
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
called = true
end
......@@ -428,13 +424,13 @@ def test_recognize_head_route
end
def test_recognize_head_request_as_get_route
mapper.get "/books(/:action(.:format))", to: 'foo#bar'
get "/books(/:action(.:format))", to: 'foo#bar'
env = rails_env 'PATH_INFO' => '/books/list.rss',
"REQUEST_METHOD" => "HEAD"
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
called = true
end
......@@ -442,13 +438,13 @@ def test_recognize_head_request_as_get_route
end
def test_recognize_cares_about_get_verbs
mapper.match "/books(/:action(.:format))", to: "foo#bar", via: :get
match "/books(/:action(.:format))", to: "foo#bar", via: :get
env = rails_env 'PATH_INFO' => '/books/list.rss',
"REQUEST_METHOD" => "POST"
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
called = true
end
......@@ -456,13 +452,13 @@ def test_recognize_cares_about_get_verbs
end
def test_recognize_cares_about_post_verbs
mapper.match "/books(/:action(.:format))", to: "foo#bar", via: :post
match "/books(/:action(.:format))", to: "foo#bar", via: :post
env = rails_env 'PATH_INFO' => '/books/list.rss',
"REQUEST_METHOD" => "POST"
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
called = true
end
......@@ -470,14 +466,14 @@ def test_recognize_cares_about_post_verbs
end
def test_multi_verb_recognition
mapper.match "/books(/:action(.:format))", to: "foo#bar", via: [:post, :get]
match "/books(/:action(.:format))", to: "foo#bar", via: [:post, :get]
%w( POST GET ).each do |verb|
env = rails_env 'PATH_INFO' => '/books/list.rss',
"REQUEST_METHOD" => verb
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
called = true
end
......@@ -488,7 +484,7 @@ def test_multi_verb_recognition
"REQUEST_METHOD" => 'PUT'
called = false
@router.recognize(env) do |r, params|
router.recognize(env) do |r, params|
called = true
end
......@@ -497,6 +493,18 @@ def test_multi_verb_recognition
private
def get *args
ActiveSupport::Deprecation.silence do
mapper.get(*args)
end
end
def match *args
ActiveSupport::Deprecation.silence do
mapper.match(*args)
end
end
def rails_env env, klass = ActionDispatch::Request
klass.new(rack_env(env))
end
......
......@@ -124,7 +124,7 @@ def build_app(options = {})
routes = File.read("#{app_path}/config/routes.rb")
if routes =~ /(\n\s*end\s*)\Z/
File.open("#{app_path}/config/routes.rb", 'w') do |f|
f.puts $` + "\nmatch ':controller(/:action(/:id))(.:format)', via: :all\n" + $1
f.puts $` + "\nActiveSupport::Deprecation.silence { match ':controller(/:action(/:id))(.:format)', via: :all }\n" + $1
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册