about summary refs log tree commit homepage
path: root/test/test_server.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-11-05 02:22:55 +0000
committerEric Wong <normalperson@yhbt.net>2013-11-05 02:24:40 +0000
commit631a2bfc73f12c06e0e5071958176a2ef92c5e25 (patch)
tree69d3f4d70f5fc26864cc36f76f477b35aaf3c1a4 /test/test_server.rb
parent3192ec1b4054bcc228dfb88e57d5e1c828682a7b (diff)
downloadyahns-631a2bfc73f12c06e0e5071958176a2ef92c5e25.tar.gz
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.
Diffstat (limited to 'test/test_server.rb')
-rw-r--r--test/test_server.rb48
1 files changed, 48 insertions, 0 deletions
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