2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
describe 'Options' do
|
2009-01-13 12:53:53 -05:00
|
|
|
before { @app = Class.new(Sinatra::Base) }
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
it 'sets options to literal values' do
|
|
|
|
@app.set(:foo, 'bar')
|
2009-01-14 17:00:26 -05:00
|
|
|
assert @app.respond_to?(:foo)
|
|
|
|
assert_equal 'bar', @app.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets options to Procs' do
|
|
|
|
@app.set(:foo, Proc.new { 'baz' })
|
2009-01-14 17:00:26 -05:00
|
|
|
assert @app.respond_to?(:foo)
|
|
|
|
assert_equal 'baz', @app.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets multiple options with a Hash" do
|
|
|
|
@app.set :foo => 1234,
|
|
|
|
:bar => 'Hello World',
|
|
|
|
:baz => Proc.new { 'bizzle' }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 1234, @app.foo
|
|
|
|
assert_equal 'Hello World', @app.bar
|
|
|
|
assert_equal 'bizzle', @app.baz
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'inherits option methods when subclassed' do
|
|
|
|
@app.set :foo, 'bar'
|
|
|
|
@app.set :biz, Proc.new { 'baz' }
|
|
|
|
|
|
|
|
sub = Class.new(@app)
|
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
|
|
|
|
|
|
|
|
it 'overrides options in subclass' do
|
|
|
|
@app.set :foo, 'bar'
|
|
|
|
@app.set :biz, Proc.new { 'baz' }
|
|
|
|
sub = Class.new(@app)
|
|
|
|
sub.set :foo, 'bling'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 'bling', sub.foo
|
|
|
|
assert_equal 'bar', @app.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates setter methods when first defined' do
|
|
|
|
@app.set :foo, 'bar'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert @app.respond_to?('foo=')
|
2008-12-13 16:06:02 -05:00
|
|
|
@app.foo = 'biz'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 'biz', @app.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates predicate methods when first defined' do
|
|
|
|
@app.set :foo, 'hello world'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert @app.respond_to?(:foo?)
|
|
|
|
assert @app.foo?
|
2008-12-13 16:06:02 -05:00
|
|
|
@app.set :foo, nil
|
2009-01-14 17:00:26 -05:00
|
|
|
assert !@app.foo?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses existing setter methods if detected' do
|
|
|
|
class << @app
|
|
|
|
def foo
|
|
|
|
@foo
|
|
|
|
end
|
|
|
|
def foo=(value)
|
|
|
|
@foo = 'oops'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@app.set :foo, 'bam'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 'oops', @app.foo
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets multiple options to true with #enable" do
|
|
|
|
@app.enable :sessions, :foo, :bar
|
2009-01-14 17:00:26 -05:00
|
|
|
assert @app.sessions
|
|
|
|
assert @app.foo
|
|
|
|
assert @app.bar
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets multiple options to false with #disable" do
|
|
|
|
@app.disable :sessions, :foo, :bar
|
2009-01-14 17:00:26 -05:00
|
|
|
assert !@app.sessions
|
|
|
|
assert !@app.foo
|
|
|
|
assert !@app.bar
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-12-21 05:11:25 -05:00
|
|
|
|
|
|
|
it 'enables MethodOverride middleware when :methodoverride is enabled' do
|
|
|
|
@app.set :methodoverride, true
|
|
|
|
@app.put('/') { 'okay' }
|
|
|
|
post '/', {'_method'=>'PUT'}, {}
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'okay', body
|
2008-12-21 05:11:25 -05:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2009-01-21 17:55:04 -05:00
|
|
|
|
|
|
|
describe 'Backtrace Cleaning (clean_trace option)' do
|
|
|
|
before do
|
|
|
|
@app = Class.new(Sinatra::Base)
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_backtrace(trace)
|
|
|
|
@app.new.send(:clean_backtrace, trace)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is enabled by default' do
|
|
|
|
assert @app.clean_trace
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does nothing when disabled' do
|
|
|
|
backtrace = [
|
|
|
|
"./lib/sinatra/base.rb",
|
|
|
|
"./myapp:42",
|
|
|
|
("#{Gem.dir}/some/lib.rb" if defined?(Gem))
|
|
|
|
].compact
|
|
|
|
@app.set :clean_trace, false
|
|
|
|
assert_equal backtrace, clean_backtrace(backtrace)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'removes ./ prefix from backtrace paths when enabled' do
|
|
|
|
assert_equal ['myapp.rb:42'], clean_backtrace(['./myapp.rb:42'])
|
|
|
|
end
|
|
|
|
|
|
|
|
if defined?(Gem)
|
|
|
|
it 'removes gem lib paths from backtrace when enabled' do
|
|
|
|
assert clean_backtrace(["#{Gem.dir}/some/lib"]).empty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|