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

404 lines
9.6 KiB
Ruby
Raw Normal View History

2009-12-01 22:19:20 -05:00
require 'isolation/abstract_unit'
require 'rack/test'
2009-12-01 22:19:20 -05:00
module ApplicationTests
2012-01-05 20:30:17 -05:00
class RoutingTest < ActiveSupport::TestCase
2009-12-01 22:19:20 -05:00
include ActiveSupport::Testing::Isolation
include Rack::Test::Methods
2009-12-01 22:19:20 -05:00
def setup
build_app
boot_rails
2009-12-01 22:19:20 -05:00
end
def teardown
teardown_app
end
2012-12-05 14:02:51 -05:00
test "rails/welcome in development" do
app("development")
get "/"
assert_equal 200, last_response.status
end
test "rails/info/routes in development" do
app("development")
get "/rails/info/routes"
assert_equal 200, last_response.status
end
test "rails/info/properties in development" do
app("development")
get "/rails/info/properties"
assert_equal 200, last_response.status
end
2012-12-05 14:02:51 -05:00
test "root takes precedence over internal welcome controller" do
app("development")
get '/'
assert_match %r{<h1>Getting started</h1>} , last_response.body
controller :foo, <<-RUBY
class FooController < ApplicationController
def index
render text: "foo"
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
2012-12-05 14:02:51 -05:00
root to: "foo#index"
end
RUBY
get '/'
assert_equal 'foo', last_response.body
end
test "rails/welcome in production" do
app("production")
get "/"
assert_equal 404, last_response.status
end
test "rails/info/routes in production" do
app("production")
get "/rails/info/routes"
assert_equal 404, last_response.status
end
test "rails/info/properties in production" do
app("production")
get "/rails/info/properties"
assert_equal 404, last_response.status
end
test "simple controller" do
simple_controller
2009-12-01 22:19:20 -05:00
get '/foo'
assert_equal 'foo', last_response.body
end
test "simple controller with helper" do
controller :foo, <<-RUBY
class FooController < ApplicationController
def index
2012-10-14 06:03:39 -04:00
render inline: "<%= foo_or_bar? %>"
end
end
RUBY
app_file 'app/helpers/bar_helper.rb', <<-RUBY
module BarHelper
def foo_or_bar?
"bar"
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
get ':controller(/:action)'
end
RUBY
get '/foo'
assert_equal 'bar', last_response.body
end
2010-06-17 07:48:02 -04:00
test "mount rack app" do
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
2012-10-14 06:03:39 -04:00
mount lambda { |env| [200, {}, [env["PATH_INFO"]]] }, at: "/blog"
2010-06-17 07:48:02 -04:00
# The line below is required because mount sometimes
# fails when a resource route is added.
resource :user
end
RUBY
get '/blog/archives'
assert_equal '/archives', last_response.body
end
2009-12-01 22:19:20 -05:00
test "multiple controllers" do
controller :foo, <<-RUBY
class FooController < ApplicationController
2009-12-01 22:19:20 -05:00
def index
2012-10-14 06:03:39 -04:00
render text: "foo"
2009-12-01 22:19:20 -05:00
end
end
RUBY
controller :bar, <<-RUBY
class BarController < ActionController::Base
def index
2012-10-14 06:03:39 -04:00
render text: "bar"
2009-12-01 22:19:20 -05:00
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
get ':controller(/:action)'
end
RUBY
2009-12-01 22:19:20 -05:00
get '/foo'
assert_equal 'foo', last_response.body
get '/bar'
assert_equal 'bar', last_response.body
end
test "nested controller" do
controller 'foo', <<-RUBY
class FooController < ApplicationController
2009-12-01 22:19:20 -05:00
def index
2012-10-14 06:03:39 -04:00
render text: "foo"
2009-12-01 22:19:20 -05:00
end
end
RUBY
controller 'admin/foo', <<-RUBY
module Admin
class FooController < ApplicationController
2009-12-01 22:19:20 -05:00
def index
2012-10-14 06:03:39 -04:00
render text: "admin::foo"
2009-12-01 22:19:20 -05:00
end
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
2012-10-14 06:03:39 -04:00
get 'admin/foo', to: 'admin/foo#index'
get 'foo', to: 'foo#index'
end
RUBY
2009-12-01 22:19:20 -05:00
get '/foo'
assert_equal 'foo', last_response.body
get '/admin/foo'
assert_equal 'admin::foo', last_response.body
end
test "routes appending blocks" do
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
get ':controller/:action'
end
RUBY
add_to_config <<-R
routes.append do
get '/win' => lambda { |e| [200, {'Content-Type'=>'text/plain'}, ['WIN']] }
end
R
app 'development'
get '/win'
assert_equal 'WIN', last_response.body
app_file 'config/routes.rb', <<-R
Rails.application.routes.draw do
get 'lol' => 'hello#index'
end
R
get '/win'
assert_equal 'WIN', last_response.body
end
{"development" => "baz", "production" => "bar"}.each do |mode, expected|
test "reloads routes when configuration is changed in #{mode}" do
controller :foo, <<-RUBY
class FooController < ApplicationController
def bar
2012-10-14 06:03:39 -04:00
render text: "bar"
end
def baz
2012-10-14 06:03:39 -04:00
render text: "baz"
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
2012-10-14 06:03:39 -04:00
get 'foo', to: 'foo#bar'
end
RUBY
app(mode)
get '/foo'
assert_equal 'bar', last_response.body
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
2012-10-14 06:03:39 -04:00
get 'foo', to: 'foo#baz'
end
RUBY
sleep 0.1
get '/foo'
assert_equal expected, last_response.body
end
end
test 'routes are loaded just after initialization' do
require "#{app_path}/config/application"
# Create the rack app just inside after initialize callback
ActiveSupport.on_load(:after_initialize) do
::InitializeRackApp = lambda { |env| [200, {}, ["InitializeRackApp"]] }
end
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
2012-10-14 06:03:39 -04:00
get 'foo', to: ::InitializeRackApp
end
RUBY
get '/foo'
assert_equal "InitializeRackApp", last_response.body
end
test 'reload_routes! is part of Rails.application API' do
app("development")
assert_nothing_raised do
Rails.application.reload_routes!
end
end
2013-04-17 14:20:12 -04:00
def test_root_path
app('development')
controller :foo, <<-RUBY
class FooController < ApplicationController
def index
render :text => "foo"
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
2013-04-17 14:20:12 -04:00
get 'foo', :to => 'foo#index'
root :to => 'foo#index'
end
RUBY
remove_file 'public/index.html'
get '/'
assert_equal 'foo', last_response.body
end
test 'routes are added and removed when reloading' do
app('development')
controller :foo, <<-RUBY
class FooController < ApplicationController
def index
render text: "foo"
end
end
RUBY
controller :bar, <<-RUBY
class BarController < ApplicationController
def index
render text: "bar"
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
get 'foo', to: 'foo#index'
end
RUBY
get '/foo'
assert_equal 'foo', last_response.body
assert_equal '/foo', Rails.application.routes.url_helpers.foo_path
get '/bar'
assert_equal 404, last_response.status
assert_raises NoMethodError do
assert_equal '/bar', Rails.application.routes.url_helpers.bar_path
end
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
get 'foo', to: 'foo#index'
get 'bar', to: 'bar#index'
end
RUBY
Rails.application.reload_routes!
get '/foo'
assert_equal 'foo', last_response.body
assert_equal '/foo', Rails.application.routes.url_helpers.foo_path
get '/bar'
assert_equal 'bar', last_response.body
assert_equal '/bar', Rails.application.routes.url_helpers.bar_path
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
get 'foo', to: 'foo#index'
end
RUBY
Rails.application.reload_routes!
get '/foo'
assert_equal 'foo', last_response.body
assert_equal '/foo', Rails.application.routes.url_helpers.foo_path
get '/bar'
assert_equal 404, last_response.status
assert_raises NoMethodError do
assert_equal '/bar', Rails.application.routes.url_helpers.bar_path
end
end
test 'resource routing with irregular inflection' do
app_file 'config/initializers/inflection.rb', <<-RUBY
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'yazi', 'yazilar'
end
RUBY
app_file 'config/routes.rb', <<-RUBY
Rails.application.routes.draw do
resources :yazilar
end
RUBY
controller 'yazilar', <<-RUBY
class YazilarController < ApplicationController
def index
2012-10-14 06:03:39 -04:00
render text: 'yazilar#index'
end
end
RUBY
get '/yazilars'
assert_equal 404, last_response.status
get '/yazilar'
assert_equal 200, last_response.status
end
2009-12-01 22:19:20 -05:00
end
end