2021-10-18 12:40:01 -04:00
|
|
|
require File.expand_path('helper', __dir__)
|
2011-06-16 08:02:52 -04:00
|
|
|
require 'stringio'
|
2009-01-19 20:58:26 -05:00
|
|
|
|
2009-05-20 21:13:38 -04:00
|
|
|
module Rack::Handler
|
|
|
|
class Mock
|
2015-01-10 14:30:47 -05:00
|
|
|
extend Minitest::Assertions
|
|
|
|
# Allow assertions in request context
|
|
|
|
def self.assertions
|
|
|
|
@assertions ||= 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.assertions= assertions
|
|
|
|
@assertions = assertions
|
|
|
|
end
|
2009-05-20 21:13:38 -04:00
|
|
|
|
|
|
|
def self.run(app, options={})
|
|
|
|
assert(app < Sinatra::Base)
|
|
|
|
assert_equal 9001, options[:Port]
|
|
|
|
assert_equal 'foo.local', options[:Host]
|
|
|
|
yield new
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
end
|
2009-01-19 20:58:26 -05:00
|
|
|
end
|
|
|
|
|
2009-05-20 21:13:38 -04:00
|
|
|
register 'mock', 'Rack::Handler::Mock'
|
2009-01-19 20:58:26 -05:00
|
|
|
end
|
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class ServerTest < Minitest::Test
|
2009-03-26 11:42:13 -04:00
|
|
|
setup do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-19 20:58:26 -05:00
|
|
|
set :server, 'mock'
|
2010-02-04 20:16:05 -05:00
|
|
|
set :bind, 'foo.local'
|
2009-01-19 20:58:26 -05:00
|
|
|
set :port, 9001
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2011-06-16 08:02:52 -04:00
|
|
|
$stderr = StringIO.new
|
2009-01-19 20:58:26 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
def teardown
|
2011-06-16 08:02:52 -04:00
|
|
|
$stderr = STDERR
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-01-19 20:58:26 -05:00
|
|
|
|
|
|
|
it "locates the appropriate Rack handler and calls ::run" do
|
|
|
|
@app.run!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets options on the app before running" do
|
|
|
|
@app.run! :sessions => true
|
|
|
|
assert @app.sessions?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "falls back on the next server handler when not found" do
|
|
|
|
@app.run! :server => %w[foo bar mock]
|
|
|
|
end
|
2016-07-25 13:05:25 -04:00
|
|
|
|
2016-11-22 10:13:13 -05:00
|
|
|
it "initializes Rack middleware immediately on server run" do
|
|
|
|
class MyMiddleware
|
|
|
|
@@initialized = false
|
|
|
|
def initialize(app)
|
|
|
|
@@initialized = true
|
|
|
|
end
|
|
|
|
def self.initialized
|
|
|
|
@@initialized
|
|
|
|
end
|
|
|
|
def call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@app.use MyMiddleware
|
|
|
|
assert_equal(MyMiddleware.initialized, false)
|
|
|
|
@app.run!
|
|
|
|
assert_equal(MyMiddleware.initialized, true)
|
|
|
|
end
|
|
|
|
|
2016-07-25 13:05:25 -04:00
|
|
|
describe "Quiet mode" do
|
|
|
|
it "sends data to stderr when server starts and stops" do
|
|
|
|
@app.run!
|
|
|
|
assert_match(/\=\= Sinatra/, $stderr.string)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when quiet mode is activated" do
|
|
|
|
it "does not generate Sinatra start and stop messages" do
|
|
|
|
@app.run! quiet: true
|
|
|
|
refute_match(/\=\= Sinatra/, $stderr.string)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-01-19 20:58:26 -05:00
|
|
|
end
|