2017-05-12 15:16:55 -04:00
|
|
|
require_relative "helper"
|
2011-09-30 11:30:37 -04:00
|
|
|
|
2016-11-22 10:05:49 -05:00
|
|
|
class TestPersistent < Minitest::Test
|
2011-09-30 11:30:37 -04:00
|
|
|
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"
|
2011-10-03 17:28:46 -04:00
|
|
|
@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
|
|
|
|
2011-11-22 13:45:58 -05:00
|
|
|
@valid_post = "POST / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nhello"
|
2011-12-13 08:45:12 -05:00
|
|
|
@valid_no_body = "GET / HTTP/1.1\r\nHost: test.com\r\nX-Status: 204\r\nContent-Type: text/plain\r\n\r\n"
|
2011-11-22 13:45:58 -05:00
|
|
|
|
2011-09-30 11:30:37 -04:00
|
|
|
@headers = { "X-Header" => "Works" }
|
|
|
|
@body = ["Hello"]
|
2011-11-22 14:47:50 -05:00
|
|
|
@inputs = []
|
|
|
|
|
|
|
|
@simple = lambda do |env|
|
|
|
|
@inputs << env['rack.input']
|
2011-12-13 08:45:12 -05:00
|
|
|
status = Integer(env['HTTP_X_STATUS'] || 200)
|
|
|
|
[status, @headers, @body]
|
2011-11-22 14:47:50 -05:00
|
|
|
end
|
|
|
|
|
2012-07-23 14:24:22 -04:00
|
|
|
@host = "127.0.0.1"
|
|
|
|
@port = 9988
|
|
|
|
|
2011-09-30 11:30:37 -04:00
|
|
|
@server = Puma::Server.new @simple
|
|
|
|
@server.add_tcp_listener "127.0.0.1", 9988
|
2012-07-23 14:24:22 -04:00
|
|
|
@server.max_threads = 1
|
2011-09-30 11:30:37 -04:00
|
|
|
@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 = ""
|
2016-01-14 21:45:20 -05:00
|
|
|
Timeout.timeout(5) do
|
2011-11-22 13:45:58 -05:00
|
|
|
count.times { str << s.gets }
|
|
|
|
end
|
2011-09-30 11:30:37 -04:00
|
|
|
str
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_one_with_content_length
|
|
|
|
@client << @valid_request
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
2011-10-17 16:56:45 -04:00
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
2011-09-30 11:30:37 -04:00
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_two_back_to_back
|
|
|
|
@client << @valid_request
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
2011-10-17 16:56:45 -04:00
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
2011-09-30 11:30:37 -04:00
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
|
|
|
|
@client << @valid_request
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
2011-10-17 16:56:45 -04:00
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
2011-11-22 13:45:58 -05:00
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_post_then_get
|
|
|
|
@client << @valid_post
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\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\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
2011-09-30 11:30:37 -04:00
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
end
|
|
|
|
|
2011-12-13 08:45:12 -05:00
|
|
|
def test_no_body_then_get
|
|
|
|
@client << @valid_no_body
|
|
|
|
assert_equal "HTTP/1.1 204 No Content\r\nX-Header: Works\r\n\r\n", lines(3)
|
|
|
|
|
|
|
|
@client << @valid_request
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
end
|
|
|
|
|
2011-09-30 11:30:37 -04:00
|
|
|
def test_chunked
|
|
|
|
@body << "Chunked"
|
|
|
|
|
|
|
|
@client << @valid_request
|
|
|
|
|
2011-10-21 02:04:24 -04:00
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello\r\n7\r\nChunked\r\n0\r\n\r\n", lines(10)
|
2011-09-30 11:30:37 -04:00
|
|
|
end
|
|
|
|
|
2015-07-17 19:31:33 -04:00
|
|
|
def test_chunked_with_empty_part
|
|
|
|
@body << ""
|
|
|
|
@body << "Chunked"
|
|
|
|
|
|
|
|
@client << @valid_request
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello\r\n7\r\nChunked\r\n0\r\n\r\n", lines(10)
|
|
|
|
end
|
|
|
|
|
2011-10-18 02:08:09 -04:00
|
|
|
def test_no_chunked_in_http10
|
|
|
|
@body << "Chunked"
|
|
|
|
|
|
|
|
@client << @http10_request
|
|
|
|
|
2015-02-19 08:43:17 -05:00
|
|
|
assert_equal "HTTP/1.0 200 OK\r\nX-Header: Works\r\n\r\n", lines(3)
|
2011-10-18 02:08:09 -04:00
|
|
|
assert_equal "HelloChunked", @client.read
|
|
|
|
end
|
|
|
|
|
2011-09-30 12:05:44 -04:00
|
|
|
def test_hex
|
|
|
|
str = "This is longer and will be in hex"
|
|
|
|
@body << str
|
|
|
|
|
|
|
|
@client << @valid_request
|
|
|
|
|
2011-10-21 02:04:24 -04:00
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello\r\n#{str.size.to_s(16)}\r\n#{str}\r\n0\r\n\r\n", lines(10)
|
2011-09-30 12:05:44 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-10-17 16:56:45 -04:00
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nConnection: close\r\nContent-Length: #{sz}\r\n\r\n", lines(5)
|
2011-10-03 17:04:57 -04:00
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
end
|
|
|
|
|
2011-10-03 17:44:18 -04:00
|
|
|
def test_client10_close
|
2011-10-03 17:28:46 -04:00
|
|
|
@client << @http10_request
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
2015-02-19 08:43:17 -05:00
|
|
|
assert_equal "HTTP/1.0 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
2011-10-03 17:28:46 -04:00
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_one_with_keep_alive_header
|
|
|
|
@client << @keep_request
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
2011-11-30 18:07:23 -05:00
|
|
|
assert_equal "HTTP/1.0 200 OK\r\nX-Header: Works\r\nConnection: Keep-Alive\r\nContent-Length: #{sz}\r\n\r\n", lines(5)
|
2011-10-03 17:28:46 -04:00
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
end
|
|
|
|
|
2011-10-05 00:11:10 -04:00
|
|
|
def test_persistent_timeout
|
|
|
|
@server.persistent_timeout = 2
|
|
|
|
@client << @valid_request
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
2011-10-17 16:56:45 -04:00
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
2011-10-05 00:11:10 -04:00
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
|
|
|
|
sleep 3
|
|
|
|
|
|
|
|
assert_raises EOFError do
|
|
|
|
@client.read_nonblock(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-17 16:56:45 -04:00
|
|
|
def test_app_sets_content_length
|
|
|
|
@body = ["hello", " world"]
|
|
|
|
@headers['Content-Length'] = "11"
|
|
|
|
|
|
|
|
@client << @valid_request
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: 11\r\n\r\n",
|
|
|
|
lines(4)
|
|
|
|
assert_equal "hello world", @client.read(11)
|
|
|
|
end
|
|
|
|
|
2011-10-23 02:06:27 -04:00
|
|
|
def test_allow_app_to_chunk_itself
|
|
|
|
@headers = {'Transfer-Encoding' => "chunked" }
|
|
|
|
|
|
|
|
@body = ["5\r\nhello\r\n0\r\n\r\n"]
|
|
|
|
|
|
|
|
@client << @valid_request
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n", lines(7)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-10-18 01:47:35 -04:00
|
|
|
def test_two_requests_in_one_chunk
|
|
|
|
@server.persistent_timeout = 3
|
|
|
|
|
|
|
|
req = @valid_request.to_s
|
|
|
|
req << "GET /second HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\n\r\n"
|
|
|
|
|
|
|
|
@client << req
|
|
|
|
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
end
|
|
|
|
|
2011-11-22 14:47:50 -05:00
|
|
|
def test_second_request_not_in_first_req_body
|
|
|
|
@server.persistent_timeout = 3
|
|
|
|
|
|
|
|
req = @valid_request.to_s
|
|
|
|
req << "GET /second HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\n\r\n"
|
|
|
|
|
|
|
|
@client << req
|
|
|
|
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
|
|
|
|
assert_equal "Hello", @client.read(5)
|
|
|
|
|
2011-12-01 17:33:34 -05:00
|
|
|
assert_kind_of Puma::NullIO, @inputs[0]
|
|
|
|
assert_kind_of Puma::NullIO, @inputs[1]
|
2012-07-23 14:24:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_keepalive_doesnt_starve_clients
|
|
|
|
sz = @body[0].size.to_s
|
|
|
|
|
|
|
|
@client << @valid_request
|
|
|
|
|
|
|
|
c2 = TCPSocket.new @host, @port
|
|
|
|
c2 << @valid_request
|
|
|
|
|
|
|
|
out = IO.select([c2], nil, nil, 1)
|
|
|
|
|
|
|
|
assert out, "select returned nil"
|
|
|
|
assert_equal c2, out.first.first
|
2011-11-22 14:47:50 -05:00
|
|
|
|
2012-07-23 14:24:22 -04:00
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4, c2)
|
|
|
|
assert_equal "Hello", c2.read(5)
|
2011-11-22 14:47:50 -05:00
|
|
|
end
|
|
|
|
|
2011-09-30 11:30:37 -04:00
|
|
|
end
|