2011-09-23 23:46:33 -04:00
|
|
|
# Copyright (c) 2011 Evan Phoenix
|
2006-06-30 16:42:12 -04:00
|
|
|
# Copyright (c) 2005 Zed A. Shaw
|
2006-05-21 10:46:42 -04:00
|
|
|
|
2007-10-20 19:15:19 -04:00
|
|
|
require 'test/testhelp'
|
2006-05-23 08:46:05 -04:00
|
|
|
|
2011-09-22 22:24:43 -04:00
|
|
|
include Puma
|
2007-10-18 17:03:53 -04:00
|
|
|
|
2011-09-23 01:14:39 -04:00
|
|
|
class TestHandler
|
2006-05-23 08:46:05 -04:00
|
|
|
attr_reader :ran_test
|
|
|
|
|
2011-09-23 01:14:39 -04:00
|
|
|
def call(env)
|
2006-05-23 08:46:05 -04:00
|
|
|
@ran_test = true
|
2011-09-23 01:14:39 -04:00
|
|
|
|
|
|
|
[200, {"Content-Type" => "text/plain"}, ["hello!"]]
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class WebServerTest < Test::Unit::TestCase
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-05-23 08:46:05 -04:00
|
|
|
def setup
|
2007-10-20 19:39:06 -04:00
|
|
|
@valid_request = "GET / HTTP/1.1\r\nHost: www.zedshaw.com\r\nContent-Type: text/plain\r\n\r\n"
|
2007-10-26 15:14:54 -04:00
|
|
|
|
2006-05-23 08:46:05 -04:00
|
|
|
@tester = TestHandler.new
|
2011-09-23 01:14:39 -04:00
|
|
|
|
2011-09-27 17:33:17 -04:00
|
|
|
@server = Server.new @tester, Events.strings
|
2011-09-27 13:53:45 -04:00
|
|
|
@server.add_tcp_listener "127.0.0.1", 9998
|
|
|
|
|
2007-10-19 04:31:34 -04:00
|
|
|
redirect_test_io do
|
|
|
|
@server.run
|
|
|
|
end
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-05-23 08:46:05 -04:00
|
|
|
def teardown
|
2007-10-20 20:49:39 -04:00
|
|
|
redirect_test_io do
|
|
|
|
@server.stop(true)
|
|
|
|
end
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_simple_server
|
|
|
|
hit(['http://localhost:9998/test'])
|
|
|
|
assert @tester.ran_test, "Handler didn't really run"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-10-26 20:58:48 -04:00
|
|
|
def do_test(string, chunk, close_after=nil, shutdown_delay=0)
|
2007-10-22 02:22:48 -04:00
|
|
|
# Do not use instance variables here, because it needs to be thread safe
|
|
|
|
socket = TCPSocket.new("127.0.0.1", 9998);
|
2007-10-18 22:07:15 -04:00
|
|
|
request = StringIO.new(string)
|
|
|
|
chunks_out = 0
|
2006-05-23 08:46:05 -04:00
|
|
|
|
2007-10-18 22:07:15 -04:00
|
|
|
while data = request.read(chunk)
|
2007-10-22 02:22:48 -04:00
|
|
|
chunks_out += socket.write(data)
|
|
|
|
socket.flush
|
2006-05-23 08:46:05 -04:00
|
|
|
sleep 0.2
|
2007-10-18 22:07:15 -04:00
|
|
|
if close_after and chunks_out > close_after
|
2007-10-22 02:22:48 -04:00
|
|
|
socket.close
|
2006-05-23 08:46:05 -04:00
|
|
|
sleep 1
|
|
|
|
end
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
2007-10-26 20:58:48 -04:00
|
|
|
sleep(shutdown_delay)
|
2007-10-22 02:22:48 -04:00
|
|
|
socket.write(" ") # Some platforms only raise the exception on attempted write
|
2007-10-26 20:58:48 -04:00
|
|
|
socket.flush
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_trickle_attack
|
2007-10-20 19:39:06 -04:00
|
|
|
do_test(@valid_request, 3)
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_close_client
|
|
|
|
assert_raises IOError do
|
2007-10-20 19:39:06 -04:00
|
|
|
do_test(@valid_request, 10, 20)
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_client
|
2006-05-24 04:34:17 -04:00
|
|
|
redirect_test_io do
|
|
|
|
do_test("GET /test HTTP/BAD", 3)
|
|
|
|
end
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_header_is_too_long
|
2006-05-24 04:34:17 -04:00
|
|
|
redirect_test_io do
|
|
|
|
long = "GET /test HTTP/1.1\r\n" + ("X-Big: stuff\r\n" * 15000) + "\r\n"
|
2007-10-27 04:48:53 -04:00
|
|
|
assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL, IOError do
|
2007-10-26 20:58:48 -04:00
|
|
|
do_test(long, long.length/2, 10)
|
2006-05-24 04:34:17 -04:00
|
|
|
end
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_streamed_request
|
2011-09-22 22:24:43 -04:00
|
|
|
body = "a" * (Puma::Const::MAX_BODY * 2)
|
2006-05-23 08:46:05 -04:00
|
|
|
long = "GET /test HTTP/1.1\r\nContent-length: #{body.length}\r\n\r\n" + body
|
2011-09-22 22:24:43 -04:00
|
|
|
do_test(long, Puma::Const::CHUNK_SIZE * 2 -400)
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|