2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class SettingsTest < Minitest::Test
|
2009-03-26 11:42:13 -04:00
|
|
|
setup do
|
2009-10-14 07:01:06 -04:00
|
|
|
@base = Sinatra.new(Sinatra::Base)
|
2011-05-03 14:17:02 -04:00
|
|
|
@base.set :environment => :foo, :app_file => nil
|
2009-12-22 21:50:40 -05:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
@application = Sinatra.new(Sinatra::Application)
|
2011-05-03 14:17:02 -04:00
|
|
|
@application.set :environment => :foo, :app_file => nil
|
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
|
2015-01-10 14:30:47 -05:00
|
|
|
assert_raises ArgumentError do
|
2010-02-05 16:45:52 -05:00
|
|
|
@base.set(:fiz, 'boom!'){ 'baz' }
|
|
|
|
end
|
|
|
|
assert !@base.respond_to?(:fiz)
|
2010-02-05 15:12:28 -05:00
|
|
|
end
|
|
|
|
|
2011-04-15 09:08:25 -04:00
|
|
|
it 'raises an error without value and block' do
|
2015-01-10 14:30:47 -05:00
|
|
|
assert_raises(ArgumentError) { @base.set(:fiz) }
|
2011-04-15 09:08:25 -04:00
|
|
|
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
|
2015-01-10 14:30:47 -05:00
|
|
|
assert_raises ArgumentError do
|
2011-04-15 09:08:25 -04:00
|
|
|
@base.set(:fiz, @base) { 'baz' }
|
|
|
|
end
|
|
|
|
assert !@base.respond_to?(:fiz)
|
|
|
|
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
|
|
|
|
|
2011-06-05 03:33:00 -04:00
|
|
|
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
|
|
|
|
|
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
|
2011-03-12 09:52:43 -05:00
|
|
|
assert_equal :foo, @base.new!.settings.environment
|
2009-12-22 21:50:40 -05:00
|
|
|
end
|
|
|
|
|
2010-09-02 07:58:23 -04:00
|
|
|
it 'is accessible from class via #settings' do
|
|
|
|
assert_equal :foo, @base.settings.environment
|
|
|
|
end
|
|
|
|
|
Add default_content_type setting
Historically, Sinatra::Response defaults to a text/html Content-Type.
However, in practice, Sinatra immediately clears this attribute after
instantiating a new Sinatra::Response object, so this default is some-
what suspect. Let's break this behavior and have Sinatra::Response
be Content-Type-less by default, and update the tests to reflect this.
Next, let's introduce a new default_content_type setting that will be
applied (instead of text/html) if the Content-Type is not set before the
response is finalized. This will be great for e.g. JSON API developers.
Let's also make it nil-able to indicate that a default Content-Type
should never be applied.
Wherever Sinatra is emitting HTML, e.g. in error pages, force the
Content-Type to text/html.
Finally, clean up the error-handling logic to behave as follows:
* Set the X-Cascade header as early as possible.
* If an error block matches and returns a value, stop processing and
return that value.
* If we have a not found or bad request error, inspect the exception (if
any) for an error message and present it as the response body if it
exists, or present a default error message.
The remaining logic is the same otherwise. This should make error
handlers simpler to write and behave more consistently by not polluting
the body with a default message when none may be necessary.
2017-01-23 17:36:00 -05:00
|
|
|
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
|
|
|
|
|
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
|
2019-02-03 11:06:35 -05:00
|
|
|
|
|
|
|
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
|
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")
|
2016-05-09 22:45:34 -04:00
|
|
|
assert body.include?("<code>show_exceptions</code> setting")
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2010-10-12 16:27:56 -04:00
|
|
|
|
2016-12-01 12:24:39 -05:00
|
|
|
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?('<div id="get">')
|
|
|
|
refute body.include?('<div id="post">')
|
|
|
|
end
|
|
|
|
|
2010-10-12 16:27:56 -04:00
|
|
|
it 'does not override app-specified error handling when set to :after_handler' do
|
2011-06-04 14:02:21 -04:00
|
|
|
ran = false
|
|
|
|
mock_app do
|
2010-10-12 16:27:56 -04:00
|
|
|
set :show_exceptions, :after_handler
|
2011-06-04 14:02:21 -04:00
|
|
|
error(RuntimeError) { ran = true }
|
|
|
|
get('/') { raise RuntimeError }
|
|
|
|
end
|
|
|
|
|
2010-10-10 20:41:12 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
2011-06-04 14:02:21 -04:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does catch any other exceptions when set to :after_handler' do
|
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
set :show_exceptions, :after_handler
|
|
|
|
error(RuntimeError) { ran = true }
|
|
|
|
get('/') { raise ArgumentError }
|
|
|
|
end
|
2010-10-10 20:41:12 -04:00
|
|
|
|
2011-06-04 14:02:21 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert !ran
|
2010-10-10 20:41:12 -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
|
2011-06-10 07:21:52 -04:00
|
|
|
|
|
|
|
it 'does not dump 404 errors' do
|
|
|
|
klass = Sinatra.new(Sinatra::Application)
|
|
|
|
|
|
|
|
mock_app(klass) {
|
|
|
|
enable :dump_errors
|
|
|
|
disable :raise_errors
|
|
|
|
|
|
|
|
error do
|
|
|
|
error = @env['rack.errors'].instance_variable_get(:@error)
|
|
|
|
error.rewind
|
|
|
|
|
|
|
|
error.read
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/' do
|
|
|
|
raise Sinatra::NotFound
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert !body.include?("NotFound") && !body.include?("settings_test.rb")
|
|
|
|
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
|
|
|
|
2011-06-15 18:24:27 -04:00
|
|
|
it 'is enabled on Base when public_folder is set and exists' do
|
2010-01-28 10:53:42 -05:00
|
|
|
@base.set :environment, :development
|
2020-03-13 17:20:04 -04:00
|
|
|
@base.set :public_folder, __dir__
|
2010-01-28 10:53:42 -05:00
|
|
|
assert @base.static?
|
|
|
|
end
|
|
|
|
|
2011-06-15 18:24:27 -04:00
|
|
|
it 'is enabled on Base when root is set and root/public_folder exists' do
|
2010-01-28 10:53:42 -05:00
|
|
|
@base.set :environment, :development
|
2020-03-13 17:20:04 -04:00
|
|
|
@base.set :root, __dir__
|
2010-01-28 10:53:42 -05:00
|
|
|
assert @base.static?
|
|
|
|
end
|
|
|
|
|
2010-03-04 08:57:26 -05:00
|
|
|
it 'is disabled on Application by default' do
|
|
|
|
assert ! @application.static?
|
|
|
|
end
|
|
|
|
|
2011-06-15 18:24:27 -04:00
|
|
|
it 'is enabled on Application when public_folder is set and exists' do
|
2010-03-04 08:57:26 -05:00
|
|
|
@application.set :environment, :development
|
2020-03-13 17:20:04 -04:00
|
|
|
@application.set :public_folder, __dir__
|
2010-03-04 08:57:26 -05:00
|
|
|
assert @application.static?
|
|
|
|
end
|
|
|
|
|
2011-06-15 18:24:27 -04:00
|
|
|
it 'is enabled on Application when root is set and root/public_folder exists' do
|
2010-03-04 08:57:26 -05:00
|
|
|
@application.set :environment, :development
|
2020-03-13 17:20:04 -04:00
|
|
|
@application.set :root, __dir__
|
2009-10-14 07:01:06 -04:00
|
|
|
assert @application.static?
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2011-06-15 17:30:46 -04:00
|
|
|
|
|
|
|
it 'is possible to use Module#public' do
|
|
|
|
@base.send(:define_method, :foo) { }
|
|
|
|
@base.send(:private, :foo)
|
2011-06-16 04:31:20 -04:00
|
|
|
assert !@base.public_method_defined?(:foo)
|
2011-06-15 17:30:46 -04:00
|
|
|
@base.send(:public, :foo)
|
2011-06-16 04:31:20 -04:00
|
|
|
assert @base.public_method_defined?(:foo)
|
2011-06-15 17:30:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is possible to use the keyword public in a sinatra app' do
|
|
|
|
app = Sinatra.new do
|
|
|
|
private
|
|
|
|
def priv; end
|
|
|
|
public
|
|
|
|
def pub; end
|
|
|
|
end
|
2011-06-16 04:31:20 -04:00
|
|
|
assert !app.public_method_defined?(:priv)
|
|
|
|
assert app.public_method_defined?(:pub)
|
2011-06-15 17:30:46 -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
|
2012-05-13 14:59:49 -04:00
|
|
|
it 'includes webrick' do
|
|
|
|
assert @base.server.include?('webrick')
|
|
|
|
assert @application.server.include?('webrick')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes puma' do
|
|
|
|
assert @base.server.include?('puma')
|
|
|
|
assert @application.server.include?('puma')
|
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'app_file' do
|
2011-05-03 14:17:02 -04:00
|
|
|
it 'is nil for base classes' do
|
|
|
|
assert_nil Sinatra::Base.app_file
|
|
|
|
assert_nil Sinatra::Application.app_file
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to the file subclassing' do
|
2012-08-17 03:55:56 -04:00
|
|
|
assert_equal File.expand_path(__FILE__), Sinatra.new.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
|
2016-12-28 10:13:51 -05:00
|
|
|
assert_nil @base.root
|
|
|
|
assert_nil @application.root
|
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__
|
2020-03-13 17:20:04 -04:00
|
|
|
assert_equal File.expand_path(__dir__), @base.root
|
2009-03-26 11:42:13 -04:00
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
@application.app_file = __FILE__
|
2020-03-13 17:20:04 -04:00
|
|
|
assert_equal File.expand_path(__dir__), @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
|
2016-12-28 10:13:51 -05:00
|
|
|
assert_nil @base.views
|
|
|
|
assert_nil @application.views
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is set to root joined with views/' do
|
2020-03-13 17:20:04 -04:00
|
|
|
@base.root = __dir__
|
|
|
|
assert_equal __dir__ + "/views", @base.views
|
2009-03-26 11:42:13 -04:00
|
|
|
|
2020-03-13 17:20:04 -04:00
|
|
|
@application.root = __dir__
|
|
|
|
assert_equal __dir__ + "/views", @application.views
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2011-06-15 18:24:27 -04:00
|
|
|
describe 'public_folder' do
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'is nil if root is not set' do
|
2016-12-28 10:13:51 -05:00
|
|
|
assert_nil @base.public_folder
|
|
|
|
assert_nil @application.public_folder
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is set to root joined with public/' do
|
2020-03-13 17:20:04 -04:00
|
|
|
@base.root = __dir__
|
|
|
|
assert_equal __dir__ + "/public", @base.public_folder
|
2009-02-14 14:22:52 -05:00
|
|
|
|
2020-03-13 17:20:04 -04:00
|
|
|
@application.root = __dir__
|
|
|
|
assert_equal __dir__ + "/public", @application.public_folder
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|
|
|
|
|
2012-03-11 14:00:33 -04:00
|
|
|
describe 'public_dir' do
|
|
|
|
it 'is an alias for public_folder' do
|
2020-03-13 17:20:04 -04:00
|
|
|
@base.public_dir = __dir__
|
|
|
|
assert_equal __dir__, @base.public_dir
|
2012-03-11 14:00:33 -04:00
|
|
|
assert_equal @base.public_folder, @base.public_dir
|
|
|
|
|
2020-03-13 17:20:04 -04:00
|
|
|
@application.public_dir = __dir__
|
|
|
|
assert_equal __dir__, @application.public_dir
|
2012-03-11 14:00:33 -04:00
|
|
|
assert_equal @application.public_folder, @application.public_dir
|
|
|
|
end
|
|
|
|
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
|
2011-09-02 16:54:49 -04:00
|
|
|
|
|
|
|
describe 'protection' do
|
|
|
|
class MiddlewareTracker < Rack::Builder
|
|
|
|
def self.track
|
|
|
|
Rack.send :remove_const, :Builder
|
|
|
|
Rack.const_set :Builder, MiddlewareTracker
|
|
|
|
MiddlewareTracker.used.clear
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Rack.send :remove_const, :Builder
|
|
|
|
Rack.const_set :Builder, MiddlewareTracker.superclass
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.used
|
|
|
|
@used ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def use(middleware, *)
|
|
|
|
MiddlewareTracker.used << middleware
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets up Rack::Protection' do
|
|
|
|
MiddlewareTracker.track do
|
|
|
|
Sinatra::Base.new
|
|
|
|
assert_include MiddlewareTracker.used, Rack::Protection
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets up Rack::Protection::PathTraversal' do
|
|
|
|
MiddlewareTracker.track do
|
|
|
|
Sinatra::Base.new
|
|
|
|
assert_include MiddlewareTracker.used, Rack::Protection::PathTraversal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not set up Rack::Protection::PathTraversal when disabling it' do
|
|
|
|
MiddlewareTracker.track do
|
|
|
|
Sinatra.new { set :protection, :except => :path_traversal }.new
|
|
|
|
assert_include MiddlewareTracker.used, Rack::Protection
|
|
|
|
assert !MiddlewareTracker.used.include?(Rack::Protection::PathTraversal)
|
|
|
|
end
|
|
|
|
end
|
2013-02-26 01:25:19 -05:00
|
|
|
|
|
|
|
it 'sets up RemoteToken if sessions are enabled' do
|
|
|
|
MiddlewareTracker.track do
|
|
|
|
Sinatra.new { enable :sessions }.new
|
|
|
|
assert_include MiddlewareTracker.used, Rack::Protection::RemoteToken
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-03 22:53:12 -04:00
|
|
|
it 'sets up RemoteToken if sessions are enabled with a custom session store' do
|
|
|
|
MiddlewareTracker.track do
|
2016-08-04 10:05:02 -04:00
|
|
|
Sinatra.new {
|
|
|
|
enable :sessions
|
|
|
|
set :session_store, Rack::Session::Pool
|
|
|
|
}.new
|
2016-08-03 22:53:12 -04:00
|
|
|
assert_include MiddlewareTracker.used, Rack::Session::Pool
|
|
|
|
assert_include MiddlewareTracker.used, Rack::Protection::RemoteToken
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 01:25:19 -05:00
|
|
|
it 'does not set up RemoteToken if sessions are disabled' do
|
|
|
|
MiddlewareTracker.track do
|
|
|
|
Sinatra.new.new
|
|
|
|
assert !MiddlewareTracker.used.include?(Rack::Protection::RemoteToken)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets up RemoteToken if it is configured to' do
|
|
|
|
MiddlewareTracker.track do
|
|
|
|
Sinatra.new { set :protection, :session => true }.new
|
|
|
|
assert_include MiddlewareTracker.used, Rack::Protection::RemoteToken
|
|
|
|
end
|
|
|
|
end
|
2011-09-02 16:54:49 -04:00
|
|
|
end
|
2009-02-14 14:22:52 -05:00
|
|
|
end
|