2009-10-08 21:12:28 -04:00
|
|
|
require "isolation/abstract_unit"
|
|
|
|
|
|
|
|
module ApplicationTests
|
2010-01-26 07:57:11 -05:00
|
|
|
class ConfigurationTest < Test::Unit::TestCase
|
2009-10-08 21:12:28 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
2009-12-22 20:03:23 -05:00
|
|
|
def new_app
|
|
|
|
File.expand_path("#{app_path}/../new_app")
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_app
|
|
|
|
FileUtils.cp_r(app_path, new_app)
|
|
|
|
end
|
|
|
|
|
2010-01-27 11:46:55 -05:00
|
|
|
def app
|
|
|
|
@app ||= Rails.application
|
|
|
|
end
|
|
|
|
|
2009-10-08 21:12:28 -04:00
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
2009-12-31 16:11:54 -05:00
|
|
|
FileUtils.rm_rf("#{app_path}/config/environments")
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
|
|
|
|
2010-01-27 15:20:32 -05:00
|
|
|
def teardown
|
|
|
|
FileUtils.rm_rf(new_app) if File.directory?(new_app)
|
|
|
|
end
|
|
|
|
|
2010-07-19 11:53:14 -04:00
|
|
|
test "Rails.application is nil until app is initialized" do
|
2010-01-26 06:14:48 -05:00
|
|
|
require 'rails'
|
2010-07-19 11:53:14 -04:00
|
|
|
assert_nil Rails.application
|
2010-01-26 06:14:48 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2010-07-19 11:53:14 -04:00
|
|
|
assert_equal AppTemplate::Application.instance, Rails.application
|
2010-01-26 06:14:48 -05:00
|
|
|
end
|
|
|
|
|
2010-07-19 11:53:14 -04:00
|
|
|
test "Rails.application responds to all instance methods" do
|
2010-01-27 15:20:32 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2010-07-19 11:53:14 -04:00
|
|
|
assert_respond_to Rails.application, :routes_reloader
|
|
|
|
assert_equal Rails.application.routes_reloader, AppTemplate::Application.routes_reloader
|
2010-01-30 06:30:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "Rails::Application responds to paths" do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_respond_to AppTemplate::Application, :paths
|
2010-10-06 11:18:59 -04:00
|
|
|
assert_equal AppTemplate::Application.paths["app/views"].expanded, ["#{app_path}/app/views"]
|
2010-01-27 15:20:32 -05:00
|
|
|
end
|
|
|
|
|
2009-10-08 21:12:28 -04:00
|
|
|
test "the application root is set correctly" do
|
2009-10-14 19:13:45 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2009-10-16 15:49:39 -04:00
|
|
|
assert_equal Pathname.new(app_path), Rails.application.root
|
2009-10-14 19:13:45 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 01:49:40 -05:00
|
|
|
test "the application root can be seen from the application singleton" do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal Pathname.new(app_path), AppTemplate::Application.root
|
|
|
|
end
|
|
|
|
|
2009-10-14 19:13:45 -04:00
|
|
|
test "the application root can be set" do
|
2009-12-22 20:03:23 -05:00
|
|
|
copy_app
|
2009-10-14 19:13:45 -04:00
|
|
|
add_to_config <<-RUBY
|
2009-12-22 20:03:23 -05:00
|
|
|
config.root = '#{new_app}'
|
2009-10-14 19:13:45 -04:00
|
|
|
RUBY
|
2010-01-22 10:24:44 -05:00
|
|
|
|
2009-12-22 20:03:23 -05:00
|
|
|
use_frameworks []
|
2010-01-22 10:24:44 -05:00
|
|
|
|
2009-10-14 19:13:45 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2009-12-22 20:03:23 -05:00
|
|
|
assert_equal Pathname.new(new_app), Rails.application.root
|
2009-10-14 19:13:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "the application root is Dir.pwd if there is no config.ru" do
|
|
|
|
File.delete("#{app_path}/config.ru")
|
|
|
|
|
2009-12-22 20:03:23 -05:00
|
|
|
use_frameworks []
|
|
|
|
|
|
|
|
Dir.chdir("#{app_path}") do
|
2009-10-14 19:13:45 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2009-12-22 20:03:23 -05:00
|
|
|
assert_equal Pathname.new("#{app_path}"), Rails.application.root
|
2009-10-14 19:13:45 -04:00
|
|
|
end
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
2009-12-02 13:14:02 -05:00
|
|
|
|
2010-01-26 07:57:11 -05:00
|
|
|
test "Rails.root should be a Pathname" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.root = "#{app_path}"
|
|
|
|
RUBY
|
2009-12-23 22:00:20 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2010-01-26 07:57:11 -05:00
|
|
|
assert_instance_of Pathname, Rails.root
|
2009-12-02 13:14:02 -05:00
|
|
|
end
|
2009-12-16 01:07:12 -05:00
|
|
|
|
|
|
|
test "marking the application as threadsafe sets the correct config variables" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.threadsafe!
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/application"
|
2010-02-01 05:40:27 -05:00
|
|
|
assert AppTemplate::Application.config.allow_concurrency
|
2009-12-16 01:07:12 -05:00
|
|
|
end
|
|
|
|
|
2010-12-09 15:18:41 -05:00
|
|
|
test "asset_path defaults to nil for application" do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal nil, AppTemplate::Application.config.asset_path
|
|
|
|
end
|
|
|
|
|
2009-12-16 01:07:12 -05:00
|
|
|
test "the application can be marked as threadsafe when there are no frameworks" do
|
|
|
|
FileUtils.rm_rf("#{app_path}/config/environments")
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.threadsafe!
|
|
|
|
RUBY
|
|
|
|
|
2009-12-31 13:36:24 -05:00
|
|
|
use_frameworks []
|
|
|
|
|
2009-12-16 01:07:12 -05:00
|
|
|
assert_nothing_raised do
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
end
|
|
|
|
end
|
2009-12-29 22:04:14 -05:00
|
|
|
|
2010-04-26 03:04:04 -04:00
|
|
|
test "frameworks are not preloaded by default" do
|
2009-12-29 22:04:14 -05:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert ActionController.autoload?(:RecordIdentifier)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "frameworks are preloaded with config.preload_frameworks is set" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.preload_frameworks = true
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert !ActionController.autoload?(:RecordIdentifier)
|
|
|
|
end
|
2009-12-31 14:57:48 -05:00
|
|
|
|
2010-01-20 23:48:27 -05:00
|
|
|
test "filter_parameters should be able to set via config.filter_parameters" do
|
|
|
|
add_to_config <<-RUBY
|
2010-01-21 10:50:11 -05:00
|
|
|
config.filter_parameters += [ :foo, 'bar', lambda { |key, value|
|
2010-01-20 23:48:27 -05:00
|
|
|
value = value.reverse if key =~ /baz/
|
2010-01-21 10:50:11 -05:00
|
|
|
}]
|
2010-01-20 23:48:27 -05:00
|
|
|
RUBY
|
2010-01-26 07:57:11 -05:00
|
|
|
|
2010-01-20 23:48:27 -05:00
|
|
|
assert_nothing_raised do
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
end
|
|
|
|
end
|
2010-01-27 11:46:55 -05:00
|
|
|
|
|
|
|
test "config.to_prepare is forwarded to ActionDispatch" do
|
|
|
|
$prepared = false
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.to_prepare do
|
|
|
|
$prepared = true
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
assert !$prepared
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require 'rack/test'
|
|
|
|
extend Rack::Test::Methods
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert $prepared
|
|
|
|
end
|
2010-02-23 20:03:06 -05:00
|
|
|
|
2010-08-15 19:29:27 -04:00
|
|
|
def assert_utf8
|
|
|
|
if RUBY_VERSION < '1.9'
|
|
|
|
assert_equal "UTF8", $KCODE
|
|
|
|
else
|
|
|
|
assert_equal Encoding::UTF_8, Encoding.default_external
|
|
|
|
assert_equal Encoding::UTF_8, Encoding.default_internal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "skipping config.encoding still results in 'utf-8' as the default" do
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
assert_utf8
|
|
|
|
end
|
|
|
|
|
2010-04-06 20:24:29 -04:00
|
|
|
test "config.encoding sets the default encoding" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.encoding = "utf-8"
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/application"
|
2010-08-15 19:29:27 -04:00
|
|
|
assert_utf8
|
2010-04-06 20:24:29 -04:00
|
|
|
end
|
|
|
|
|
2010-04-08 06:52:37 -04:00
|
|
|
test "config.paths.public sets Rails.public_path" do
|
|
|
|
add_to_config <<-RUBY
|
2010-10-06 11:18:59 -04:00
|
|
|
config.paths["public"] = "somewhere"
|
2010-04-08 06:52:37 -04:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
assert_equal File.join(app_path, "somewhere"), Rails.public_path
|
|
|
|
end
|
|
|
|
|
2010-04-05 04:52:47 -04:00
|
|
|
test "config.secret_token is sent in env" do
|
|
|
|
make_basic_app do |app|
|
2010-05-17 19:43:06 -04:00
|
|
|
app.config.secret_token = 'b3c631c314c0bbca50c1b2843150fe33'
|
2010-04-26 03:04:04 -04:00
|
|
|
app.config.session_store :disabled
|
2010-04-05 04:52:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
|
|
|
cookies.signed[:some_key] = "some_value"
|
|
|
|
render :text => env["action_dispatch.secret_token"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
2010-05-17 19:43:06 -04:00
|
|
|
assert_equal 'b3c631c314c0bbca50c1b2843150fe33', last_response.body
|
2010-04-05 04:52:47 -04:00
|
|
|
end
|
|
|
|
|
2010-03-19 14:09:41 -04:00
|
|
|
test "protect from forgery is the default in a new app" do
|
2010-03-26 19:58:55 -04:00
|
|
|
make_basic_app
|
2010-03-19 14:09:41 -04:00
|
|
|
|
2010-03-26 19:58:55 -04:00
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
protect_from_forgery
|
2010-03-19 14:09:41 -04:00
|
|
|
|
2010-03-26 19:58:55 -04:00
|
|
|
def index
|
2010-09-11 05:04:19 -04:00
|
|
|
render :inline => "<%= csrf_meta_tags %>"
|
2010-03-19 14:09:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert last_response.body =~ /csrf\-param/
|
|
|
|
end
|
2010-04-13 19:15:18 -04:00
|
|
|
|
|
|
|
test "config.action_controller.perform_caching = true" do
|
2010-04-26 03:04:04 -04:00
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.action_controller.perform_caching = true
|
|
|
|
end
|
2010-04-13 19:15:18 -04:00
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
2010-04-19 00:49:48 -04:00
|
|
|
@@count = 0
|
|
|
|
|
2010-04-13 19:15:18 -04:00
|
|
|
caches_action :index
|
|
|
|
def index
|
2010-04-19 00:49:48 -04:00
|
|
|
@@count += 1
|
|
|
|
render :text => @@count
|
2010-04-13 19:15:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
res = last_response.body
|
|
|
|
get "/"
|
|
|
|
assert_equal res, last_response.body # value should be unchanged
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_controller.perform_caching = false" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.action_controller.perform_caching = false
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
2010-04-19 00:49:48 -04:00
|
|
|
@@count = 0
|
|
|
|
|
2010-04-13 19:15:18 -04:00
|
|
|
caches_action :index
|
|
|
|
def index
|
2010-04-19 00:49:48 -04:00
|
|
|
@@count += 1
|
|
|
|
render :text => @@count
|
2010-04-13 19:15:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
res = last_response.body
|
|
|
|
get "/"
|
|
|
|
assert_not_equal res, last_response.body
|
|
|
|
end
|
2010-07-22 16:11:32 -04:00
|
|
|
|
|
|
|
test "config.asset_path is not passed through env" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.asset_path = "/omg%s"
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
|
|
|
render :inline => "<%= image_path('foo.jpg') %>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert_equal "/omg/images/foo.jpg", last_response.body
|
|
|
|
end
|
2010-12-16 15:37:48 -05:00
|
|
|
|
|
|
|
test "config.action_view.cache_template_loading with cache_classes default" do
|
|
|
|
add_to_config "config.cache_classes = true"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require 'action_view/base'
|
|
|
|
|
|
|
|
assert ActionView::Resolver.caching?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_view.cache_template_loading without cache_classes default" do
|
|
|
|
add_to_config "config.cache_classes = false"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require 'action_view/base'
|
|
|
|
|
|
|
|
assert !ActionView::Resolver.caching?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_view.cache_template_loading = false" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = true
|
|
|
|
config.action_view.cache_template_loading = false
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require 'action_view/base'
|
|
|
|
|
|
|
|
assert !ActionView::Resolver.caching?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_view.cache_template_loading = true" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = false
|
|
|
|
config.action_view.cache_template_loading = true
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require 'action_view/base'
|
|
|
|
|
|
|
|
assert ActionView::Resolver.caching?
|
|
|
|
end
|
2011-02-22 14:25:38 -05:00
|
|
|
|
|
|
|
test "config.action_dispatch.show_exceptions is sent in env" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.action_dispatch.show_exceptions = true
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
|
|
|
render :text => env["action_dispatch.show_exceptions"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert_equal 'true', last_response.body
|
|
|
|
end
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
2009-12-06 20:23:43 -05:00
|
|
|
end
|