Updated MSpec source to 1c3ee1c8.
[rbx.git] / test / mri / webrick / test_server.rb
blobce5ee85c6cf6dff48f052e2bb62d1e442eb58cfc
1 require "test/unit"
2 require "tempfile"
3 require "webrick"
4 require File.join(File.dirname(__FILE__), "utils.rb")
6 class TestWEBrickServer < Test::Unit::TestCase
7   class Echo < WEBrick::GenericServer
8     def run(sock)
9       while line = sock.gets
10         sock << line
11       end
12     end
13   end
15   def test_server
16     TestWEBrick.start_server(Echo){|server, addr, port|
17       TCPSocket.open(addr, port){|sock|
18         sock.puts("foo"); assert_equal("foo\n", sock.gets)
19         sock.puts("bar"); assert_equal("bar\n", sock.gets)
20         sock.puts("baz"); assert_equal("baz\n", sock.gets)
21         sock.puts("qux"); assert_equal("qux\n", sock.gets)
22       }
23     }
24   end
26   def test_callbacks
27     accepted = started = stopped = 0
28     config = {
29       :AcceptCallback => Proc.new{ accepted += 1 },
30       :StartCallback => Proc.new{ started += 1 },
31       :StopCallback => Proc.new{ stopped += 1 },
32     }
33     TestWEBrick.start_server(Echo, config){|server, addr, port|
34       true while server.status != :Running
35       assert_equal(started, 1)
36       assert_equal(stopped, 0)
37       assert_equal(accepted, 0)
38       TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
39       TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
40       TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
41       assert_equal(accepted, 3)
42     }
43     assert_equal(started, 1)
44     assert_equal(stopped, 1)
45   end
47   def test_daemon
48     begin
49       r, w = IO.pipe
50       Process.fork{
51         r.close
52         WEBrick::Daemon.start
53         w.puts(Process.pid)
54         sleep
55       }
56       assert(Process.kill(:KILL, r.gets.to_i))
57     rescue NotImplementedError
58       # snip this test
59     ensure
60       r.close
61       w.close
62     end
63   end
64 end