2019-07-27 12:47:19 -04:00
|
|
|
# frozen_string_literal: true
|
2017-05-12 15:16:55 -04:00
|
|
|
require_relative "helper"
|
2011-09-18 16:02:34 -04:00
|
|
|
|
2016-11-22 10:05:49 -05:00
|
|
|
require "rack"
|
|
|
|
|
|
|
|
class TestRackServer < Minitest::Test
|
2019-07-27 12:47:19 -04:00
|
|
|
parallelize_me!
|
2011-09-18 16:02:34 -04:00
|
|
|
|
|
|
|
class ErrorChecker
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
@exception = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :exception, :env
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
begin
|
2019-07-27 12:47:19 -04:00
|
|
|
@app.call(env)
|
2011-09-18 16:02:34 -04:00
|
|
|
rescue Exception => e
|
|
|
|
@exception = e
|
2019-07-27 12:47:19 -04:00
|
|
|
[ 500, {}, ["Error detected"] ]
|
2011-09-18 16:02:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ServerLint < Rack::Lint
|
|
|
|
def call(env)
|
|
|
|
check_env env
|
|
|
|
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
2011-10-18 02:12:56 -04:00
|
|
|
@simple = lambda { |env| [200, { "X-Header" => "Works" }, ["Hello"]] }
|
2011-09-27 13:53:45 -04:00
|
|
|
@server = Puma::Server.new @simple
|
2016-02-04 14:18:54 -05:00
|
|
|
@server.add_tcp_listener "127.0.0.1", 0
|
2011-11-22 14:47:50 -05:00
|
|
|
|
|
|
|
@stopped = false
|
2011-09-18 16:02:34 -04:00
|
|
|
end
|
|
|
|
|
2011-11-22 14:47:50 -05:00
|
|
|
def stop
|
2011-09-18 16:02:34 -04:00
|
|
|
@server.stop(true)
|
2011-11-22 14:47:50 -05:00
|
|
|
@stopped = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
@server.stop(true) unless @stopped
|
2011-09-18 16:02:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_lint
|
|
|
|
@checker = ErrorChecker.new ServerLint.new(@simple)
|
|
|
|
@server.app = @checker
|
|
|
|
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 14:18:54 -05:00
|
|
|
hit(["http://127.0.0.1:#{ @server.connected_port }/test"])
|
2011-09-18 16:02:34 -04:00
|
|
|
|
2011-11-22 19:32:35 -05:00
|
|
|
stop
|
|
|
|
|
2019-07-27 12:47:19 -04:00
|
|
|
refute @checker.exception, "Checker raised exception"
|
2011-09-18 16:02:34 -04:00
|
|
|
end
|
2011-10-14 15:15:35 -04:00
|
|
|
|
2012-04-28 12:51:08 -04:00
|
|
|
def test_large_post_body
|
|
|
|
@checker = ErrorChecker.new ServerLint.new(@simple)
|
|
|
|
@server.app = @checker
|
|
|
|
|
|
|
|
@server.run
|
|
|
|
|
|
|
|
big = "x" * (1024 * 16)
|
|
|
|
|
2016-02-04 14:18:54 -05:00
|
|
|
Net::HTTP.post_form URI.parse("http://127.0.0.1:#{ @server.connected_port }/test"),
|
2012-04-28 12:51:08 -04:00
|
|
|
{ "big" => big }
|
|
|
|
|
|
|
|
stop
|
|
|
|
|
2019-07-27 12:47:19 -04:00
|
|
|
refute @checker.exception, "Checker raised exception"
|
2012-04-28 12:51:08 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 15:15:35 -04:00
|
|
|
def test_path_info
|
|
|
|
input = nil
|
|
|
|
@server.app = lambda { |env| input = env; @simple.call(env) }
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 14:18:54 -05:00
|
|
|
hit(["http://127.0.0.1:#{ @server.connected_port }/test/a/b/c"])
|
2011-10-14 15:15:35 -04:00
|
|
|
|
2011-11-22 19:32:35 -05:00
|
|
|
stop
|
|
|
|
|
2011-10-14 15:15:35 -04:00
|
|
|
assert_equal "/test/a/b/c", input['PATH_INFO']
|
|
|
|
end
|
2011-10-21 00:44:34 -04:00
|
|
|
|
|
|
|
def test_after_reply
|
|
|
|
closed = false
|
|
|
|
|
|
|
|
@server.app = lambda do |env|
|
|
|
|
env['rack.after_reply'] << lambda { closed = true }
|
|
|
|
@simple.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 14:18:54 -05:00
|
|
|
hit(["http://127.0.0.1:#{ @server.connected_port }/test"])
|
2011-10-21 00:44:34 -04:00
|
|
|
|
2011-11-22 19:32:35 -05:00
|
|
|
stop
|
|
|
|
|
2011-10-21 00:44:34 -04:00
|
|
|
assert_equal true, closed
|
|
|
|
end
|
2011-10-21 02:07:24 -04:00
|
|
|
|
|
|
|
def test_common_logger
|
|
|
|
log = StringIO.new
|
|
|
|
|
|
|
|
logger = Rack::CommonLogger.new(@simple, log)
|
|
|
|
|
|
|
|
@server.app = logger
|
|
|
|
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 14:18:54 -05:00
|
|
|
hit(["http://127.0.0.1:#{ @server.connected_port }/test"])
|
2011-10-21 02:07:24 -04:00
|
|
|
|
2011-11-22 14:47:50 -05:00
|
|
|
stop
|
|
|
|
|
2011-10-21 02:07:24 -04:00
|
|
|
assert_match %r!GET /test HTTP/1\.1!, log.string
|
|
|
|
end
|
2011-09-18 16:02:34 -04:00
|
|
|
end
|