class ActionDispatch::Routing::RoutesInspectorTest

Public Class Methods

inspect() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 30
def self.inspect
  "Blog::Engine"
end

Public Instance Methods

draw(options = nil, &block) click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 22
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
inspect() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 255
def inspect
  "( my custom constraint )"
end
setup() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 18
def setup
  @set = ActionDispatch::Routing::RouteSet.new
end
test_articles_inspect_with_multiple_verbs() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 85
def test_articles_inspect_with_multiple_verbs
  output = draw do
    match "articles/:id", to: "articles#update", via: [:put, :patch]
  end

  assert_equal [
    "Prefix Verb      URI Pattern             Controller#Action",
    "       PUT|PATCH /articles/:id(.:format) articles#update"
  ], output
end
test_cart_inspect() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 74
def test_cart_inspect
  output = draw do
    get "/cart", to: "cart#show"
  end

  assert_equal [
    "Prefix Verb URI Pattern     Controller#Action",
    "  cart GET  /cart(.:format) cart#show"
  ], output
end
test_displaying_routes_for_engines() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 28
def test_displaying_routes_for_engines
  engine = Class.new(Quails::Engine) do
    def self.inspect
      "Blog::Engine"
    end
  end
  engine.routes.draw do
    get "/cart", to: "cart#show"
  end

  output = draw do
    get "/custom/assets", to: "custom_assets#show"
    mount engine => "/blog", :as => "blog"
  end

  assert_equal [
    "       Prefix Verb URI Pattern              Controller#Action",
    "custom_assets GET  /custom/assets(.:format) custom_assets#show",
    "         blog      /blog                    Blog::Engine",
    "",
    "Routes for Blog::Engine:",
    "  cart GET  /cart(.:format) cart#show"
  ], output
end
test_displaying_routes_for_engines_without_routes() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 53
def test_displaying_routes_for_engines_without_routes
  engine = Class.new(Quails::Engine) do
    def self.inspect
      "Blog::Engine"
    end
  end
  engine.routes.draw do
  end

  output = draw do
    mount engine => "/blog", as: "blog"
  end

  assert_equal [
    "Prefix Verb URI Pattern Controller#Action",
    "  blog      /blog       Blog::Engine",
    "",
    "Routes for Blog::Engine:"
  ], output
end
test_displaying_routes_for_internal_engines() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 401
def test_displaying_routes_for_internal_engines
  engine = Class.new(Quails::Engine) do
    def self.inspect
      "Blog::Engine"
    end
  end
  engine.routes.draw do
    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"
    mount engine => "/blog", as: "blog", internal: true
  end

  assert_equal [
    "       Prefix Verb URI Pattern              Controller#Action",
    "custom_assets GET  /custom/assets(.:format) custom_assets#show",
  ], output
end
test_inspect_routes_shows_controller_and_action_only_route() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 149
def test_inspect_routes_shows_controller_and_action_only_route
  output = draw do
    ActiveSupport::Deprecation.silence do
      get ":controller/:action"
    end
  end

  assert_equal [
    "Prefix Verb URI Pattern                    Controller#Action",
    "       GET  /:controller/:action(.:format) :controller#:action"
  ], output
end
test_inspect_routes_shows_controller_and_action_route_with_constraints() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 162
def test_inspect_routes_shows_controller_and_action_route_with_constraints
  output = draw do
    ActiveSupport::Deprecation.silence do
      get ":controller(/:action(/:id))", id: /\d+/
    end
  end

  assert_equal [
    "Prefix Verb URI Pattern                            Controller#Action",
    "       GET  /:controller(/:action(/:id))(.:format) :controller#:action {:id=>/\\d+/}"
  ], output
end
test_inspect_routes_shows_dynamic_action_route() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 136
def test_inspect_routes_shows_dynamic_action_route
  output = draw do
    ActiveSupport::Deprecation.silence do
      get "api/:action" => "api"
    end
  end

  assert_equal [
    "Prefix Verb URI Pattern            Controller#Action",
    "       GET  /api/:action(.:format) api#:action"
  ], output
end
test_inspect_routes_shows_resources_route() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 107
def test_inspect_routes_shows_resources_route
  output = draw do
    resources :articles
  end

  assert_equal [
    "      Prefix Verb   URI Pattern                  Controller#Action",
    "    articles GET    /articles(.:format)          articles#index",
    "             POST   /articles(.:format)          articles#create",
    " new_article GET    /articles/new(.:format)      articles#new",
    "edit_article GET    /articles/:id/edit(.:format) articles#edit",
    "     article GET    /articles/:id(.:format)      articles#show",
    "             PATCH  /articles/:id(.:format)      articles#update",
    "             PUT    /articles/:id(.:format)      articles#update",
    "             DELETE /articles/:id(.:format)      articles#destroy"
  ], output
end
test_inspect_routes_shows_resources_route_when_assets_disabled() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 354
def test_inspect_routes_shows_resources_route_when_assets_disabled
  @set = ActionDispatch::Routing::RouteSet.new

  output = draw do
    get "/cart", to: "cart#show"
  end

  assert_equal [
    "Prefix Verb URI Pattern     Controller#Action",
    "  cart GET  /cart(.:format) cart#show"
  ], output
end
test_inspect_routes_shows_root_route() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 125
def test_inspect_routes_shows_root_route
  output = draw do
    root to: "pages#main"
  end

  assert_equal [
    "Prefix Verb URI Pattern Controller#Action",
    "  root GET  /           pages#main"
  ], output
end
test_inspect_shows_custom_assets() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 96
def test_inspect_shows_custom_assets
  output = draw do
    get "/custom/assets", to: "custom_assets#show"
  end

  assert_equal [
    "       Prefix Verb URI Pattern              Controller#Action",
    "custom_assets GET  /custom/assets(.:format) custom_assets#show"
  ], output
end
test_no_routes_matched_filter() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 378
def test_no_routes_matched_filter
  output = draw("quails/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 Quails guide: http://guides.rubyonquails.org/routing.html."
  ], output
end
test_no_routes_were_defined() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 389
def test_no_routes_were_defined
  output = draw("Quails::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 Quails guide: http://guides.rubyonquails.org/routing.html."
  ], output
end
test_quails_routes_dont_show_app_mounted_in_assets_prefix() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 272
def test_quails_routes_dont_show_app_mounted_in_assets_prefix
  output = draw do
    get "/sprockets" => MountedRackApp
  end
  assert_no_match(/MountedRackApp/, output.first)
  assert_no_match(/\/sprockets/, output.first)
end
test_quails_routes_shows_named_route_with_mounted_rack_app() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 231
def test_quails_routes_shows_named_route_with_mounted_rack_app
  output = draw do
    mount MountedRackApp => "/foo"
  end

  assert_equal [
    "          Prefix Verb URI Pattern Controller#Action",
    "mounted_rack_app      /foo        MountedRackApp"
  ], output
end
test_quails_routes_shows_overridden_named_route_with_mounted_rack_app_with_name() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 242
def test_quails_routes_shows_overridden_named_route_with_mounted_rack_app_with_name
  output = draw do
    mount MountedRackApp => "/foo", as: "blog"
  end

  assert_equal [
    "Prefix Verb URI Pattern Controller#Action",
    "  blog      /foo        MountedRackApp"
  ], output
end
test_quails_routes_shows_route_defined_in_under_assets_prefix() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 280
def test_quails_routes_shows_route_defined_in_under_assets_prefix
  output = draw do
    scope "/sprockets" do
      get "/foo" => "foo#bar"
    end
  end
  assert_equal [
    "Prefix Verb URI Pattern              Controller#Action",
    "   foo GET  /sprockets/foo(.:format) foo#bar"
  ], output
end
test_quails_routes_shows_route_with_constraints() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 186
def test_quails_routes_shows_route_with_constraints
  output = draw do
    get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/
  end

  assert_equal [
    "Prefix Verb URI Pattern           Controller#Action",
    "       GET  /photos/:id(.:format) photos#show {:id=>/[A-Z]\\d{5}/}"
  ], output
end
test_quails_routes_shows_route_with_defaults() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 175
def test_quails_routes_shows_route_with_defaults
  output = draw do
    get "photos/:id" => "photos#show", :defaults => { format: "jpg" }
  end

  assert_equal [
    "Prefix Verb URI Pattern           Controller#Action",
    '       GET  /photos/:id(.:format) photos#show {:format=>"jpg"}'
  ], output
end
test_quails_routes_shows_route_with_rack_app() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 220
def test_quails_routes_shows_route_with_rack_app
  output = draw do
    get "foo/:id" => MountedRackApp, :id => /[A-Z]\d{5}/
  end

  assert_equal [
    "Prefix Verb URI Pattern        Controller#Action",
    "       GET  /foo/:id(.:format) MountedRackApp {:id=>/[A-Z]\\d{5}/}"
  ], output
end
test_quails_routes_shows_route_with_rack_app_nested_with_dynamic_constraints() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 253
def test_quails_routes_shows_route_with_rack_app_nested_with_dynamic_constraints
  constraint = Class.new do
    def inspect
      "( my custom constraint )"
    end
  end

  output = draw do
    scope constraint: constraint.new do
      mount MountedRackApp => "/foo"
    end
  end

  assert_equal [
    "          Prefix Verb URI Pattern Controller#Action",
    "mounted_rack_app      /foo        MountedRackApp {:constraint=>( my custom constraint )}"
  ], output
end
test_quails_routes_shows_routes_with_dashes() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 197
def test_quails_routes_shows_routes_with_dashes
  output = draw do
    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"
    end
  end

  assert_equal [
    "               Prefix Verb URI Pattern                              Controller#Action",
    "             about_us GET  /about-us(.:format)                      pages#about_us",
    "      our_work_latest GET  /our-work/latest(.:format)               our_work#latest",
    "user_favorites_photos GET  /photos/user-favorites(.:format)         photos#user_favorites",
    "  preview_photo_photo GET  /photos/:id/preview-photo(.:format)      photos#preview_photo",
    "   photo_summary_text GET  /photos/:photo_id/summary-text(.:format) photos#summary_text",
    "                photo GET  /photos/:id(.:format)                    photos#show"
  ], output
end
test_redirect() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 292
def test_redirect
  output = draw do
    get "/foo"    => redirect("/foo/bar"), :constraints => { subdomain: "admin" }
    get "/bar"    => redirect(path: "/foo/bar", status: 307)
    get "/foobar" => redirect { "/foo/bar" }
  end

  assert_equal [
    "Prefix Verb URI Pattern       Controller#Action",
    "   foo GET  /foo(.:format)    redirect(301, /foo/bar) {:subdomain=>\"admin\"}",
    "   bar GET  /bar(.:format)    redirect(307, path: /foo/bar)",
    "foobar GET  /foobar(.:format) redirect(301)"
  ], output
end
test_regression_route_with_controller_regexp() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 343
def test_regression_route_with_controller_regexp
  output = draw do
    ActiveSupport::Deprecation.silence do
      get ":controller(/:action)", controller: /api\/[^\/]+/, format: false
    end
  end

  assert_equal ["Prefix Verb URI Pattern            Controller#Action",
                "       GET  /:controller(/:action) :controller#:action"], output
end
test_routes_can_be_filtered() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 307
def test_routes_can_be_filtered
  output = draw("posts") do
    resources :articles
    resources :posts
  end

  assert_equal ["   Prefix Verb   URI Pattern               Controller#Action",
                "    posts GET    /posts(.:format)          posts#index",
                "          POST   /posts(.:format)          posts#create",
                " new_post GET    /posts/new(.:format)      posts#new",
                "edit_post GET    /posts/:id/edit(.:format) posts#edit",
                "     post GET    /posts/:id(.:format)      posts#show",
                "          PATCH  /posts/:id(.:format)      posts#update",
                "          PUT    /posts/:id(.:format)      posts#update",
                "          DELETE /posts/:id(.:format)      posts#destroy"], output
end
test_routes_can_be_filtered_with_namespaced_controllers() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 324
def test_routes_can_be_filtered_with_namespaced_controllers
  output = draw("admin/posts") do
    resources :articles
    namespace :admin do
      resources :posts
    end
  end

  assert_equal ["         Prefix Verb   URI Pattern                     Controller#Action",
                "    admin_posts GET    /admin/posts(.:format)          admin/posts#index",
                "                POST   /admin/posts(.:format)          admin/posts#create",
                " new_admin_post GET    /admin/posts/new(.:format)      admin/posts#new",
                "edit_admin_post GET    /admin/posts/:id/edit(.:format) admin/posts#edit",
                "     admin_post GET    /admin/posts/:id(.:format)      admin/posts#show",
                "                PATCH  /admin/posts/:id(.:format)      admin/posts#update",
                "                PUT    /admin/posts/:id(.:format)      admin/posts#update",
                "                DELETE /admin/posts/:id(.:format)      admin/posts#destroy"], output
end
test_routes_with_undefined_filter() click to toggle source
# File actionpack/test/dispatch/routing/inspector_test.rb, line 367
def test_routes_with_undefined_filter
  output = draw(controller: "Quails::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 Quails guide: http://guides.rubyonquails.org/routing.html."
  ], output
end