2006-06-30 16:42:12 -04:00
|
|
|
# Copyright (c) 2005 Zed A. Shaw
|
|
|
|
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
|
|
#
|
|
|
|
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
|
|
|
# for more information.
|
2006-05-21 10:46:42 -04:00
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'net/http'
|
|
|
|
require 'mongrel'
|
|
|
|
require 'timeout'
|
2006-05-30 19:36:48 -04:00
|
|
|
require File.dirname(__FILE__) + "/testhelp.rb"
|
2006-05-23 08:46:05 -04:00
|
|
|
|
2007-10-18 17:03:53 -04:00
|
|
|
include Mongrel
|
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
class TestHandler < Mongrel::HttpHandler
|
2006-05-23 08:46:05 -04:00
|
|
|
attr_reader :ran_test
|
|
|
|
|
|
|
|
def process(request, response)
|
|
|
|
@ran_test = true
|
|
|
|
response.socket.write("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nhello!\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-05-23 08:46:05 -04:00
|
|
|
class WebServerTest < Test::Unit::TestCase
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-05-23 08:46:05 -04:00
|
|
|
def setup
|
|
|
|
@request = "GET / HTTP/1.1\r\nHost: www.zedshaw.com\r\nContent-Type: text/plain\r\n\r\n"
|
|
|
|
# we set num_processors=1 so that we can test the reaping code
|
2007-10-18 17:38:12 -04:00
|
|
|
@server = HttpServer.new("127.0.0.1", 9998, num_processors=1)
|
2006-05-23 08:46:05 -04:00
|
|
|
@tester = TestHandler.new
|
|
|
|
@server.register("/test", @tester)
|
2007-10-18 17:38:12 -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-18 17:03:53 -04:00
|
|
|
@server.stop(true)
|
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-18 22:07:15 -04:00
|
|
|
def do_test(string, chunk, close_after=nil)
|
|
|
|
socket = TCPSocket.new("127.0.0.1", 9998);
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
socket.close_write
|
2006-05-23 08:46:05 -04:00
|
|
|
sleep 1
|
|
|
|
end
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
2007-10-18 22:07:15 -04:00
|
|
|
socket.write(" ") if RUBY_PLATFORM =~ /mingw|mswin|cygwin|java/
|
|
|
|
socket.close
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_trickle_attack
|
|
|
|
do_test(@request, 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_close_client
|
|
|
|
assert_raises IOError do
|
|
|
|
do_test(@request, 10, 20)
|
|
|
|
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"
|
2006-06-01 23:00:49 -04:00
|
|
|
assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL do
|
2006-05-24 04:34:17 -04:00
|
|
|
do_test(long, long.length/2)
|
|
|
|
end
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_num_processors_overload
|
2006-05-24 04:34:17 -04:00
|
|
|
redirect_test_io do
|
2006-06-01 23:00:49 -04:00
|
|
|
assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL do
|
2006-05-24 04:34:17 -04:00
|
|
|
tests = [
|
|
|
|
Thread.new { do_test(@request, 1) },
|
|
|
|
Thread.new { do_test(@request, 10) },
|
|
|
|
]
|
|
|
|
|
|
|
|
tests.each {|t| t.join}
|
|
|
|
end
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_streamed_request
|
|
|
|
body = "a" * (Mongrel::Const::MAX_BODY * 2)
|
|
|
|
long = "GET /test HTTP/1.1\r\nContent-length: #{body.length}\r\n\r\n" + body
|
|
|
|
do_test(long, Mongrel::Const::CHUNK_SIZE * 2 -400)
|
|
|
|
end
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|