2019-07-27 12:47:19 -04:00
|
|
|
# frozen_string_literal: true
|
2011-09-23 23:46:33 -04:00
|
|
|
# Copyright (c) 2011 Evan Phoenix
|
2016-02-04 14:18:54 -05:00
|
|
|
# Copyright (c) 2005 Zed A. Shaw
|
2006-05-21 10:46:42 -04:00
|
|
|
|
2017-05-12 15:16:55 -04:00
|
|
|
require_relative "helper"
|
2006-05-23 08:46:05 -04:00
|
|
|
|
2016-11-22 10:05:49 -05:00
|
|
|
require "puma/server"
|
2007-10-18 17:03:53 -04:00
|
|
|
|
2011-09-23 01:14:39 -04:00
|
|
|
class TestHandler
|
2006-05-23 08:46:05 -04:00
|
|
|
attr_reader :ran_test
|
|
|
|
|
2011-09-23 01:14:39 -04:00
|
|
|
def call(env)
|
2006-05-23 08:46:05 -04:00
|
|
|
@ran_test = true
|
2011-09-23 01:14:39 -04:00
|
|
|
|
|
|
|
[200, {"Content-Type" => "text/plain"}, ["hello!"]]
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-22 10:05:49 -05:00
|
|
|
class WebServerTest < Minitest::Test
|
2019-09-11 10:11:46 -04:00
|
|
|
parallelize_me!
|
2019-09-11 10:34:11 -04:00
|
|
|
|
2019-11-11 18:56:45 -05:00
|
|
|
VALID_REQUEST = "GET / HTTP/1.1\r\nHost: www.zedshaw.com\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n"
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-05-23 08:46:05 -04:00
|
|
|
def setup
|
|
|
|
@tester = TestHandler.new
|
2016-11-22 10:05:49 -05:00
|
|
|
@server = Puma::Server.new @tester, Puma::Events.strings
|
2016-02-04 14:18:54 -05:00
|
|
|
@server.add_tcp_listener "127.0.0.1", 0
|
2011-09-27 13:53:45 -04:00
|
|
|
|
2012-06-29 20:41:03 -04:00
|
|
|
@server.run
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-05-23 08:46:05 -04:00
|
|
|
def teardown
|
2012-06-29 20:41:03 -04:00
|
|
|
@server.stop(true)
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_simple_server
|
2019-11-19 13:36:15 -05:00
|
|
|
hit(["http://127.0.0.1:#{@server.connected_ports[0]}/test"])
|
2006-05-23 08:46:05 -04:00
|
|
|
assert @tester.ran_test, "Handler didn't really run"
|
|
|
|
end
|
|
|
|
|
2020-02-11 10:47:40 -05:00
|
|
|
def test_requests_count
|
|
|
|
assert_equal @server.requests_count, 0
|
|
|
|
3.times do
|
2020-02-27 11:32:48 -05:00
|
|
|
hit(["http://127.0.0.1:#{@server.connected_ports[0]}/test"])
|
2020-02-11 10:47:40 -05:00
|
|
|
end
|
|
|
|
assert_equal @server.requests_count, 3
|
|
|
|
end
|
|
|
|
|
2006-05-23 08:46:05 -04:00
|
|
|
def test_trickle_attack
|
2019-07-27 12:47:19 -04:00
|
|
|
socket = do_test(VALID_REQUEST, 3)
|
|
|
|
assert_match "hello", socket.read
|
2019-08-09 14:19:40 -04:00
|
|
|
socket.close
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_close_client
|
|
|
|
assert_raises IOError do
|
2019-08-09 14:19:40 -04:00
|
|
|
do_test_raise(VALID_REQUEST, 10, 20)
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_client
|
2019-07-27 12:47:19 -04:00
|
|
|
socket = do_test("GET /test HTTP/BAD", 3)
|
|
|
|
assert_match "Bad Request", socket.read
|
2019-08-09 14:19:40 -04:00
|
|
|
socket.close
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_header_is_too_long
|
2012-06-29 20:41:03 -04:00
|
|
|
long = "GET /test HTTP/1.1\r\n" + ("X-Big: stuff\r\n" * 15000) + "\r\n"
|
|
|
|
assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL, IOError do
|
2019-08-09 14:19:40 -04:00
|
|
|
do_test_raise(long, long.length/2, 10)
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_streamed_request
|
2011-09-22 22:24:43 -04:00
|
|
|
body = "a" * (Puma::Const::MAX_BODY * 2)
|
2019-11-11 18:56:45 -05:00
|
|
|
long = "GET /test HTTP/1.1\r\nContent-length: #{body.length}\r\nConnection: close\r\n\r\n" + body
|
2019-07-27 12:47:19 -04:00
|
|
|
socket = do_test(long, (Puma::Const::CHUNK_SIZE * 2) - 400)
|
|
|
|
assert_match "hello", socket.read
|
2019-08-09 14:19:40 -04:00
|
|
|
socket.close
|
2006-05-23 08:46:05 -04:00
|
|
|
end
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2019-07-27 12:47:19 -04:00
|
|
|
private
|
|
|
|
|
2019-08-09 14:19:40 -04:00
|
|
|
def do_test(string, chunk)
|
|
|
|
# Do not use instance variables here, because it needs to be thread safe
|
2019-11-19 13:36:15 -05:00
|
|
|
socket = TCPSocket.new("127.0.0.1", @server.connected_ports[0]);
|
2019-08-09 14:19:40 -04:00
|
|
|
request = StringIO.new(string)
|
|
|
|
chunks_out = 0
|
|
|
|
|
|
|
|
while data = request.read(chunk)
|
|
|
|
chunks_out += socket.write(data)
|
|
|
|
socket.flush
|
|
|
|
end
|
|
|
|
socket
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_test_raise(string, chunk, close_after = nil)
|
2019-07-27 12:47:19 -04:00
|
|
|
# Do not use instance variables here, because it needs to be thread safe
|
2019-11-19 13:36:15 -05:00
|
|
|
socket = TCPSocket.new("127.0.0.1", @server.connected_ports[0]);
|
2019-07-27 12:47:19 -04:00
|
|
|
request = StringIO.new(string)
|
|
|
|
chunks_out = 0
|
|
|
|
|
|
|
|
while data = request.read(chunk)
|
|
|
|
chunks_out += socket.write(data)
|
|
|
|
socket.flush
|
|
|
|
socket.close if close_after && chunks_out > close_after
|
|
|
|
end
|
|
|
|
|
|
|
|
socket.write(" ") # Some platforms only raise the exception on attempted write
|
|
|
|
socket.flush
|
|
|
|
socket
|
2019-08-09 14:19:40 -04:00
|
|
|
ensure
|
|
|
|
socket.close unless socket.closed?
|
2019-07-27 12:47:19 -04:00
|
|
|
end
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|