2012-01-08 14:55:57 -05:00
|
|
|
require "rbconfig"
|
2011-12-07 16:43:16 -05:00
|
|
|
require 'test/unit'
|
2011-12-07 16:53:36 -05:00
|
|
|
require 'socket'
|
2012-09-02 23:33:09 -04:00
|
|
|
require 'timeout'
|
2012-09-03 12:18:46 -04:00
|
|
|
require 'net/http'
|
2012-09-06 01:09:42 -04:00
|
|
|
require 'tempfile'
|
2011-12-07 16:43:16 -05:00
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
require 'puma/cli'
|
|
|
|
require 'puma/control_cli'
|
|
|
|
|
2011-12-07 16:43:16 -05:00
|
|
|
class TestIntegration < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@state_path = "test/test_puma.state"
|
|
|
|
@bind_path = "test/test_server.sock"
|
|
|
|
@control_path = "test/test_control.sock"
|
2012-09-02 23:33:09 -04:00
|
|
|
@tcp_port = 9998
|
|
|
|
|
|
|
|
@server = nil
|
2012-09-06 01:09:42 -04:00
|
|
|
@script = nil
|
2011-12-07 16:43:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
File.unlink @state_path rescue nil
|
|
|
|
File.unlink @bind_path rescue nil
|
|
|
|
File.unlink @control_path rescue nil
|
2012-09-02 23:33:09 -04:00
|
|
|
|
|
|
|
if @server
|
|
|
|
Process.kill "INT", @server.pid
|
|
|
|
Process.wait @server.pid
|
|
|
|
@server.close
|
|
|
|
end
|
2012-09-06 01:09:42 -04:00
|
|
|
|
|
|
|
if @script
|
|
|
|
@script.close!
|
|
|
|
end
|
2012-09-02 23:33:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def server(opts)
|
|
|
|
core = "#{Gem.ruby} -rubygems -Ilib bin/puma"
|
|
|
|
cmd = "#{core} --restart-cmd '#{core}' -b tcp://127.0.0.1:#{@tcp_port} #{opts}"
|
2012-09-06 01:09:42 -04:00
|
|
|
tf = Tempfile.new "puma-test"
|
|
|
|
tf.puts "exec #{cmd}"
|
|
|
|
tf.close
|
|
|
|
|
|
|
|
@script = tf
|
|
|
|
|
|
|
|
@server = IO.popen("sh #{tf.path}", "r")
|
|
|
|
|
|
|
|
true while @server.gets =~ /Ctrl-C/
|
2012-09-02 23:33:09 -04:00
|
|
|
|
2012-09-03 12:18:46 -04:00
|
|
|
sleep 1
|
2012-09-06 01:09:42 -04:00
|
|
|
|
2012-09-02 23:33:09 -04:00
|
|
|
@server
|
|
|
|
end
|
|
|
|
|
|
|
|
def signal(which)
|
|
|
|
Process.kill which, @server.pid
|
2011-12-07 16:43:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_stop_via_pumactl
|
2012-01-08 14:55:57 -05:00
|
|
|
if defined?(JRUBY_VERSION) || RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
|
2011-12-07 17:58:30 -05:00
|
|
|
assert true
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
sin = StringIO.new
|
|
|
|
sout = StringIO.new
|
2011-12-07 16:43:16 -05:00
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
cli = Puma::CLI.new %W!-q -S #{@state_path} -b unix://#{@bind_path} --control unix://#{@control_path} test/hello.ru!, sin, sout
|
2011-12-07 16:43:16 -05:00
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
t = Thread.new do
|
|
|
|
cli.run
|
2011-12-07 16:43:16 -05:00
|
|
|
end
|
|
|
|
|
2011-12-07 16:53:36 -05:00
|
|
|
sleep 1
|
|
|
|
|
|
|
|
s = UNIXSocket.new @bind_path
|
|
|
|
s << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
assert_equal "Hello World", s.read.split("\r\n").last
|
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
ccli = Puma::ControlCLI.new %W!-S #{@state_path} stop!, sout
|
2011-12-07 16:43:16 -05:00
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
ccli.run
|
2011-12-07 16:43:16 -05:00
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
assert_kind_of Thread, t.join(1), "server didn't stop"
|
2011-12-07 16:43:16 -05:00
|
|
|
end
|
2012-09-02 23:33:09 -04:00
|
|
|
|
|
|
|
def test_restart_closes_keepalive_sockets
|
|
|
|
server("-q test/hello.ru")
|
|
|
|
|
|
|
|
s = TCPSocket.new "localhost", @tcp_port
|
|
|
|
s << "GET / HTTP/1.1\r\n\r\n"
|
|
|
|
true until s.gets == "\r\n"
|
|
|
|
|
|
|
|
s.readpartial(20)
|
|
|
|
signal :USR2
|
|
|
|
|
2012-09-06 01:09:42 -04:00
|
|
|
true while @server.gets =~ /Ctrl-C/
|
|
|
|
sleep 1
|
2012-09-03 12:23:42 -04:00
|
|
|
|
2012-09-02 23:33:09 -04:00
|
|
|
s.write "GET / HTTP/1.1\r\n\r\n"
|
|
|
|
|
|
|
|
assert_raises Errno::ECONNRESET do
|
|
|
|
Timeout.timeout(2) do
|
|
|
|
s.read(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
s = TCPSocket.new "localhost", @tcp_port
|
|
|
|
s << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
assert_equal "Hello World", s.read.split("\r\n").last
|
|
|
|
end
|
2012-09-06 01:09:42 -04:00
|
|
|
|
|
|
|
def test_bad_query_string_outputs_400
|
|
|
|
server "-q test/hello.ru 2>&1"
|
|
|
|
|
|
|
|
s = TCPSocket.new "localhost", @tcp_port
|
|
|
|
s << "GET /?h=% HTTP/1.0\r\n\r\n"
|
|
|
|
data = s.read
|
|
|
|
assert_equal "HTTP/1.1 400 Bad Request\r\n\r\n", data
|
|
|
|
end
|
2011-12-07 16:43:16 -05:00
|
|
|
end
|