about summary refs log tree commit homepage
path: root/test
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-10-30 01:50:09 +0000
committerEric Wong <normalperson@yhbt.net>2013-10-30 07:00:51 +0000
commit41c81cf79d5318111ffa93e309bcafb149e6aa0e (patch)
treedfd6f39595ba3a2bc4303adfbfeb10e49cb26679 /test
parent92b4d2b4ffbf8221260c1195ece1c227ec6824e3 (diff)
downloadyahns-41c81cf79d5318111ffa93e309bcafb149e6aa0e.tar.gz
While we're at it, add some additional tests for input handling.
Diffstat (limited to 'test')
-rw-r--r--test/test_input.rb75
1 files changed, 75 insertions, 0 deletions
diff --git a/test/test_input.rb b/test/test_input.rb
index 608ff23..404263d 100644
--- a/test/test_input.rb
+++ b/test/test_input.rb
@@ -49,4 +49,79 @@ class TestInput < Testcase
   ensure
     quit_wait(pid)
   end
+
+  def input_server(ru, ibtype)
+    err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
+    cfg.instance_eval do
+      GTL.synchronize do
+        app(:rack, ru) { listen "#{host}:#{port}"; input_buffering ibtype }
+      end
+      stderr_path err.path
+    end
+    pid = mkserver(cfg)
+    [ host, port, pid ]
+  end
+
+  def test_read_negative_lazy; _read_neg(:lazy); end
+  def test_read_negative_nobuffer; _read_neg(false); end
+
+  def _read_neg(ibtype)
+    ru = lambda do |env|
+      rv = []
+      input = env["rack.input"]
+      begin
+        input.read(-1)
+      rescue => e
+        rv << e.class.to_s
+      end
+      rv << input.read
+      rv << input.read(1).nil?
+      rv = rv.join(",")
+      h = { "Content-Length" => rv.size.to_s }
+      [ 200, h, [ rv ] ]
+    end
+    host, port, pid = input_server(ru, ibtype)
+    c = get_tcp_client(host, port)
+    c.write "PUT / HTTP/1.0\r\nContent-Length: 5\r\n\r\nhello"
+    assert_equal c, c.wait(30)
+    head, body = c.read.split(/\r\n\r\n/)
+    assert_match %r{ 200 OK}, head
+    exc, full, final = body.split(/,/)
+    assert_equal "hello", full
+    assert_equal "ArgumentError", exc
+    assert_equal true.to_s, final
+    c.close
+  ensure
+    quit_wait(pid)
+  end
+
+  def test_gets_lazy; _gets(:lazy); end
+  def test_gets_nobuffer; _gets(false); end
+
+  def _gets(ibtype)
+    in_join = lambda do |input|
+      rv = []
+      while line = input.gets
+        rv << line
+      end
+      rv.join(",")
+    end
+    ru = lambda do |env|
+      rv = in_join.call(env["rack.input"])
+      h = { "Content-Length" => rv.size.to_s }
+      [ 200, h, [ rv ] ]
+    end
+    host, port, pid = input_server(ru, ibtype)
+    c = get_tcp_client(host, port)
+    buf = "a\nb\n\n"
+    c.write "PUT / HTTP/1.0\r\nContent-Length: 5\r\n\r\n#{buf}"
+    assert_equal c, c.wait(30)
+    head, body = c.read.split(/\r\n\r\n/)
+    assert_match %r{ 200 OK}, head
+    expect = in_join.call(StringIO.new(buf))
+    assert_equal expect, body
+    c.close
+  ensure
+    quit_wait(pid)
+  end
 end