2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-12-22 21:48:19 -05:00
|
|
|
class SettingsTest < Test::Unit::TestCase
|
2009-03-26 11:42:13 -04:00
|
|
|
setup do
|
2009-10-14 07:01:06 -04:00
|
|
|
@base = Sinatra.new(Sinatra::Base)
|
2009-12-22 21:50:40 -05:00
|
|
|
@base.set :environment, :foo
|
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
@application = Sinatra.new(Sinatra::Application)
|
2009-12-22 21:50:40 -05:00
|
|
|
@application.set :environment, :foo
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it 'sets settings to literal values' do
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.set(:foo, 'bar')
|
|
|
|
assert @base.respond_to?(:foo)
|
|
|
|
assert_equal 'bar', @base.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it 'sets settings to Procs' do
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.set(:foo, Proc.new { 'baz' })
|
|
|
|
assert @base.respond_to?(:foo)
|
|
|
|
assert_equal 'baz', @base.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it "sets multiple settings with a Hash" do
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.set :foo => 1234,
|
2008-12-13 16:06:02 -05:00
|
|
|
:bar => 'Hello World',
|
|
|
|
:baz => Proc.new { 'bizzle' }
|
2009-03-26 14:40:14 -04:00
|
|
|
assert_equal 1234, @base.foo
|
|
|
|
assert_equal 'Hello World', @base.bar
|
|
|
|
assert_equal 'bizzle', @base.baz
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it 'inherits settings methods when subclassed' do
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.set :foo, 'bar'
|
|
|
|
@base.set :biz, Proc.new { 'baz' }
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 14:40:14 -04:00
|
|
|
sub = Class.new(@base)
|
2009-01-14 17:00:26 -05:00
|
|
|
assert sub.respond_to?(:foo)
|
|
|
|
assert_equal 'bar', sub.foo
|
|
|
|
assert sub.respond_to?(:biz)
|
|
|
|
assert_equal 'baz', sub.biz
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it 'overrides settings in subclass' do
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.set :foo, 'bar'
|
|
|
|
@base.set :biz, Proc.new { 'baz' }
|
|
|
|
sub = Class.new(@base)
|
2008-12-13 16:06:02 -05:00
|
|
|
sub.set :foo, 'bling'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 'bling', sub.foo
|
2009-03-26 14:40:14 -04:00
|
|
|
assert_equal 'bar', @base.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates setter methods when first defined' do
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.set :foo, 'bar'
|
|
|
|
assert @base.respond_to?('foo=')
|
|
|
|
@base.foo = 'biz'
|
|
|
|
assert_equal 'biz', @base.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates predicate methods when first defined' do
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.set :foo, 'hello world'
|
|
|
|
assert @base.respond_to?(:foo?)
|
|
|
|
assert @base.foo?
|
|
|
|
@base.set :foo, nil
|
|
|
|
assert !@base.foo?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses existing setter methods if detected' do
|
2009-03-26 14:40:14 -04:00
|
|
|
class << @base
|
2008-12-13 16:06:02 -05:00
|
|
|
def foo
|
|
|
|
@foo
|
|
|
|
end
|
|
|
|
def foo=(value)
|
|
|
|
@foo = 'oops'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.set :foo, 'bam'
|
|
|
|
assert_equal 'oops', @base.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it "sets multiple settings to true with #enable" do
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.enable :sessions, :foo, :bar
|
|
|
|
assert @base.sessions
|
|
|
|
assert @base.foo
|
|
|
|
assert @base.bar
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it "sets multiple settings to false with #disable" do
|
2009-03-26 14:40:14 -04:00
|
|
|
@base.disable :sessions, :foo, :bar
|
|
|
|
assert !@base.sessions
|
|
|
|
assert !@base.foo
|
|
|
|
assert !@base.bar
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-12-21 05:11:25 -05:00
|
|
|
|
2009-01-21 17:55:04 -05:00
|
|
|
|
2009-10-11 16:36:39 -04:00
|
|
|
it 'is accessible from instances via #settings' do
|
2009-12-22 21:50:40 -05:00
|
|
|
assert_equal :foo, @base.new.settings.environment
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'methodoverride' do
|
|
|
|
it 'is disabled on Base' do
|
|
|
|
assert ! @base.methodoverride?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is enabled on Application' do
|
|
|
|
assert @application.methodoverride?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enables MethodOverride middleware' do
|
|
|
|
@base.set :methodoverride, true
|
|
|
|
@base.put('/') { 'okay' }
|
|
|
|
@app = @base
|
|
|
|
post '/', {'_method'=>'PUT'}, {}
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'okay', body
|
|
|
|
end
|
2009-10-11 16:36:39 -04:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'clean_trace' do
|
|
|
|
def clean_backtrace(trace)
|
2009-03-26 14:40:14 -04:00
|
|
|
Sinatra::Base.new.send(:clean_backtrace, trace)
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-01-21 17:55:04 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'is enabled on Base' do
|
|
|
|
assert @base.clean_trace?
|
|
|
|
end
|
2009-01-21 17:55:04 -05:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
it 'is enabled on Application' do
|
|
|
|
assert @application.clean_trace?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-01-21 17:55:04 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'does nothing when disabled' do
|
|
|
|
backtrace = [
|
|
|
|
"./lib/sinatra/base.rb",
|
|
|
|
"./myapp:42",
|
|
|
|
("#{Gem.dir}/some/lib.rb" if defined?(Gem))
|
|
|
|
].compact
|
2009-03-26 14:40:14 -04:00
|
|
|
|
|
|
|
klass = Class.new(Sinatra::Base)
|
|
|
|
klass.disable :clean_trace
|
|
|
|
|
|
|
|
assert_equal backtrace, klass.new.send(:clean_backtrace, backtrace)
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-01-21 17:55:04 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'removes sinatra lib paths from backtrace when enabled' do
|
|
|
|
backtrace = [
|
|
|
|
"./lib/sinatra/base.rb",
|
|
|
|
"./lib/sinatra/compat.rb:42",
|
|
|
|
"./lib/sinatra/main.rb:55 in `foo'"
|
|
|
|
]
|
|
|
|
assert clean_backtrace(backtrace).empty?
|
|
|
|
end
|
2009-01-21 17:55:04 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'removes ./ prefix from backtrace paths when enabled' do
|
|
|
|
assert_equal ['myapp.rb:42'], clean_backtrace(['./myapp.rb:42'])
|
2009-01-21 17:55:04 -05:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
if defined?(Gem)
|
|
|
|
it 'removes gem lib paths from backtrace when enabled' do
|
|
|
|
assert clean_backtrace(["#{Gem.dir}/some/lib"]).empty?
|
|
|
|
end
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'run' do
|
|
|
|
it 'is disabled on Base' do
|
|
|
|
assert ! @base.run?
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it 'is enabled on Application except in test environment' do
|
2009-10-14 07:01:06 -04:00
|
|
|
assert @application.run?
|
2009-03-26 11:42:13 -04:00
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
@application.set :environment, :test
|
|
|
|
assert ! @application.run?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'raise_errors' do
|
|
|
|
it 'is enabled on Base' do
|
|
|
|
assert @base.raise_errors?
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
it 'is enabled on Application only in test' do
|
|
|
|
assert ! @application.raise_errors?
|
2009-03-26 11:42:13 -04:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
@application.set(:environment, :test)
|
|
|
|
assert @application.raise_errors?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'show_exceptions' do
|
2009-12-22 21:50:40 -05:00
|
|
|
it 'is disabled on Base' do
|
|
|
|
assert ! @base.show_exceptions?
|
2009-06-06 01:34:03 -04:00
|
|
|
end
|
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it 'is disabled on Application except in development' do
|
2009-10-14 07:01:06 -04:00
|
|
|
assert ! @application.show_exceptions?
|
2009-01-23 05:37:43 -05:00
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
@application.set(:environment, :development)
|
|
|
|
assert @application.show_exceptions?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-01-23 05:37:43 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'returns a friendly 500' do
|
2009-10-14 07:01:06 -04:00
|
|
|
klass = Sinatra.new(Sinatra::Application)
|
2009-03-26 14:40:14 -04:00
|
|
|
mock_app(klass) {
|
2009-03-26 11:42:13 -04:00
|
|
|
enable :show_exceptions
|
2009-01-23 05:37:43 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/' do
|
|
|
|
raise StandardError
|
|
|
|
end
|
|
|
|
}
|
2009-01-23 05:37:43 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert body.include?("StandardError")
|
|
|
|
assert body.include?("<code>show_exceptions</code> option")
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'dump_errors' do
|
|
|
|
it 'is disabled on Base' do
|
|
|
|
assert ! @base.dump_errors?
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
it 'is enabled on Application' do
|
|
|
|
assert @application.dump_errors?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'dumps exception with backtrace to rack.errors' do
|
2009-10-14 07:01:06 -04:00
|
|
|
klass = Sinatra.new(Sinatra::Application)
|
2009-03-26 14:40:14 -04:00
|
|
|
|
|
|
|
mock_app(klass) {
|
|
|
|
disable :raise_errors
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
error do
|
|
|
|
error = @env['rack.errors'].instance_variable_get(:@error)
|
|
|
|
error.rewind
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
error.read
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/' do
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
}
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/'
|
2009-12-22 21:48:19 -05:00
|
|
|
assert body.include?("RuntimeError") && body.include?("settings_test.rb")
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'sessions' do
|
|
|
|
it 'is disabled on Base' do
|
|
|
|
assert ! @base.sessions?
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
it 'is disabled on Application' do
|
|
|
|
assert ! @application.sessions?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'logging' do
|
|
|
|
it 'is disabled on Base' do
|
|
|
|
assert ! @base.logging?
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
it 'is enabled on Application except in test environment' do
|
2009-10-14 07:01:06 -04:00
|
|
|
assert @application.logging?
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
@application.set :environment, :test
|
|
|
|
assert ! @application.logging
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'static' do
|
|
|
|
it 'is disabled on Base' do
|
|
|
|
assert ! @base.static?
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
it 'is enabled on Application' do
|
|
|
|
assert @application.static?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'host' do
|
|
|
|
it 'defaults to 0.0.0.0' do
|
|
|
|
assert_equal '0.0.0.0', @base.host
|
2009-10-14 07:01:06 -04:00
|
|
|
assert_equal '0.0.0.0', @application.host
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'port' do
|
|
|
|
it 'defaults to 4567' do
|
|
|
|
assert_equal 4567, @base.port
|
2009-10-14 07:01:06 -04:00
|
|
|
assert_equal 4567, @application.port
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'server' do
|
|
|
|
it 'is one of thin, mongrel, webrick' do
|
|
|
|
assert_equal %w[thin mongrel webrick], @base.server
|
2009-10-14 07:01:06 -04:00
|
|
|
assert_equal %w[thin mongrel webrick], @application.server
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'app_file' do
|
|
|
|
it 'is nil' do
|
2009-12-22 21:50:40 -05:00
|
|
|
assert_nil @base.app_file
|
|
|
|
assert_nil @application.app_file
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'root' do
|
|
|
|
it 'is nil if app_file is not set' do
|
|
|
|
assert @base.root.nil?
|
2009-10-14 07:01:06 -04:00
|
|
|
assert @application.root.nil?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'is equal to the expanded basename of app_file' do
|
|
|
|
@base.app_file = __FILE__
|
|
|
|
assert_equal File.expand_path(File.dirname(__FILE__)), @base.root
|
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
@application.app_file = __FILE__
|
|
|
|
assert_equal File.expand_path(File.dirname(__FILE__)), @application.root
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'views' do
|
|
|
|
it 'is nil if root is not set' do
|
|
|
|
assert @base.views.nil?
|
2009-10-14 07:01:06 -04:00
|
|
|
assert @application.views.nil?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is set to root joined with views/' do
|
|
|
|
@base.root = File.dirname(__FILE__)
|
|
|
|
assert_equal File.dirname(__FILE__) + "/views", @base.views
|
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
@application.root = File.dirname(__FILE__)
|
|
|
|
assert_equal File.dirname(__FILE__) + "/views", @application.views
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'public' do
|
|
|
|
it 'is nil if root is not set' do
|
|
|
|
assert @base.public.nil?
|
2009-10-14 07:01:06 -04:00
|
|
|
assert @application.public.nil?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is set to root joined with public/' do
|
|
|
|
@base.root = File.dirname(__FILE__)
|
|
|
|
assert_equal File.dirname(__FILE__) + "/public", @base.public
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
@application.root = File.dirname(__FILE__)
|
|
|
|
assert_equal File.dirname(__FILE__) + "/public", @application.public
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'lock' do
|
|
|
|
it 'is disabled by default' do
|
|
|
|
assert ! @base.lock?
|
2009-12-22 21:50:40 -05:00
|
|
|
assert ! @application.lock?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
end
|