aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/routing/inspector_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/test/dispatch/routing/inspector_test.rb')
-rw-r--r--actionpack/test/dispatch/routing/inspector_test.rb184
1 files changed, 127 insertions, 57 deletions
diff --git a/actionpack/test/dispatch/routing/inspector_test.rb b/actionpack/test/dispatch/routing/inspector_test.rb
index 9d0d23d6de..fe1f1995d8 100644
--- a/actionpack/test/dispatch/routing/inspector_test.rb
+++ b/actionpack/test/dispatch/routing/inspector_test.rb
@@ -1,6 +1,9 @@
-require 'abstract_unit'
-require 'rails/engine'
-require 'action_dispatch/routing/inspector'
+# frozen_string_literal: true
+
+require "abstract_unit"
+require "rails/engine"
+require "action_dispatch/routing/inspector"
+require "io/console/size"
class MountedRackApp
def self.call(env)
@@ -13,16 +16,10 @@ end
module ActionDispatch
module Routing
class RoutesInspectorTest < ActiveSupport::TestCase
- def setup
+ setup do
@set = ActionDispatch::Routing::RouteSet.new
end
- def draw(options = nil, &block)
- @set.draw(&block)
- inspector = ActionDispatch::Routing::RoutesInspector.new(@set.routes)
- inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, options).split("\n")
- end
-
def test_displaying_routes_for_engines
engine = Class.new(Rails::Engine) do
def self.inspect
@@ -30,11 +27,11 @@ module ActionDispatch
end
end
engine.routes.draw do
- get '/cart', :to => 'cart#show'
+ get "/cart", to: "cart#show"
end
output = draw do
- get '/custom/assets', :to => 'custom_assets#show'
+ get "/custom/assets", to: "custom_assets#show"
mount engine => "/blog", :as => "blog"
end
@@ -71,7 +68,7 @@ module ActionDispatch
def test_cart_inspect
output = draw do
- get '/cart', :to => 'cart#show'
+ get "/cart", to: "cart#show"
end
assert_equal [
@@ -82,7 +79,7 @@ module ActionDispatch
def test_articles_inspect_with_multiple_verbs
output = draw do
- match 'articles/:id', to: 'articles#update', via: [:put, :patch]
+ match "articles/:id", to: "articles#update", via: [:put, :patch]
end
assert_equal [
@@ -93,7 +90,7 @@ module ActionDispatch
def test_inspect_shows_custom_assets
output = draw do
- get '/custom/assets', :to => 'custom_assets#show'
+ get "/custom/assets", to: "custom_assets#show"
end
assert_equal [
@@ -122,7 +119,7 @@ module ActionDispatch
def test_inspect_routes_shows_root_route
output = draw do
- root :to => 'pages#main'
+ root to: "pages#main"
end
assert_equal [
@@ -134,7 +131,7 @@ module ActionDispatch
def test_inspect_routes_shows_dynamic_action_route
output = draw do
ActiveSupport::Deprecation.silence do
- get 'api/:action' => 'api'
+ get "api/:action" => "api"
end
end
@@ -147,7 +144,7 @@ module ActionDispatch
def test_inspect_routes_shows_controller_and_action_only_route
output = draw do
ActiveSupport::Deprecation.silence do
- get ':controller/:action'
+ get ":controller/:action"
end
end
@@ -160,7 +157,7 @@ module ActionDispatch
def test_inspect_routes_shows_controller_and_action_route_with_constraints
output = draw do
ActiveSupport::Deprecation.silence do
- get ':controller(/:action(/:id))', :id => /\d+/
+ get ":controller(/:action(/:id))", id: /\d+/
end
end
@@ -172,18 +169,18 @@ module ActionDispatch
def test_rails_routes_shows_route_with_defaults
output = draw do
- get 'photos/:id' => 'photos#show', :defaults => {:format => 'jpg'}
+ get "photos/:id" => "photos#show", :defaults => { format: "jpg" }
end
assert_equal [
"Prefix Verb URI Pattern Controller#Action",
- %Q[ GET /photos/:id(.:format) photos#show {:format=>"jpg"}]
+ ' GET /photos/:id(.:format) photos#show {:format=>"jpg"}'
], output
end
def test_rails_routes_shows_route_with_constraints
output = draw do
- get 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
+ get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/
end
assert_equal [
@@ -194,13 +191,13 @@ module ActionDispatch
def test_rails_routes_shows_routes_with_dashes
output = draw do
- get 'about-us' => 'pages#about_us'
- get 'our-work/latest'
+ get "about-us" => "pages#about_us"
+ get "our-work/latest"
resources :photos, only: [:show] do
- get 'user-favorites', on: :collection
- get 'preview-photo', on: :member
- get 'summary-text'
+ get "user-favorites", on: :collection
+ get "preview-photo", on: :member
+ get "summary-text"
end
end
@@ -217,7 +214,7 @@ module ActionDispatch
def test_rails_routes_shows_route_with_rack_app
output = draw do
- get 'foo/:id' => MountedRackApp, :id => /[A-Z]\d{5}/
+ get "foo/:id" => MountedRackApp, :id => /[A-Z]\d{5}/
end
assert_equal [
@@ -228,7 +225,7 @@ module ActionDispatch
def test_rails_routes_shows_named_route_with_mounted_rack_app
output = draw do
- mount MountedRackApp => '/foo'
+ mount MountedRackApp => "/foo"
end
assert_equal [
@@ -239,7 +236,7 @@ module ActionDispatch
def test_rails_routes_shows_overridden_named_route_with_mounted_rack_app_with_name
output = draw do
- mount MountedRackApp => '/foo', as: 'blog'
+ mount MountedRackApp => "/foo", as: "blog"
end
assert_equal [
@@ -256,8 +253,8 @@ module ActionDispatch
end
output = draw do
- scope :constraint => constraint.new do
- mount MountedRackApp => '/foo'
+ scope constraint: constraint.new do
+ mount MountedRackApp => "/foo"
end
end
@@ -269,7 +266,7 @@ module ActionDispatch
def test_rails_routes_dont_show_app_mounted_in_assets_prefix
output = draw do
- get '/sprockets' => MountedRackApp
+ get "/sprockets" => MountedRackApp
end
assert_no_match(/MountedRackApp/, output.first)
assert_no_match(/\/sprockets/, output.first)
@@ -277,8 +274,8 @@ module ActionDispatch
def test_rails_routes_shows_route_defined_in_under_assets_prefix
output = draw do
- scope '/sprockets' do
- get '/foo' => 'foo#bar'
+ scope "/sprockets" do
+ get "/foo" => "foo#bar"
end
end
assert_equal [
@@ -289,9 +286,9 @@ module ActionDispatch
def test_redirect
output = draw do
- get "/foo" => redirect("/foo/bar"), :constraints => { :subdomain => "admin" }
+ get "/foo" => redirect("/foo/bar"), :constraints => { subdomain: "admin" }
get "/bar" => redirect(path: "/foo/bar", status: 307)
- get "/foobar" => redirect{ "/foo/bar" }
+ get "/foobar" => redirect { "/foo/bar" }
end
assert_equal [
@@ -303,7 +300,7 @@ module ActionDispatch
end
def test_routes_can_be_filtered
- output = draw('posts') do
+ output = draw(grep: "posts") do
resources :articles
resources :posts
end
@@ -319,8 +316,76 @@ module ActionDispatch
" DELETE /posts/:id(.:format) posts#destroy"], output
end
+ def test_routes_when_expanded
+ previous_console_winsize = IO.console.winsize
+ IO.console.winsize = [0, 23]
+
+ engine = Class.new(Rails::Engine) do
+ def self.inspect
+ "Blog::Engine"
+ end
+ end
+ engine.routes.draw do
+ get "/cart", to: "cart#show"
+ end
+
+ output = draw(formatter: ActionDispatch::Routing::ConsoleFormatter::Expanded.new) do
+ get "/custom/assets", to: "custom_assets#show"
+ get "/custom/furnitures", to: "custom_furnitures#show"
+ mount engine => "/blog", :as => "blog"
+ end
+
+ assert_equal ["--[ Route 1 ]----------",
+ "Prefix | custom_assets",
+ "Verb | GET",
+ "URI | /custom/assets(.:format)",
+ "Controller#Action | custom_assets#show",
+ "--[ Route 2 ]----------",
+ "Prefix | custom_furnitures",
+ "Verb | GET",
+ "URI | /custom/furnitures(.:format)",
+ "Controller#Action | custom_furnitures#show",
+ "--[ Route 3 ]----------",
+ "Prefix | blog",
+ "Verb | ",
+ "URI | /blog",
+ "Controller#Action | Blog::Engine",
+ "",
+ "[ Routes for Blog::Engine ]",
+ "--[ Route 1 ]----------",
+ "Prefix | cart",
+ "Verb | GET",
+ "URI | /cart(.:format)",
+ "Controller#Action | cart#show"], output
+ ensure
+ IO.console.winsize = previous_console_winsize
+ end
+
+ def test_no_routes_matched_filter_when_expanded
+ output = draw(grep: "rails/dummy", formatter: ActionDispatch::Routing::ConsoleFormatter::Expanded.new) do
+ get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/
+ end
+
+ assert_equal [
+ "No routes were found for this grep pattern.",
+ "For more information about routes, see the Rails guide: https://guides.rubyonrails.org/routing.html."
+ ], output
+ end
+
+ def test_not_routes_when_expanded
+ output = draw(grep: "rails/dummy", formatter: ActionDispatch::Routing::ConsoleFormatter::Expanded.new) { }
+
+ assert_equal [
+ "You don't have any routes defined!",
+ "",
+ "Please add some routes in config/routes.rb.",
+ "",
+ "For more information about routes, see the Rails guide: https://guides.rubyonrails.org/routing.html."
+ ], output
+ end
+
def test_routes_can_be_filtered_with_namespaced_controllers
- output = draw('admin/posts') do
+ output = draw(grep: "admin/posts") do
resources :articles
namespace :admin do
resources :posts
@@ -338,23 +403,22 @@ module ActionDispatch
" DELETE /admin/posts/:id(.:format) admin/posts#destroy"], output
end
-
def test_regression_route_with_controller_regexp
output = draw do
ActiveSupport::Deprecation.silence do
- get ':controller(/:action)', controller: /api\/[^\/]+/, format: false
+ get ":controller(/:action)", controller: /api\/[^\/]+/, format: false
end
end
assert_equal ["Prefix Verb URI Pattern Controller#Action",
- " GET /:controller(/:action) (?-mix:api\\/[^\\/]+)#:action"], output
+ " GET /:controller(/:action) :controller#:action"], output
end
def test_inspect_routes_shows_resources_route_when_assets_disabled
@set = ActionDispatch::Routing::RouteSet.new
output = draw do
- get '/cart', to: 'cart#show'
+ get "/cart", to: "cart#show"
end
assert_equal [
@@ -364,36 +428,36 @@ module ActionDispatch
end
def test_routes_with_undefined_filter
- output = draw(controller: 'Rails::MissingController') do
- get 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
+ output = draw(controller: "Rails::MissingController") do
+ get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/
end
assert_equal [
- "No routes were found for this controller",
- "For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
+ "No routes were found for this controller.",
+ "For more information about routes, see the Rails guide: https://guides.rubyonrails.org/routing.html."
], output
end
def test_no_routes_matched_filter
- output = draw('rails/dummy') do
- get 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
+ output = draw(grep: "rails/dummy") do
+ get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/
end
assert_equal [
- "No routes were found for this controller",
- "For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
+ "No routes were found for this grep pattern.",
+ "For more information about routes, see the Rails guide: https://guides.rubyonrails.org/routing.html."
], output
end
def test_no_routes_were_defined
- output = draw('Rails::DummyController') {}
+ output = draw(grep: "Rails::DummyController") { }
assert_equal [
"You don't have any routes defined!",
"",
"Please add some routes in config/routes.rb.",
"",
- "For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
+ "For more information about routes, see the Rails guide: https://guides.rubyonrails.org/routing.html."
], output
end
@@ -404,13 +468,13 @@ module ActionDispatch
end
end
engine.routes.draw do
- get '/cart', to: 'cart#show'
- post '/cart', to: 'cart#create'
- patch '/cart', to: 'cart#update'
+ get "/cart", to: "cart#show"
+ post "/cart", to: "cart#create"
+ patch "/cart", to: "cart#update"
end
output = draw do
- get '/custom/assets', to: 'custom_assets#show'
+ get "/custom/assets", to: "custom_assets#show"
mount engine => "/blog", as: "blog", internal: true
end
@@ -420,6 +484,12 @@ module ActionDispatch
], output
end
+ private
+ def draw(formatter: ActionDispatch::Routing::ConsoleFormatter::Sheet.new, **options, &block)
+ @set.draw(&block)
+ inspector = ActionDispatch::Routing::RoutesInspector.new(@set.routes)
+ inspector.format(formatter, options).split("\n")
+ end
end
end
end