diff options
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/test_http_parser.rb | 3 | ||||
-rw-r--r-- | test/unit/test_request.rb | 82 | ||||
-rw-r--r-- | test/unit/test_response.rb | 7 | ||||
-rw-r--r-- | test/unit/test_upload.rb | 28 |
4 files changed, 118 insertions, 2 deletions
diff --git a/test/unit/test_http_parser.rb b/test/unit/test_http_parser.rb index ca1cd01..fc75990 100644 --- a/test/unit/test_http_parser.rb +++ b/test/unit/test_http_parser.rb @@ -25,11 +25,10 @@ class HttpParserTest < Test::Unit::TestCase assert_equal '/', req['REQUEST_PATH'] assert_equal 'HTTP/1.1', req['HTTP_VERSION'] assert_equal '/', req['REQUEST_URI'] - assert_equal 'CGI/1.2', req['GATEWAY_INTERFACE'] assert_equal 'GET', req['REQUEST_METHOD'] assert_nil req['FRAGMENT'] assert_nil req['QUERY_STRING'] - + parser.reset assert parser.nread == 0, "Number read after reset should be 0" end 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 + diff --git a/test/unit/test_response.rb b/test/unit/test_response.rb index c30a141..4c7423b 100644 --- a/test/unit/test_response.rb +++ b/test/unit/test_response.rb @@ -41,5 +41,12 @@ class ResponseTest < Test::Unit::TestCase io.rewind assert_match(/.* #{HTTP_STATUS_CODES[code]}$/, io.readline.chomp, "wrong default reason phrase") end + + def test_rack_multivalue_headers + out = StringIO.new + HttpResponse.write(out,[200, {"X-Whatever" => "stuff\nbleh"}, []]) + assert_match(/^X-Whatever: stuff\r\nX-Whatever: bleh\r\n/, out.string) + end + end diff --git a/test/unit/test_upload.rb b/test/unit/test_upload.rb index edc94da..41fc473 100644 --- a/test/unit/test_upload.rb +++ b/test/unit/test_upload.rb @@ -135,6 +135,34 @@ class UploadTest < Test::Unit::TestCase assert_equal resp[:size], new_tmp.stat.size end + # Despite reading numerous articles and inspecting the 1.9.1-p0 C + # source, Eric Wong will never trust that we're always handling + # encoding-aware IO objects correctly. Thus this test uses shell + # utilities that should always operate on files/sockets on a + # byte-level. + def test_uncomfortable_with_onenine_encodings + # POSIX doesn't require all of these to be present on a system + which('curl') or return + which('sha1sum') or return + which('dd') or return + + start_server(@sha1_app) + + tmp = Tempfile.new('dd_dest') + assert(system("dd", "if=#{@random.path}", "of=#{tmp.path}", + "bs=#{@bs}", "count=#{@count}"), + "dd #@random to #{tmp}") + sha1_re = %r!\b([a-f0-9]{40})\b! + sha1_out = `sha1sum #{tmp.path}` + assert $?.success?, 'sha1sum ran OK' + + assert_match(sha1_re, sha1_out) + sha1 = sha1_re.match(sha1_out)[1] + resp = `curl -isSfN -T#{tmp.path} http://#@addr:#@port/` + assert $?.success?, 'curl ran OK' + assert_match(%r!\b#{sha1}\b!, resp) + end + private def length |