2009-12-01 22:19:20 -05:00
|
|
|
require 'isolation/abstract_unit'
|
2011-05-08 06:57:07 -04:00
|
|
|
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
|
2011-05-08 06:57:07 -04:00
|
|
|
include Rack::Test::Methods
|
2009-12-01 22:19:20 -05:00
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
2009-12-23 19:14:34 -05:00
|
|
|
boot_rails
|
2009-12-01 22:19:20 -05:00
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2012-05-23 15:42:28 -04:00
|
|
|
test "rails/info/routes in development" do
|
|
|
|
app("development")
|
|
|
|
get "/rails/info/routes"
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
end
|
|
|
|
|
2010-08-09 14:48:31 -04:00
|
|
|
test "rails/info/properties in development" do
|
|
|
|
app("development")
|
|
|
|
get "/rails/info/properties"
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
end
|
|
|
|
|
2012-05-23 15:42:28 -04:00
|
|
|
test "rails/info/routes in production" do
|
|
|
|
app("production")
|
|
|
|
get "/rails/info/routes"
|
|
|
|
assert_equal 404, last_response.status
|
|
|
|
end
|
|
|
|
|
2010-08-09 14:48:31 -04:00
|
|
|
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-21 18:47:03 -05:00
|
|
|
|
2009-12-01 22:19:20 -05:00
|
|
|
get '/foo'
|
|
|
|
assert_equal 'foo', last_response.body
|
2010-08-09 14:48:31 -04:00
|
|
|
end
|
|
|
|
|
2010-01-26 07:57:11 -05:00
|
|
|
test "simple controller with helper" do
|
|
|
|
controller :foo, <<-RUBY
|
|
|
|
class FooController < ApplicationController
|
|
|
|
def index
|
|
|
|
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
|
2010-08-05 16:47:01 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller(/:action)'
|
2010-01-26 07:57:11 -05:00
|
|
|
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
|
2010-08-05 16:47:01 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2010-06-17 07:48:02 -04:00
|
|
|
mount lambda { |env| [200, {}, [env["PATH_INFO"]]] }, :at => "/blog"
|
|
|
|
# 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
|
2010-01-26 07:57:11 -05:00
|
|
|
class FooController < ApplicationController
|
2009-12-01 22:19:20 -05:00
|
|
|
def index
|
|
|
|
render :text => "foo"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
controller :bar, <<-RUBY
|
|
|
|
class BarController < ActionController::Base
|
|
|
|
def index
|
|
|
|
render :text => "bar"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2009-12-21 18:47:03 -05:00
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2010-08-05 16:47:01 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller(/:action)'
|
2009-12-21 18:47:03 -05:00
|
|
|
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
|
2010-01-26 07:57:11 -05:00
|
|
|
class FooController < ApplicationController
|
2009-12-01 22:19:20 -05:00
|
|
|
def index
|
|
|
|
render :text => "foo"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
controller 'admin/foo', <<-RUBY
|
|
|
|
module Admin
|
2010-01-26 07:57:11 -05:00
|
|
|
class FooController < ApplicationController
|
2009-12-01 22:19:20 -05:00
|
|
|
def index
|
|
|
|
render :text => "admin::foo"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2009-12-21 18:47:03 -05:00
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2010-08-05 16:47:01 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'admin/foo', :to => 'admin/foo#index'
|
|
|
|
get 'foo', :to => 'foo#index'
|
2009-12-21 18:47:03 -05:00
|
|
|
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
|
2009-12-14 17:51:13 -05:00
|
|
|
|
2010-10-02 11:42:36 -04:00
|
|
|
test "routes appending blocks" do
|
2010-09-17 15:05:40 -04:00
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
|
|
|
AppTemplate::Application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action'
|
2010-09-17 15:05:40 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-R
|
|
|
|
routes.append do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/win' => lambda { |e| [200, {'Content-Type'=>'text/plain'}, ['WIN']] }
|
2010-09-17 15:05:40 -04:00
|
|
|
end
|
|
|
|
R
|
|
|
|
|
|
|
|
app 'development'
|
|
|
|
|
|
|
|
get '/win'
|
|
|
|
assert_equal 'WIN', last_response.body
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-R
|
|
|
|
AppTemplate::Application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'lol' => 'hello#index'
|
2010-09-17 15:05:40 -04:00
|
|
|
end
|
|
|
|
R
|
|
|
|
|
|
|
|
get '/win'
|
|
|
|
assert_equal 'WIN', last_response.body
|
|
|
|
end
|
|
|
|
|
2012-04-25 23:45:56 -04:00
|
|
|
{"development" => "baz", "production" => "bar"}.each do |mode, expected|
|
2010-08-09 14:48:31 -04:00
|
|
|
test "reloads routes when configuration is changed in #{mode}" do
|
|
|
|
controller :foo, <<-RUBY
|
|
|
|
class FooController < ApplicationController
|
|
|
|
def bar
|
|
|
|
render :text => "bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
def baz
|
|
|
|
render :text => "baz"
|
|
|
|
end
|
2009-12-15 11:48:56 -05:00
|
|
|
end
|
2010-08-09 14:48:31 -04:00
|
|
|
RUBY
|
2009-12-15 11:48:56 -05:00
|
|
|
|
2010-08-09 14:48:31 -04:00
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2010-08-05 16:47:01 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'foo', :to => 'foo#bar'
|
2009-12-15 11:48:56 -05:00
|
|
|
end
|
2010-08-09 14:48:31 -04:00
|
|
|
RUBY
|
2009-12-15 11:48:56 -05:00
|
|
|
|
2010-08-09 14:48:31 -04:00
|
|
|
app(mode)
|
2009-12-15 11:48:56 -05:00
|
|
|
|
2010-08-09 14:48:31 -04:00
|
|
|
get '/foo'
|
|
|
|
assert_equal 'bar', last_response.body
|
2009-12-15 11:48:56 -05:00
|
|
|
|
2010-08-09 14:48:31 -04:00
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2010-09-07 16:15:07 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'foo', :to => 'foo#baz'
|
2010-08-09 14:48:31 -04:00
|
|
|
end
|
|
|
|
RUBY
|
2009-12-15 11:48:56 -05:00
|
|
|
|
2010-08-09 14:48:31 -04:00
|
|
|
sleep 0.1
|
2009-12-23 20:01:07 -05:00
|
|
|
|
2010-08-09 14:48:31 -04:00
|
|
|
get '/foo'
|
|
|
|
assert_equal expected, last_response.body
|
|
|
|
end
|
2009-12-15 11:48:56 -05:00
|
|
|
end
|
2010-01-21 11:30:17 -05:00
|
|
|
|
2010-09-02 06:54:16 -04:00
|
|
|
test 'routes are loaded just after initialization' do
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
|
2010-10-02 11:42:36 -04:00
|
|
|
# Create the rack app just inside after initialize callback
|
|
|
|
ActiveSupport.on_load(:after_initialize) do
|
|
|
|
::InitializeRackApp = lambda { |env| [200, {}, ["InitializeRackApp"]] }
|
|
|
|
end
|
2010-09-11 06:58:37 -04:00
|
|
|
|
2010-10-02 11:42:36 -04:00
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2010-10-02 11:45:26 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'foo', :to => ::InitializeRackApp
|
2010-09-02 06:54:16 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal "InitializeRackApp", last_response.body
|
|
|
|
end
|
|
|
|
|
2010-10-02 11:45:26 -04:00
|
|
|
test 'reload_routes! is part of Rails.application API' do
|
|
|
|
app("development")
|
|
|
|
assert_nothing_raised do
|
|
|
|
Rails.application.reload_routes!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-02 11:42:36 -04:00
|
|
|
test 'resource routing with irregular inflection' do
|
2010-01-21 11:30:17 -05:00
|
|
|
app_file 'config/initializers/inflection.rb', <<-RUBY
|
|
|
|
ActiveSupport::Inflector.inflections do |inflect|
|
|
|
|
inflect.irregular 'yazi', 'yazilar'
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2010-08-05 16:47:01 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2010-01-21 11:30:17 -05:00
|
|
|
resources :yazilar
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
controller 'yazilar', <<-RUBY
|
2010-01-26 07:57:11 -05:00
|
|
|
class YazilarController < ApplicationController
|
2010-01-21 11:30:17 -05:00
|
|
|
def index
|
|
|
|
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
|