2010-09-07 03:43:31 -04:00
|
|
|
# encoding: UTF-8
|
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
|
|
|
|
|
2010-02-05 15:12:28 -05:00
|
|
|
it 'sets settings using a block' do
|
|
|
|
@base.set(:foo){ 'baz' }
|
|
|
|
assert @base.respond_to?(:foo)
|
|
|
|
assert_equal 'baz', @base.foo
|
|
|
|
end
|
|
|
|
|
2010-02-05 16:45:52 -05:00
|
|
|
it 'raises an error with a value and a block' do
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
@base.set(:fiz, 'boom!'){ 'baz' }
|
|
|
|
end
|
|
|
|
assert !@base.respond_to?(:fiz)
|
2010-02-05 15:12:28 -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
|
|
|
|
|
2010-07-11 05:11:18 -04:00
|
|
|
it 'sets multiple settings using #each' do
|
|
|
|
@base.set [["foo", "bar"]]
|
|
|
|
assert_equal "bar", @base.foo
|
|
|
|
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-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
|
|
|
|
|
2010-09-02 07:58:23 -04:00
|
|
|
it 'is accessible from class via #settings' do
|
|
|
|
assert_equal :foo, @base.settings.environment
|
|
|
|
end
|
|
|
|
|
2009-12-22 21:50:40 -05:00
|
|
|
describe 'methodoverride' do
|
|
|
|
it 'is disabled on Base' do
|
2009-12-13 12:36:01 -05:00
|
|
|
assert ! @base.method_override?
|
2009-12-22 21:50:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is enabled on Application' do
|
2009-12-13 12:36:01 -05:00
|
|
|
assert @application.method_override?
|
2009-12-22 21:50:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enables MethodOverride middleware' do
|
2009-12-13 12:36:01 -05:00
|
|
|
@base.set :method_override, true
|
2009-12-22 21:50:40 -05:00
|
|
|
@base.put('/') { 'okay' }
|
|
|
|
@app = @base
|
|
|
|
post '/', {'_method'=>'PUT'}, {}
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'okay', body
|
|
|
|
end
|
2009-12-13 12:36:01 -05:00
|
|
|
|
|
|
|
it 'is backward compatible with methodoverride' do
|
|
|
|
assert ! @base.methodoverride?
|
|
|
|
@base.enable :methodoverride
|
|
|
|
assert @base.methodoverride?
|
|
|
|
end
|
2009-10-11 16:36:39 -04: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
|
2010-03-04 08:57:26 -05:00
|
|
|
it 'is enabled on Base only in test' do
|
|
|
|
assert ! @base.raise_errors?
|
|
|
|
|
|
|
|
@base.set(:environment, :test)
|
2009-03-26 11:42:13 -04:00
|
|
|
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
|
2010-01-28 11:11:19 -05:00
|
|
|
it 'is disabled on Base except under development' do
|
2009-12-22 21:50:40 -05:00
|
|
|
assert ! @base.show_exceptions?
|
2010-01-28 11:11:19 -05:00
|
|
|
@base.environment = :development
|
|
|
|
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")
|
2009-12-22 21:51:59 -05:00
|
|
|
assert body.include?("<code>show_exceptions</code> setting")
|
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 'dump_errors' do
|
2010-03-04 08:57:26 -05:00
|
|
|
it 'is disabled on Base in test' do
|
|
|
|
@base.environment = :test
|
2009-03-26 11:42:13 -04:00
|
|
|
assert ! @base.dump_errors?
|
2010-01-28 11:11:19 -05:00
|
|
|
@base.environment = :development
|
|
|
|
assert @base.dump_errors?
|
2010-03-04 08:57:26 -05:00
|
|
|
@base.environment = :production
|
|
|
|
assert @base.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) {
|
2010-03-04 08:57:26 -05:00
|
|
|
enable :dump_errors
|
2009-03-26 14:40:14 -04:00
|
|
|
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
|
2010-01-28 10:53:42 -05:00
|
|
|
it 'is disabled on Base by default' do
|
2009-03-26 11:42:13 -04:00
|
|
|
assert ! @base.static?
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2010-01-28 10:53:42 -05:00
|
|
|
it 'is enabled on Base when public is set and exists' do
|
|
|
|
@base.set :environment, :development
|
|
|
|
@base.set :public, File.dirname(__FILE__)
|
|
|
|
assert @base.static?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is enabled on Base when root is set and root/public exists' do
|
|
|
|
@base.set :environment, :development
|
|
|
|
@base.set :root, File.dirname(__FILE__)
|
|
|
|
assert @base.static?
|
|
|
|
end
|
|
|
|
|
2010-03-04 08:57:26 -05:00
|
|
|
it 'is disabled on Application by default' do
|
|
|
|
assert ! @application.static?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is enabled on Application when public is set and exists' do
|
|
|
|
@application.set :environment, :development
|
|
|
|
@application.set :public, File.dirname(__FILE__)
|
|
|
|
assert @application.static?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is enabled on Application when root is set and root/public exists' do
|
|
|
|
@application.set :environment, :development
|
|
|
|
@application.set :root, File.dirname(__FILE__)
|
2009-10-14 07:01:06 -04:00
|
|
|
assert @application.static?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2010-02-04 20:16:05 -05:00
|
|
|
describe 'bind' do
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'defaults to 0.0.0.0' do
|
2010-02-04 20:16:05 -05:00
|
|
|
assert_equal '0.0.0.0', @base.bind
|
|
|
|
assert_equal '0.0.0.0', @application.bind
|
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
|
2010-09-07 03:43:31 -04:00
|
|
|
|
|
|
|
describe 'default_encoding' do
|
|
|
|
it 'allows unicode strings in ascii templates per default (1.9)' do
|
|
|
|
@base.set :views, File.dirname(__FILE__) + "/views"
|
|
|
|
@base.new.haml(:ascii, {}, :value => "åkej")
|
|
|
|
end
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|