1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/test/test_persistent.rb

112 lines
3.1 KiB
Ruby
Raw Normal View History

require 'puma'
require 'test/unit'
class TestPersistent < Test::Unit::TestCase
def setup
@valid_request = "GET / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\n\r\n"
2011-10-03 17:04:57 -04:00
@close_request = "GET / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n"
@http10_request = "GET / HTTP/1.0\r\nHost: test.com\r\nContent-Type: text/plain\r\n\r\n"
@keep_request = "GET / HTTP/1.0\r\nHost: test.com\r\nContent-Type: text/plain\r\nConnection: Keep-Alive\r\n\r\n"
2011-10-03 17:04:57 -04:00
@headers = { "X-Header" => "Works" }
@body = ["Hello"]
@simple = lambda { |env| [200, @headers, @body] }
@server = Puma::Server.new @simple
@server.add_tcp_listener "127.0.0.1", 9988
@server.run
@client = TCPSocket.new "127.0.0.1", 9988
end
def teardown
@client.close
@server.stop(true)
end
def lines(count, s=@client)
str = ""
count.times { str << s.gets }
str
end
def test_one_with_content_length
@client << @valid_request
sz = @body[0].size.to_s
assert_equal "HTTP/1.1 200 OK\r\nContent-Length: #{sz}\r\nX-Header: Works\r\n\r\n", lines(4)
assert_equal "Hello", @client.read(5)
end
def test_two_back_to_back
@client << @valid_request
sz = @body[0].size.to_s
assert_equal "HTTP/1.1 200 OK\r\nContent-Length: #{sz}\r\nX-Header: Works\r\n\r\n", lines(4)
assert_equal "Hello", @client.read(5)
@client << @valid_request
sz = @body[0].size.to_s
assert_equal "HTTP/1.1 200 OK\r\nContent-Length: #{sz}\r\nX-Header: Works\r\n\r\n", lines(4)
assert_equal "Hello", @client.read(5)
end
def test_chunked
@body << "Chunked"
@client << @valid_request
assert_equal "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nX-Header: Works\r\n\r\n5\r\nHello\r\n7\r\nChunked\r\n0\r\n", lines(9)
end
def test_hex
str = "This is longer and will be in hex"
@body << str
@client << @valid_request
assert_equal "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nX-Header: Works\r\n\r\n5\r\nHello\r\n#{str.size.to_s(16)}\r\n#{str}\r\n0\r\n", lines(9)
end
2011-10-03 17:44:18 -04:00
def test_client11_close
2011-10-03 17:04:57 -04:00
@client << @close_request
sz = @body[0].size.to_s
assert_equal "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: #{sz}\r\nX-Header: Works\r\n\r\n", lines(5)
assert_equal "Hello", @client.read(5)
end
2011-10-03 17:44:18 -04:00
def test_client10_close
@client << @http10_request
sz = @body[0].size.to_s
assert_equal "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Length: #{sz}\r\nX-Header: Works\r\n\r\n", lines(5)
assert_equal "Hello", @client.read(5)
end
def test_one_with_keep_alive_header
@client << @keep_request
sz = @body[0].size.to_s
assert_equal "HTTP/1.0 200 OK\r\nContent-Length: #{sz}\r\nX-Header: Works\r\n\r\n", lines(4)
assert_equal "Hello", @client.read(5)
end
def test_persistent_timeout
@server.persistent_timeout = 2
@client << @valid_request
sz = @body[0].size.to_s
assert_equal "HTTP/1.1 200 OK\r\nContent-Length: #{sz}\r\nX-Header: Works\r\n\r\n", lines(4)
assert_equal "Hello", @client.read(5)
sleep 3
assert_raises EOFError do
@client.read_nonblock(1)
end
end
end