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

test_puma_server.rb - clean up leaky fd's, refactor (#2017)

1. test file was leaking fd's, fixed

2. test file was rather wet.  DRY'd up
This commit is contained in:
MSP-Greg 2019-10-10 04:22:34 -05:00 committed by Nate Berkopec
parent 5e5f06a872
commit ccc4d1db23

View file

@ -7,7 +7,9 @@ class TestPumaServer < Minitest::Test
@port = 0
@host = "127.0.0.1"
@app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }
@ios = []
@app = ->(env) { [200, {}, [env['rack.url_scheme']]] }
@events = Puma::Events.strings
@server = Puma::Server.new @app, @events
@ -15,6 +17,14 @@ class TestPumaServer < Minitest::Test
def teardown
@server.stop(true)
@ios.each { |io| io.close if io && !io.closed? }
end
def server_run(app: @app, early_hints: false)
@server.app = app
@server.add_tcp_listener @host, @port
@server.early_hints = true if early_hints
@server.run
end
def header(sock)
@ -28,21 +38,32 @@ class TestPumaServer < Minitest::Test
header
end
def send_http_and_read(req)
sock = TCPSocket.new @host, @server.connected_port
@ios << sock
sock << req
sock.read
end
def send_http(req)
sock = TCPSocket.new @host, @server.connected_port
@ios << sock
sock << req
sock
end
def test_proper_stringio_body
data = nil
@server.app = proc do |env|
server_run app: ->(env) do
data = env['rack.input'].read
[200, {}, ["ok"]]
end
@server.add_tcp_listener @host, @port
@server.run
fifteen = "1" * 15
sock = TCPSocket.new @host, @server.connected_port
sock << "PUT / HTTP/1.0\r\nContent-Length: 30\r\n\r\n#{fifteen}"
sock = send_http "PUT / HTTP/1.0\r\nContent-Length: 30\r\n\r\n#{fifteen}"
sleep 0.1 # important so that the previous data is sent as a packet
sock << fifteen
@ -53,37 +74,26 @@ class TestPumaServer < Minitest::Test
def test_puma_socket
body = "HTTP/1.1 750 Upgraded to Awesome\r\nDone: Yep!\r\n"
@server.app = proc do |env|
server_run app: ->(env) do
io = env['puma.socket']
io.write body
io.close
[-1, {}, []]
end
@server.add_tcp_listener @host, @port
@server.run
data = send_http_and_read "PUT / HTTP/1.0\r\n\r\nHello"
sock = TCPSocket.new @host, @server.connected_port
sock << "PUT / HTTP/1.0\r\n\r\nHello"
assert_equal body, sock.read
assert_equal body, data
end
def test_very_large_return
giant = "x" * 2056610
@server.app = proc do |env|
server_run app: ->(env) do
[200, {}, [giant]]
end
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\n\r\n"
sock = send_http "GET / HTTP/1.0\r\n\r\n"
while true
line = sock.gets
@ -105,30 +115,27 @@ class TestPumaServer < Minitest::Test
def test_respect_x_forwarded_ssl_on
env = {}
env['HOST'] = "example.com"
env['HTTP_X_FORWARDED_SSL'] = "on"
env['HOST'] = 'example.com'
env['HTTP_X_FORWARDED_SSL'] = 'on'
assert_equal "443", @server.default_server_port(env)
end
def test_respect_x_forwarded_scheme
env = {}
env['HOST'] = "example.com"
env['HTTP_X_FORWARDED_SCHEME'] = "https"
env['HOST'] = 'example.com'
env['HTTP_X_FORWARDED_SCHEME'] = 'https'
assert_equal "443", @server.default_server_port(env)
assert_equal '443', @server.default_server_port(env)
end
def test_default_server_port
@server.app = proc do |env|
server_run app: ->(env) do
[200, {}, [env['SERVER_PORT']]]
end
@server.add_tcp_listener @host, @port
@server.run
req = Net::HTTP::Get.new("/")
req['HOST'] = "example.com"
req = Net::HTTP::Get.new '/'
req['HOST'] = 'example.com'
res = Net::HTTP.start @host, @server.connected_port do |http|
http.request(req)
@ -138,13 +145,10 @@ class TestPumaServer < Minitest::Test
end
def test_default_server_port_respects_x_forwarded_proto
@server.app = proc do |env|
server_run app: ->(env) do
[200, {}, [env['SERVER_PORT']]]
end
@server.add_tcp_listener @host, @port
@server.run
req = Net::HTTP::Get.new("/")
req['HOST'] = "example.com"
req['X_FORWARDED_PROTO'] = "https,http"
@ -157,47 +161,28 @@ class TestPumaServer < Minitest::Test
end
def test_HEAD_has_no_body
@server.app = proc { |env| [200, {"Foo" => "Bar"}, ["hello"]] }
server_run app: ->(env) { [200, {"Foo" => "Bar"}, ["hello"]] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "HEAD / HTTP/1.0\r\n\r\n"
data = sock.read
data = send_http_and_read "HEAD / HTTP/1.0\r\n\r\n"
assert_equal "HTTP/1.0 200 OK\r\nFoo: Bar\r\nContent-Length: 5\r\n\r\n", data
end
def test_GET_with_empty_body_has_sane_chunking
@server.app = proc { |env| [200, {}, [""]] }
server_run app: ->(env) { [200, {}, [""]] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "HEAD / HTTP/1.0\r\n\r\n"
data = sock.read
data = send_http_and_read "HEAD / HTTP/1.0\r\n\r\n"
assert_equal "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n", data
end
def test_early_hints_works
@server.app = proc { |env|
server_run early_hints: true, app: ->(env) do
env['rack.early_hints'].call("Link" => "</style.css>; rel=preload; as=style\n</script.js>; rel=preload")
[200, { "X-Hello" => "World" }, ["Hello world!"]]
}
end
@server.add_tcp_listener @host, @port
@server.early_hints = true
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "HEAD / HTTP/1.0\r\n\r\n"
data = sock.read
data = send_http_and_read "HEAD / HTTP/1.0\r\n\r\n"
expected_data = (<<EOF
HTTP/1.1 103 Early Hints
@ -215,44 +200,33 @@ EOF
end
def test_early_hints_are_ignored_if_connection_lost
app = proc { |env|
env['rack.early_hints'].call("Link" => "</script.js>; rel=preload")
[200, { "X-Hello" => "World" }, ["Hello world!"]]
}
events = Puma::Events.strings
server = Puma::Server.new app, events
def server.fast_write(*args)
def @server.fast_write(*args)
raise Puma::ConnectionError
end
server.add_tcp_listener @host, @port
server.early_hints = true
server.run
server_run early_hints: true, app: ->(env) do
env['rack.early_hints'].call("Link" => "</script.js>; rel=preload")
[200, { "X-Hello" => "World" }, ["Hello world!"]]
end
# This request will cause the server to try and send early hints
sock = TCPSocket.new @host, server.connected_port
sock << "HEAD / HTTP/1.0\r\n\r\n"
_ = send_http "HEAD / HTTP/1.0\r\n\r\n"
# Give the server some time to try to write (and fail)
sleep 0.1
# Expect no errors in stderr
assert events.stderr.pos.zero?, "Server didn't swallow the connection error"
assert @events.stderr.pos.zero?, "Server didn't swallow the connection error"
end
def test_early_hints_is_off_by_default
@server.app = proc { |env|
server_run app: ->(env) do
assert_nil env['rack.early_hints']
[200, { "X-Hello" => "World" }, ["Hello world!"]]
}
end
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "HEAD / HTTP/1.0\r\n\r\n"
data = sock.read
data = send_http_and_read "HEAD / HTTP/1.0\r\n\r\n"
expected_data = (<<EOF
HTTP/1.0 200 OK
@ -266,29 +240,18 @@ EOF
end
def test_GET_with_no_body_has_sane_chunking
@server.app = proc { |env| [200, {}, []] }
server_run app: ->(env) { [200, {}, []] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "HEAD / HTTP/1.0\r\n\r\n"
data = sock.read
data = send_http_and_read "HEAD / HTTP/1.0\r\n\r\n"
assert_equal "HTTP/1.0 200 OK\r\n\r\n", data
end
def test_doesnt_print_backtrace_in_production
@server.app = proc { |e| raise "don't leak me bro" }
@server.leak_stack_on_error = false
@server.add_tcp_listener @host, @port
@server.run
server_run app: ->(env) { raise "don't leak me bro" }
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.0\r\n\r\n"
refute_match(/don't leak me bro/, data)
assert_match(/HTTP\/1.0 500 Internal Server Error/, data)
@ -298,77 +261,49 @@ EOF
re = lambda { |err| [302, {'Content-Type' => 'text', 'Location' => 'foo.html'}, ['302 found']] }
@server = Puma::Server.new @app, @events, {:lowlevel_error_handler => re}
@server.app = proc { |e| raise "don't leak me bro" }
@server.add_tcp_listener @host, @port
@server.run
server_run app: ->(env) { raise "don't leak me bro" }
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\n\r\n"
data = send_http_and_read "GET / HTTP/1.0\r\n\r\n"
data = sock.read
assert_match(/HTTP\/1.0 302 Found/, data)
end
def test_leh_gets_env_as_well
re = lambda { |err,env|
env['REQUEST_PATH'] || raise("where is env?")
env['REQUEST_PATH'] || raise('where is env?')
[302, {'Content-Type' => 'text', 'Location' => 'foo.html'}, ['302 found']]
}
@server = Puma::Server.new @app, @events, {:lowlevel_error_handler => re}
@server.app = proc { |e| raise "don't leak me bro" }
@server.add_tcp_listener @host, @port
@server.run
server_run app: ->(env) { raise "don't leak me bro" }
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\n\r\n"
data = send_http_and_read "GET / HTTP/1.0\r\n\r\n"
data = sock.read
assert_match(/HTTP\/1.0 302 Found/, data)
end
def test_custom_http_codes_10
@server.app = proc { |env| [449, {}, [""]] }
server_run app: ->(env) { [449, {}, [""]] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.0\r\n\r\n"
assert_equal "HTTP/1.0 449 CUSTOM\r\nContent-Length: 0\r\n\r\n", data
end
def test_custom_http_codes_11
@server.app = proc { |env| [449, {}, [""]] }
server_run app: ->(env) { [449, {}, [""]] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"
assert_equal "HTTP/1.1 449 CUSTOM\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
end
def test_HEAD_returns_content_headers
@server.app = proc { |env| [200, {"Content-Type" => "application/pdf",
"Content-Length" => "4242"}, []] }
server_run app: ->(env) { [200, {"Content-Type" => "application/pdf",
"Content-Length" => "4242"}, []] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "HEAD / HTTP/1.0\r\n\r\n"
data = sock.read
data = send_http_and_read "HEAD / HTTP/1.0\r\n\r\n"
assert_equal "HTTP/1.0 200 OK\r\nContent-Type: application/pdf\r\nContent-Length: 4242\r\n\r\n", data
end
@ -379,15 +314,9 @@ EOF
@events.register(:state) { |s| states << s }
@server.app = proc { |env| [200, {}, [""]] }
server_run app: ->(env) { [200, {}, [""]] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "HEAD / HTTP/1.0\r\n\r\n"
sock.read
_ = send_http_and_read "HEAD / HTTP/1.0\r\n\r\n"
assert_equal [:booting, :running], states
@ -398,28 +327,21 @@ EOF
def test_timeout_in_data_phase
@server.first_data_timeout = 2
@server.add_tcp_listener @host, @port
@server.run
server_run
client = TCPSocket.new @host, @server.connected_port
sock = send_http "POST / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\n"
client << "POST / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\n"
data = client.gets
data = sock.gets
assert_equal "HTTP/1.1 408 Request Timeout\r\n", data
end
def test_http_11_keep_alive_with_body
@server.app = proc { |env| [200, {"Content-Type" => "plain/text"}, ["hello\n"]] }
server_run app: ->(env) { [200, {"Content-Type" => "plain/text"}, ["hello\n"]] }
@server.add_tcp_listener @host, @port
@server.run
sock = send_http "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\n\r\n"
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\n\r\n"
h = header(sock)
h = header sock
body = sock.gets
@ -430,80 +352,50 @@ EOF
end
def test_http_11_close_with_body
@server.app = proc { |env| [200, {"Content-Type" => "plain/text"}, ["hello"]] }
server_run app: ->(env) { [200, {"Content-Type" => "plain/text"}, ["hello"]] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"
assert_equal "HTTP/1.1 200 OK\r\nContent-Type: plain/text\r\nConnection: close\r\nContent-Length: 5\r\n\r\nhello", data
end
def test_http_11_keep_alive_without_body
@server.app = proc { |env| [204, {}, []] }
server_run app: ->(env) { [204, {}, []] }
@server.add_tcp_listener @host, @port
@server.run
sock = send_http "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\n\r\n"
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\n\r\n"
h = header(sock)
sock.close
h = header sock
assert_equal ["HTTP/1.1 204 No Content"], h
end
def test_http_11_close_without_body
@server.app = proc { |env| [204, {}, []] }
server_run app: ->(env) { [204, {}, []] }
@server.add_tcp_listener @host, @port
@server.run
sock = send_http "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"
h = header(sock)
sock.close
h = header sock
assert_equal ["HTTP/1.1 204 No Content", "Connection: close"], h
end
def test_http_10_keep_alive_with_body
@server.app = proc { |env| [200, {"Content-Type" => "plain/text"}, ["hello\n"]] }
server_run app: ->(env) { [200, {"Content-Type" => "plain/text"}, ["hello\n"]] }
@server.add_tcp_listener @host, @port
@server.run
sock = send_http "GET / HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n"
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n"
h = header(sock)
h = header sock
body = sock.gets
assert_equal ["HTTP/1.0 200 OK", "Content-Type: plain/text", "Connection: Keep-Alive", "Content-Length: 6"], h
assert_equal "hello\n", body
sock.close
end
def test_http_10_close_with_body
@server.app = proc { |env| [200, {"Content-Type" => "plain/text"}, ["hello"]] }
server_run app: ->(env) { [200, {"Content-Type" => "plain/text"}, ["hello"]] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"
assert_equal "HTTP/1.0 200 OK\r\nContent-Type: plain/text\r\nContent-Length: 5\r\n\r\nhello", data
end
@ -511,7 +403,7 @@ EOF
def test_http_10_partial_hijack_with_content_length
body_parts = ['abc', 'de']
@server.app = proc do |env|
server_run app: ->(env) do
hijack_lambda = proc do | io |
io.write(body_parts[0])
io.write(body_parts[1])
@ -520,75 +412,45 @@ EOF
[200, {"Content-Length" => "5", 'rack.hijack' => hijack_lambda}, nil]
end
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"
assert_equal "HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nabcde", data
end
def test_http_10_keep_alive_without_body
@server.app = proc { |env| [204, {}, []] }
server_run app: ->(env) { [204, {}, []] }
@server.add_tcp_listener @host, @port
@server.run
sock = send_http "GET / HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n"
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n"
h = header(sock)
h = header sock
assert_equal ["HTTP/1.0 204 No Content", "Connection: Keep-Alive"], h
sock.close
end
def test_http_10_close_without_body
@server.app = proc { |env| [204, {}, []] }
server_run app: ->(env) { [204, {}, []] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"
assert_equal "HTTP/1.0 204 No Content\r\n\r\n", data
end
def test_Expect_100
@server.app = proc { |env| [200, {}, [""]] }
server_run app: ->(env) { [200, {}, [""]] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\nExpect: 100-continue\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.1\r\nConnection: close\r\nExpect: 100-continue\r\n\r\n"
assert_equal "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
end
def test_chunked_request
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n\r\n"
assert_equal "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
assert_equal "hello", body
@ -596,16 +458,12 @@ EOF
def test_chunked_request_pause_before_value
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\n"
sock = send_http "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\n"
sleep 1
sock << "h\r\n4\r\nello\r\n0\r\n\r\n"
@ -618,16 +476,12 @@ EOF
def test_chunked_request_pause_between_chunks
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n"
sock = send_http "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n"
sleep 1
sock << "4\r\nello\r\n0\r\n\r\n"
@ -640,16 +494,12 @@ EOF
def test_chunked_request_pause_mid_count
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r"
sock = send_http "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r"
sleep 1
sock << "\nh\r\n4\r\nello\r\n0\r\n\r\n"
@ -662,16 +512,12 @@ EOF
def test_chunked_request_pause_before_count_newline
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1"
sock = send_http "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1"
sleep 1
sock << "\r\nh\r\n4\r\nello\r\n0\r\n\r\n"
@ -684,16 +530,12 @@ EOF
def test_chunked_request_pause_mid_value
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\ne"
sock = send_http "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\ne"
sleep 1
sock << "llo\r\n0\r\n\r\n"
@ -706,20 +548,16 @@ EOF
def test_chunked_request_pause_between_cr_lf_after_size_of_second_chunk
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
part1 = 'a' * 4200
chunked_body = "#{part1.size.to_s(16)}\r\n#{part1}\r\n1\r\nb\r\n0\r\n\r\n"
sock = TCPSocket.new @host, @server.connected_port
sock << "PUT /path HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n"
sock = send_http "PUT /path HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n"
sleep 0.1
@ -737,16 +575,12 @@ EOF
def test_chunked_request_pause_between_closing_cr_lf
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "PUT /path HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r"
sock = send_http "PUT /path HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r"
sleep 1
@ -760,16 +594,12 @@ EOF
def test_chunked_request_pause_before_closing_cr_lf
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "PUT /path HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello"
sock = send_http "PUT /path HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello"
sleep 1
@ -783,18 +613,12 @@ EOF
def test_chunked_request_header_case
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: Chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n\r\n"
data = sock.read
data = send_http_and_read "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: Chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n\r\n"
assert_equal "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
assert_equal "hello", body
@ -802,18 +626,14 @@ EOF
def test_chunked_keep_alive
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = send_http "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n\r\n"
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n\r\n"
h = header(sock)
h = header sock
assert_equal ["HTTP/1.1 200 OK", "Content-Length: 0"], h
assert_equal "hello", body
@ -823,16 +643,12 @@ EOF
def test_chunked_keep_alive_two_back_to_back
body = nil
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n"
sock = send_http "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n"
last_crlf_written = false
last_crlf_writer = Thread.new do
@ -865,19 +681,15 @@ EOF
body = nil
remote_addr =nil
@server = Puma::Server.new @app, @events, { remote_address: :header, remote_address_header: 'HTTP_X_FORWARDED_FOR'}
@server.app = proc { |env|
server_run app: ->(env) {
body = env['rack.input'].read
remote_addr = env['REMOTE_ADDR']
[200, {}, [""]]
}
@server.add_tcp_listener @host, @port
@server.run
sock = send_http "GET / HTTP/1.1\r\nX-Forwarded-For: 127.0.0.1\r\nConnection: Keep-Alive\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n\r\n"
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nX-Forwarded-For: 127.0.0.1\r\nConnection: Keep-Alive\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n4\r\nello\r\n0\r\n\r\n"
h = header(sock)
h = header sock
assert_equal ["HTTP/1.1 200 OK", "Content-Length: 0"], h
assert_equal "hello", body
assert_equal "127.0.0.1", remote_addr
@ -895,32 +707,21 @@ EOF
end
def test_empty_header_values
@server.app = proc { |env| [200, {"X-Empty-Header" => ""}, []] }
server_run app: ->(env) { [200, {"X-Empty-Header" => ""}, []] }
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "HEAD / HTTP/1.0\r\n\r\n"
data = sock.read
data = send_http_and_read "HEAD / HTTP/1.0\r\n\r\n"
assert_equal "HTTP/1.0 200 OK\r\nX-Empty-Header: \r\n\r\n", data
end
def test_request_body_wait
request_body_wait = nil
@server.app = proc { |env|
server_run app: ->(env) {
request_body_wait = env['puma.request_body_wait']
[204, {}, []]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "POST / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nh"
sock = send_http "POST / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nh"
sleep 1
sock << "ello"
@ -931,16 +732,12 @@ EOF
def test_request_body_wait_chunked
request_body_wait = nil
@server.app = proc { |env|
server_run app: ->(env) {
request_body_wait = env['puma.request_body_wait']
[204, {}, []]
}
@server.add_tcp_listener @host, @port
@server.run
sock = TCPSocket.new @host, @server.connected_port
sock << "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n"
sock = send_http "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n"
sleep 1
sock << "4\r\nello\r\n0\r\n\r\n"