2005-01-07 06:05:22 -05:00
|
|
|
require "test/unit"
|
|
|
|
require "tempfile"
|
|
|
|
require "webrick"
|
2010-02-02 08:58:56 -05:00
|
|
|
require_relative "utils"
|
2005-01-07 06:05:22 -05:00
|
|
|
|
|
|
|
class TestWEBrickServer < Test::Unit::TestCase
|
|
|
|
class Echo < WEBrick::GenericServer
|
|
|
|
def run(sock)
|
|
|
|
while line = sock.gets
|
|
|
|
sock << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_server
|
2008-10-29 07:48:35 -04:00
|
|
|
TestWEBrick.start_server(Echo){|server, addr, port, log|
|
2005-01-07 06:05:22 -05:00
|
|
|
TCPSocket.open(addr, port){|sock|
|
2008-10-29 07:48:35 -04:00
|
|
|
sock.puts("foo"); assert_equal("foo\n", sock.gets, log.call)
|
|
|
|
sock.puts("bar"); assert_equal("bar\n", sock.gets, log.call)
|
|
|
|
sock.puts("baz"); assert_equal("baz\n", sock.gets, log.call)
|
|
|
|
sock.puts("qux"); assert_equal("qux\n", sock.gets, log.call)
|
2005-01-07 06:05:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_callbacks
|
|
|
|
accepted = started = stopped = 0
|
|
|
|
config = {
|
|
|
|
:AcceptCallback => Proc.new{ accepted += 1 },
|
|
|
|
:StartCallback => Proc.new{ started += 1 },
|
|
|
|
:StopCallback => Proc.new{ stopped += 1 },
|
|
|
|
}
|
2008-10-29 07:48:35 -04:00
|
|
|
TestWEBrick.start_server(Echo, config){|server, addr, port, log|
|
2005-01-07 06:05:22 -05:00
|
|
|
true while server.status != :Running
|
2008-10-29 07:48:35 -04:00
|
|
|
assert_equal(started, 1, log.call)
|
|
|
|
assert_equal(stopped, 0, log.call)
|
|
|
|
assert_equal(accepted, 0, log.call)
|
2005-01-07 06:05:22 -05:00
|
|
|
TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
|
|
|
|
TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
|
|
|
|
TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
|
2008-10-29 07:48:35 -04:00
|
|
|
assert_equal(accepted, 3, log.call)
|
2005-01-07 06:05:22 -05:00
|
|
|
}
|
|
|
|
assert_equal(started, 1)
|
|
|
|
assert_equal(stopped, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_daemon
|
|
|
|
begin
|
2010-05-07 04:09:37 -04:00
|
|
|
r, w = IO.pipe
|
2010-05-07 11:07:11 -04:00
|
|
|
pid1 = Process.fork{
|
2010-05-07 04:09:37 -04:00
|
|
|
r.close
|
2005-01-07 06:05:22 -05:00
|
|
|
WEBrick::Daemon.start
|
2010-05-07 04:09:37 -04:00
|
|
|
w.puts(Process.pid)
|
2010-05-07 11:07:11 -04:00
|
|
|
sleep 10
|
2005-01-07 06:05:22 -05:00
|
|
|
}
|
2010-05-07 11:07:11 -04:00
|
|
|
pid2 = r.gets.to_i
|
|
|
|
assert(Process.kill(:KILL, pid2))
|
|
|
|
assert_not_equal(pid1, pid2)
|
2005-01-07 06:05:22 -05:00
|
|
|
rescue NotImplementedError
|
|
|
|
# snip this test
|
2010-05-07 04:09:37 -04:00
|
|
|
ensure
|
2010-05-07 11:07:11 -04:00
|
|
|
Process.wait(pid1) if pid1
|
2010-05-07 04:09:37 -04:00
|
|
|
r.close
|
|
|
|
w.close
|
2005-01-07 06:05:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|