require File.expand_path('../helper', __FILE__)
class SettingsTest < Minitest::Test
setup do
@base = Sinatra.new(Sinatra::Base)
@base.set :environment => :foo, :app_file => nil
@application = Sinatra.new(Sinatra::Application)
@application.set :environment => :foo, :app_file => nil
end
it 'sets settings to literal values' do
@base.set(:foo, 'bar')
assert @base.respond_to?(:foo)
assert_equal 'bar', @base.foo
end
it 'sets settings to Procs' do
@base.set(:foo, Proc.new { 'baz' })
assert @base.respond_to?(:foo)
assert_equal 'baz', @base.foo
end
it 'sets settings using a block' do
@base.set(:foo){ 'baz' }
assert @base.respond_to?(:foo)
assert_equal 'baz', @base.foo
end
it 'raises an error with a value and a block' do
assert_raises ArgumentError do
@base.set(:fiz, 'boom!'){ 'baz' }
end
assert !@base.respond_to?(:fiz)
end
it 'raises an error without value and block' do
assert_raises(ArgumentError) { @base.set(:fiz) }
assert !@base.respond_to?(:fiz)
end
it 'allows setting a value to the app class' do
@base.set :base, @base
assert @base.respond_to?(:base)
assert_equal @base, @base.base
end
it 'raises an error with the app class as value and a block' do
assert_raises ArgumentError do
@base.set(:fiz, @base) { 'baz' }
end
assert !@base.respond_to?(:fiz)
end
it "sets multiple settings with a Hash" do
@base.set :foo => 1234,
:bar => 'Hello World',
:baz => Proc.new { 'bizzle' }
assert_equal 1234, @base.foo
assert_equal 'Hello World', @base.bar
assert_equal 'bizzle', @base.baz
end
it 'sets multiple settings using #each' do
@base.set [["foo", "bar"]]
assert_equal "bar", @base.foo
end
it 'inherits settings methods when subclassed' do
@base.set :foo, 'bar'
@base.set :biz, Proc.new { 'baz' }
sub = Class.new(@base)
assert sub.respond_to?(:foo)
assert_equal 'bar', sub.foo
assert sub.respond_to?(:biz)
assert_equal 'baz', sub.biz
end
it 'overrides settings in subclass' do
@base.set :foo, 'bar'
@base.set :biz, Proc.new { 'baz' }
sub = Class.new(@base)
sub.set :foo, 'bling'
assert_equal 'bling', sub.foo
assert_equal 'bar', @base.foo
end
it 'creates setter methods when first defined' do
@base.set :foo, 'bar'
assert @base.respond_to?('foo=')
@base.foo = 'biz'
assert_equal 'biz', @base.foo
end
it 'creates predicate methods when first defined' do
@base.set :foo, 'hello world'
assert @base.respond_to?(:foo?)
assert @base.foo?
@base.set :foo, nil
assert !@base.foo?
end
it 'uses existing setter methods if detected' do
class << @base
def foo
@foo
end
def foo=(value)
@foo = 'oops'
end
end
@base.set :foo, 'bam'
assert_equal 'oops', @base.foo
end
it 'merges values of multiple set calls if those are hashes' do
@base.set :foo, :a => 1
sub = Class.new(@base)
sub.set :foo, :b => 2
assert_equal({:a => 1, :b => 2}, sub.foo)
end
it 'merging does not affect the superclass' do
@base.set :foo, :a => 1
sub = Class.new(@base)
sub.set :foo, :b => 2
assert_equal({:a => 1}, @base.foo)
end
it 'is possible to change a value from a hash to something else' do
@base.set :foo, :a => 1
@base.set :foo, :bar
assert_equal(:bar, @base.foo)
end
it 'merges values with values of the superclass if those are hashes' do
@base.set :foo, :a => 1
@base.set :foo, :b => 2
assert_equal({:a => 1, :b => 2}, @base.foo)
end
it "sets multiple settings to true with #enable" do
@base.enable :sessions, :foo, :bar
assert @base.sessions
assert @base.foo
assert @base.bar
end
it "sets multiple settings to false with #disable" do
@base.disable :sessions, :foo, :bar
assert !@base.sessions
assert !@base.foo
assert !@base.bar
end
it 'is accessible from instances via #settings' do
assert_equal :foo, @base.new!.settings.environment
end
it 'is accessible from class via #settings' do
assert_equal :foo, @base.settings.environment
end
describe 'default_content_type' do
it 'defaults to html' do
assert_equal 'text/html', @base.default_content_type
end
it 'can be changed' do
@base.set :default_content_type, 'application/json'
@base.get('/') { '{"a":1}' }
@app = @base
get '/'
assert_equal 200, status
assert_equal 'application/json', response.content_type
end
it 'can be disabled' do
@base.set :default_content_type, nil
@base.error(404) { "" }
@app = @base
get '/'
assert_equal 404, status
assert_nil response.content_type
assert_empty body
end
it 'may emit content without a content-type (to be sniffed)' do
@base.set :default_content_type, nil
@base.get('/') { raise Sinatra::BadRequest, "This is a drill" }
@app = @base
get '/'
assert_equal 400, status
assert_nil response.content_type
assert_body "This is a drill"
end
end
describe 'methodoverride' do
it 'is disabled on Base' do
assert ! @base.method_override?
end
it 'is enabled on Application' do
assert @application.method_override?
end
it 'enables MethodOverride middleware' do
@base.set :method_override, true
@base.put('/') { 'okay' }
@app = @base
post '/', {'_method'=>'PUT'}, {}
assert_equal 200, status
assert_equal 'okay', body
end
it 'is backward compatible with methodoverride' do
assert ! @base.methodoverride?
@base.enable :methodoverride
assert @base.methodoverride?
end
it 'ignores bundler/inline from callers' do
@application.stub(:caller, ->(_){ ['/path/to/bundler/inline.rb', $0] }) do
assert_equal File.expand_path($0), File.expand_path(@application.send(:caller_files).first)
end
end
end
describe 'run' do
it 'is disabled on Base' do
assert ! @base.run?
end
it 'is enabled on Application except in test environment' do
assert @application.run?
@application.set :environment, :test
assert ! @application.run?
end
end
describe 'raise_errors' do
it 'is enabled on Base only in test' do
assert ! @base.raise_errors?
@base.set(:environment, :test)
assert @base.raise_errors?
end
it 'is enabled on Application only in test' do
assert ! @application.raise_errors?
@application.set(:environment, :test)
assert @application.raise_errors?
end
end
describe 'show_exceptions' do
it 'is disabled on Base except under development' do
assert ! @base.show_exceptions?
@base.environment = :development
assert @base.show_exceptions?
end
it 'is disabled on Application except in development' do
assert ! @application.show_exceptions?
@application.set(:environment, :development)
assert @application.show_exceptions?
end
it 'returns a friendly 500' do
klass = Sinatra.new(Sinatra::Application)
mock_app(klass) {
enable :show_exceptions
get '/' do
raise StandardError
end
}
get '/'
assert_equal 500, status
assert body.include?("StandardError")
assert body.include?("show_exceptions
setting")
end
it 'does not attempt to show unparseable query parameters' do
klass = Sinatra.new(Sinatra::Application)
mock_app(klass) {
enable :show_exceptions
get '/' do
raise Sinatra::BadRequest
end
}
get '/'
assert_equal 400, status
refute body.include?('