about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-03-21 00:40:56 -0700
committerEric Wong <normalperson@yhbt.net>2009-03-21 01:48:50 -0700
commitef6a84736a1c653c6a56e9efe301df5b7abf7d73 (patch)
tree5fba49ba8b9a1f2dcf67342badf4f56b11b3265d
parent3fc10e70bf3b6aa0d670d2248e801d8745e7c0d2 (diff)
downloadunicorn-ef6a84736a1c653c6a56e9efe301df5b7abf7d73.tar.gz
This test requires Rack to be loaded and will not
run without it.  This also seems broken on 1.9 still
with Rack 0.9.1...
-rw-r--r--Manifest1
-rw-r--r--test/unit/test_request.rb82
2 files changed, 83 insertions, 0 deletions
diff --git a/Manifest b/Manifest
index cca63ab..0889fc7 100644
--- a/Manifest
+++ b/Manifest
@@ -39,6 +39,7 @@ test/test_helper.rb
 test/tools/trickletest.rb
 test/unit/test_configurator.rb
 test/unit/test_http_parser.rb
+test/unit/test_request.rb
 test/unit/test_response.rb
 test/unit/test_server.rb
 test/unit/test_upload.rb
diff --git a/test/unit/test_request.rb b/test/unit/test_request.rb
new file mode 100644
index 0000000..37fbb14
--- /dev/null
+++ b/test/unit/test_request.rb
@@ -0,0 +1,82 @@
+# Copyright (c) 2009 Eric Wong
+# You can redistribute it and/or modify it under the same terms as Ruby.
+
+if RUBY_VERSION =~ /1\.9/
+  warn "#$0 current broken under Ruby 1.9 with Rack"
+  exit 0
+end
+
+require 'test/test_helper'
+begin
+  require 'rack'
+  require 'rack/lint'
+rescue LoadError
+  warn "Unable to load rack, skipping test"
+  exit 0
+end
+
+include Unicorn
+
+class RequestTest < Test::Unit::TestCase
+
+  class MockRequest < StringIO
+    def unicorn_peeraddr
+      '666.666.666.666'
+    end
+  end
+
+  def setup
+    @request = HttpRequest.new(Logger.new($stderr))
+    @app = lambda do |env|
+      [ 200, { 'Content-Length' => '0', 'Content-Type' => 'text/plain' }, [] ]
+    end
+    @lint = Rack::Lint.new(@app)
+  end
+
+  def test_rack_lint_get
+    client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")
+    res = env = nil
+    assert_nothing_raised { env = @request.read(client) }
+    assert_equal '666.666.666.666', env['REMOTE_ADDR']
+    assert_nothing_raised { res = @lint.call(env) }
+  end
+
+  def test_rack_lint_put
+    client = MockRequest.new(
+      "PUT / HTTP/1.1\r\n" \
+      "Host: foo\r\n" \
+      "Content-Length: 5\r\n" \
+      "\r\n" \
+      "abcde")
+    res = env = nil
+    assert_nothing_raised { env = @request.read(client) }
+    assert_nothing_raised { res = @lint.call(env) }
+  end
+
+  def test_rack_lint_big_put
+    count = 100
+    bs = 0x10000
+    buf = (' ' * bs).freeze
+    length = bs * count
+    client = Tempfile.new('big_put')
+    def client.unicorn_peeraddr
+      '1.1.1.1'
+    end
+    client.syswrite(
+      "PUT / HTTP/1.1\r\n" \
+      "Host: foo\r\n" \
+      "Content-Length: #{length}\r\n" \
+      "\r\n")
+    count.times { assert_equal bs, client.syswrite(buf) }
+    assert_equal 0, client.sysseek(0)
+    res = env = nil
+    assert_nothing_raised { env = @request.read(client) }
+    assert_equal length, env['rack.input'].size
+    count.times { assert_equal buf, env['rack.input'].read(bs) }
+    assert_nil env['rack.input'].read(bs)
+    assert_nothing_raised { env['rack.input'].rewind }
+    assert_nothing_raised { res = @lint.call(env) }
+  end
+
+end
+