From 631a2bfc73f12c06e0e5071958176a2ef92c5e25 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 5 Nov 2013 02:22:55 +0000 Subject: http_response: fix app-controlled close + test We allow applications to drop persistent connections, this does not seem forbidden by Rack and gives the app author some control over the lifetime of a connection. --- test/test_server.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'test/test_server.rb') diff --git a/test/test_server.rb b/test/test_server.rb index 6b9f00f..f0df475 100644 --- a/test/test_server.rb +++ b/test/test_server.rb @@ -743,4 +743,52 @@ class TestServer < Testcase tmp.close! quit_wait(pid) end + + def test_app_controls_close + err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1] + pid = mkserver(cfg) do + cfg.instance_eval do + ru = lambda { |env| + h = { 'Content-Length' => '2' } + if env["PATH_INFO"] =~ %r{\A/(.+)} + h["Connection"] = $1 + end + [ 200, h, ['HI'] ] + } + app(:rack, ru) { listen "#{host}:#{port}" } + stderr_path err.path + end + end + c = get_tcp_client(host, port) + + # normal response + c.write "GET /keep-alive HTTP/1.1\r\nHost: example.com\r\n\r\n" + buf = "" + Timeout.timeout(30) do + buf << c.readpartial(4096) until buf =~ /HI\z/ + end + assert_match %r{^Connection: keep-alive}, buf + assert_raises(Errno::EAGAIN,IO::WaitReadable) { c.read_nonblock(666) } + + # we allow whatever in the response, but don't send it + c.write "GET /whatever HTTP/1.1\r\nHost: example.com\r\n\r\n" + buf = "" + Timeout.timeout(30) do + buf << c.readpartial(4096) until buf =~ /HI\z/ + end + assert_match %r{^Connection: keep-alive}, buf + assert_raises(Errno::EAGAIN,IO::WaitReadable) { c.read_nonblock(666) } + + c.write "GET /close HTTP/1.1\r\nHost: example.com\r\n\r\n" + buf = "" + Timeout.timeout(30) do + buf << c.readpartial(4096) until buf =~ /HI\z/ + end + assert_match %r{^Connection: close}, buf + assert_equal c, IO.select([c], nil, nil, 30)[0][0] + assert_raises(EOFError) { c.readpartial(666) } + c.close + ensure + quit_wait(pid) + end end -- cgit v1.2.3-24-ge0c7