1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Kill RouteSet#recognize

This commit is contained in:
Joshua Peek 2009-12-10 23:45:04 -06:00
parent d1191507bc
commit 2f90d70049
3 changed files with 69 additions and 137 deletions

View file

@ -479,12 +479,6 @@ module ActionDispatch
end end
end end
def recognize(request)
params = recognize_path(request.path, extract_request_environment(request))
request.path_parameters = params.with_indifferent_access
"#{params[:controller].to_s.camelize}Controller".constantize
end
def recognize_path(path, environment = {}, rescue_error = true) def recognize_path(path, environment = {}, rescue_error = true)
method = (environment[:method] || "GET").to_s.upcase method = (environment[:method] || "GET").to_s.upcase
@ -499,12 +493,6 @@ module ActionDispatch
status, headers, body = call(env) status, headers, body = call(env)
body body
end end
# Subclasses and plugins may override this method to extract further attributes
# from the request, for use by route conditions and such.
def extract_request_environment(request)
{ :method => request.method }
end
end end
end end
end end

View file

@ -134,9 +134,11 @@ module ActionDispatch
# Assume given controller # Assume given controller
request = ActionController::TestRequest.new request = ActionController::TestRequest.new
request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
request.path = path request.path = path
params = ActionController::Routing::Routes.recognize_path(path, { :method => request.method })
request.path_parameters = params.with_indifferent_access
ActionController::Routing::Routes.recognize(request)
request request
end end
end end

View file

@ -554,10 +554,6 @@ class LegacyRouteSetTests < Test::Unit::TestCase
end end
def setup_request_method_routes_for(method) def setup_request_method_routes_for(method)
@request = ActionController::TestRequest.new
@request.env["REQUEST_METHOD"] = method
@request.request_uri = "/match"
rs.draw do |r| rs.draw do |r|
r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get } r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post } r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
@ -569,8 +565,8 @@ class LegacyRouteSetTests < Test::Unit::TestCase
%w(GET POST PUT DELETE).each do |request_method| %w(GET POST PUT DELETE).each do |request_method|
define_method("test_request_method_recognized_with_#{request_method}") do define_method("test_request_method_recognized_with_#{request_method}") do
setup_request_method_routes_for(request_method) setup_request_method_routes_for(request_method)
assert_nothing_raised { rs.recognize(@request) } params = rs.recognize_path("/match", :method => request_method)
assert_equal request_method.downcase, @request.path_parameters[:action] assert_equal request_method.downcase, params[:action]
end end
end end
@ -580,18 +576,11 @@ class LegacyRouteSetTests < Test::Unit::TestCase
r.connect '/match', :controller => 'books', :action => 'not_get_or_post' r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
end end
@request = ActionController::TestRequest.new params = rs.recognize_path("/match", :method => :post)
@request.env["REQUEST_METHOD"] = 'POST' assert_equal 'get_or_post', params[:action]
@request.request_uri = "/match"
assert_nothing_raised { rs.recognize(@request) }
assert_equal 'get_or_post', @request.path_parameters[:action]
# have to recreate or else the RouteSet uses a cached version: params = rs.recognize_path("/match", :method => :put)
@request = ActionController::TestRequest.new assert_equal 'not_get_or_post', params[:action]
@request.env["REQUEST_METHOD"] = 'PUT'
@request.request_uri = "/match"
assert_nothing_raised { rs.recognize(@request) }
assert_equal 'not_get_or_post', @request.path_parameters[:action]
end end
def test_subpath_recognized def test_subpath_recognized
@ -745,9 +734,7 @@ class RouteSetTest < ActiveSupport::TestCase
set.draw do |map| set.draw do |map|
map.connect '/users/index', :controller => :users, :action => :index map.connect '/users/index', :controller => :users, :action => :index
end end
@request = ActionController::TestRequest.new params = set.recognize_path('/users/index', :method => :get)
@request.request_uri = '/users/index'
assert_nothing_raised { set.recognize(@request) }
assert_equal 1, set.routes.size assert_equal 1, set.routes.size
end end
@ -980,55 +967,37 @@ class RouteSetTest < ActiveSupport::TestCase
end end
end end
request.request_uri = "/people" params = set.recognize_path("/people", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("index", params[:action])
assert_nothing_raised { set.recognize(request) }
assert_equal("index", request.path_parameters[:action])
request.recycle!
request.env["REQUEST_METHOD"] = "POST" params = set.recognize_path("/people", :method => :post)
assert_nothing_raised { set.recognize(request) } assert_equal("create", params[:action])
assert_equal("create", request.path_parameters[:action])
request.recycle!
request.env["REQUEST_METHOD"] = "PUT" params = set.recognize_path("/people", :method => :put)
assert_nothing_raised { set.recognize(request) } assert_equal("update", params[:action])
assert_equal("update", request.path_parameters[:action])
request.recycle!
assert_raise(ActionController::UnknownHttpMethod) { assert_raise(ActionController::NotImplemented) {
request.env["REQUEST_METHOD"] = "BACON" set.recognize_path("/people", :method => :bacon)
set.recognize(request)
} }
request.recycle!
request.request_uri = "/people/5" params = set.recognize_path("/people/5", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("show", params[:action])
assert_nothing_raised { set.recognize(request) } assert_equal("5", params[:id])
assert_equal("show", request.path_parameters[:action])
assert_equal("5", request.path_parameters[:id])
request.recycle!
request.env["REQUEST_METHOD"] = "PUT" params = set.recognize_path("/people/5", :method => :put)
assert_nothing_raised { set.recognize(request) } assert_equal("update", params[:action])
assert_equal("update", request.path_parameters[:action]) assert_equal("5", params[:id])
assert_equal("5", request.path_parameters[:id])
request.recycle!
request.env["REQUEST_METHOD"] = "DELETE" params = set.recognize_path("/people/5", :method => :delete)
assert_nothing_raised { set.recognize(request) } assert_equal("destroy", params[:action])
assert_equal("destroy", request.path_parameters[:action]) assert_equal("5", params[:id])
assert_equal("5", request.path_parameters[:id])
request.recycle!
begin begin
request.env["REQUEST_METHOD"] = "POST" set.recognize_path("/people/5", :method => :post)
set.recognize(request)
flunk 'Should have raised MethodNotAllowed' flunk 'Should have raised MethodNotAllowed'
rescue ActionController::MethodNotAllowed => e rescue ActionController::MethodNotAllowed => e
assert_equal [:get, :put, :delete], e.allowed_methods assert_equal [:get, :put, :delete], e.allowed_methods
end end
request.recycle!
end end
def test_recognize_with_alias_in_conditions def test_recognize_with_alias_in_conditions
@ -1038,17 +1007,13 @@ class RouteSetTest < ActiveSupport::TestCase
map.root :people map.root :people
end end
request.path = "/people" params = set.recognize_path("/people", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("people", params[:controller])
assert_nothing_raised { set.recognize(request) } assert_equal("index", params[:action])
assert_equal("people", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
request.path = "/" params = set.recognize_path("/", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("people", params[:controller])
assert_nothing_raised { set.recognize(request) } assert_equal("index", params[:action])
assert_equal("people", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
end end
def test_typo_recognition def test_typo_recognition
@ -1058,14 +1023,12 @@ class RouteSetTest < ActiveSupport::TestCase
:year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/ :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
end end
request.path = "/articles/2005/11/05/a-very-interesting-article" params = set.recognize_path("/articles/2005/11/05/a-very-interesting-article", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("permalink", params[:action])
assert_nothing_raised { set.recognize(request) } assert_equal("2005", params[:year])
assert_equal("permalink", request.path_parameters[:action]) assert_equal("11", params[:month])
assert_equal("2005", request.path_parameters[:year]) assert_equal("05", params[:day])
assert_equal("11", request.path_parameters[:month]) assert_equal("a-very-interesting-article", params[:title])
assert_equal("05", request.path_parameters[:day])
assert_equal("a-very-interesting-article", request.path_parameters[:title])
end end
def test_routing_traversal_does_not_load_extra_classes def test_routing_traversal_does_not_load_extra_classes
@ -1074,9 +1037,7 @@ class RouteSetTest < ActiveSupport::TestCase
map.connect '/profile', :controller => 'profile' map.connect '/profile', :controller => 'profile'
end end
request.path = '/profile' params = set.recognize_path("/profile") rescue nil
set.recognize(request) rescue nil
assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded" assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
end end
@ -1090,24 +1051,17 @@ class RouteSetTest < ActiveSupport::TestCase
end end
end end
request.request_uri = "/people/5" params = set.recognize_path("/people/5", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("show", params[:action])
assert_nothing_raised { set.recognize(request) } assert_equal("5", params[:id])
assert_equal("show", request.path_parameters[:action])
assert_equal("5", request.path_parameters[:id])
request.recycle!
request.env["REQUEST_METHOD"] = "PUT" params = set.recognize_path("/people/5", :method => :put)
assert_nothing_raised { set.recognize(request) } assert_equal("update", params[:action])
assert_equal("update", request.path_parameters[:action])
request.recycle!
request.request_uri = "/people/5.png" params = set.recognize_path("/people/5.png", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("show", params[:action])
assert_nothing_raised { set.recognize(request) } assert_equal("5", params[:id])
assert_equal("show", request.path_parameters[:action]) assert_equal("png", params[:_format])
assert_equal("5", request.path_parameters[:id])
assert_equal("png", request.path_parameters[:_format])
end end
def test_generate_with_default_action def test_generate_with_default_action
@ -1123,11 +1077,9 @@ class RouteSetTest < ActiveSupport::TestCase
def test_root_map def test_root_map
set.draw { |map| map.root :controller => "people" } set.draw { |map| map.root :controller => "people" }
request.path = "" params = set.recognize_path("", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("people", params[:controller])
assert_nothing_raised { set.recognize(request) } assert_equal("index", params[:action])
assert_equal("people", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
end end
def test_namespace def test_namespace
@ -1139,11 +1091,9 @@ class RouteSetTest < ActiveSupport::TestCase
end end
request.path = "/api/inventory" params = set.recognize_path("/api/inventory", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("api/products", params[:controller])
assert_nothing_raised { set.recognize(request) } assert_equal("inventory", params[:action])
assert_equal("api/products", request.path_parameters[:controller])
assert_equal("inventory", request.path_parameters[:action])
end end
def test_namespaced_root_map def test_namespaced_root_map
@ -1155,11 +1105,9 @@ class RouteSetTest < ActiveSupport::TestCase
end end
request.path = "/api" params = set.recognize_path("/api", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("api/products", params[:controller])
assert_nothing_raised { set.recognize(request) } assert_equal("index", params[:action])
assert_equal("api/products", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
end end
def test_namespace_with_path_prefix def test_namespace_with_path_prefix
@ -1169,11 +1117,9 @@ class RouteSetTest < ActiveSupport::TestCase
end end
end end
request.path = "/prefix/inventory" params = set.recognize_path("/prefix/inventory", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("api/products", params[:controller])
assert_nothing_raised { set.recognize(request) } assert_equal("inventory", params[:action])
assert_equal("api/products", request.path_parameters[:controller])
assert_equal("inventory", request.path_parameters[:action])
end end
def test_namespace_with_blank_path_prefix def test_namespace_with_blank_path_prefix
@ -1183,11 +1129,9 @@ class RouteSetTest < ActiveSupport::TestCase
end end
end end
request.path = "/inventory" params = set.recognize_path("/inventory", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("api/products", params[:controller])
assert_nothing_raised { set.recognize(request) } assert_equal("inventory", params[:action])
assert_equal("api/products", request.path_parameters[:controller])
assert_equal("inventory", request.path_parameters[:action])
end end
def test_generate_changes_controller_module def test_generate_changes_controller_module
@ -1316,11 +1260,9 @@ class RouteSetTest < ActiveSupport::TestCase
end end
end end
request.path = "/projects/1/milestones" params = set.recognize_path("/projects/1/milestones", :method => :get)
request.env["REQUEST_METHOD"] = "GET" assert_equal("milestones", params[:controller])
assert_nothing_raised { set.recognize(request) } assert_equal("index", params[:action])
assert_equal("milestones", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
end end
def test_setting_root_in_namespace_using_symbol def test_setting_root_in_namespace_using_symbol