2012-04-11 15:09:59 -04:00
|
|
|
require "rbconfig"
|
|
|
|
require 'test/unit'
|
|
|
|
require 'socket'
|
|
|
|
require 'openssl'
|
|
|
|
|
2012-08-23 01:12:12 -04:00
|
|
|
require 'puma/minissl'
|
2012-04-11 15:09:59 -04:00
|
|
|
require 'puma/server'
|
|
|
|
|
|
|
|
require 'net/https'
|
|
|
|
|
|
|
|
class TestPumaServer < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def setup
|
2016-02-04 11:15:41 -05:00
|
|
|
@port = 0
|
2012-04-11 15:09:59 -04:00
|
|
|
@host = "127.0.0.1"
|
|
|
|
|
|
|
|
@app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }
|
|
|
|
|
|
|
|
@events = Puma::Events.new STDOUT, STDERR
|
|
|
|
@server = Puma::Server.new @app, @events
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2012-05-15 18:38:22 -04:00
|
|
|
@server.stop(true)
|
2012-04-11 15:09:59 -04:00
|
|
|
end
|
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
def header(sock)
|
|
|
|
header = []
|
|
|
|
while true
|
|
|
|
line = sock.gets
|
|
|
|
break if line == "\r\n"
|
|
|
|
header << line.strip
|
|
|
|
end
|
|
|
|
|
|
|
|
header
|
|
|
|
end
|
|
|
|
|
2012-05-15 18:19:28 -04:00
|
|
|
def test_proper_stringio_body
|
|
|
|
data = nil
|
|
|
|
|
|
|
|
@server.app = proc do |env|
|
|
|
|
data = env['rack.input'].read
|
|
|
|
[200, {}, ["ok"]]
|
|
|
|
end
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
|
|
|
fifteen = "1" * 15
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2012-05-15 18:19:28 -04:00
|
|
|
sock << "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
|
|
|
|
|
|
|
|
sock.read
|
|
|
|
|
|
|
|
assert_equal "#{fifteen}#{fifteen}", data
|
|
|
|
end
|
2012-06-04 15:00:11 -04:00
|
|
|
|
|
|
|
def test_puma_socket
|
|
|
|
body = "HTTP/1.1 750 Upgraded to Awesome\r\nDone: Yep!\r\n"
|
|
|
|
@server.app = proc do |env|
|
|
|
|
io = env['puma.socket']
|
|
|
|
|
|
|
|
io.write body
|
|
|
|
|
|
|
|
io.close
|
|
|
|
|
|
|
|
[-1, {}, []]
|
|
|
|
end
|
|
|
|
|
2016-02-04 13:22:17 -05:00
|
|
|
@server.add_tcp_listener @host, @port
|
2012-06-04 15:00:11 -04:00
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2012-06-04 15:00:11 -04:00
|
|
|
sock << "PUT / HTTP/1.0\r\n\r\nHello"
|
|
|
|
|
|
|
|
assert_equal body, sock.read
|
|
|
|
end
|
2012-11-29 14:32:50 -05:00
|
|
|
|
|
|
|
def test_very_large_return
|
|
|
|
giant = "x" * 2056610
|
|
|
|
|
|
|
|
@server.app = proc do |env|
|
|
|
|
[200, {}, [giant]]
|
|
|
|
end
|
|
|
|
|
2016-02-04 13:22:17 -05:00
|
|
|
@server.add_tcp_listener @host, @port
|
2012-11-29 14:32:50 -05:00
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2012-11-29 14:32:50 -05:00
|
|
|
sock << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
while true
|
|
|
|
line = sock.gets
|
|
|
|
break if line == "\r\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
out = sock.read
|
|
|
|
|
|
|
|
assert_equal giant.bytesize, out.bytesize
|
|
|
|
end
|
2013-02-19 22:23:02 -05:00
|
|
|
|
|
|
|
def test_respect_x_forwarded_proto
|
|
|
|
@server.app = proc do |env|
|
|
|
|
[200, {}, [env['SERVER_PORT']]]
|
|
|
|
end
|
|
|
|
|
2016-02-04 13:22:17 -05:00
|
|
|
@server.add_tcp_listener @host, @port
|
2013-02-19 22:23:02 -05:00
|
|
|
@server.run
|
|
|
|
|
|
|
|
req = Net::HTTP::Get.new("/")
|
|
|
|
req['HOST'] = "example.com"
|
|
|
|
req['X_FORWARDED_PROTO'] = "https"
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
res = Net::HTTP.start @host, @server.connected_port do |http|
|
2013-02-19 22:23:02 -05:00
|
|
|
http.request(req)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "443", res.body
|
|
|
|
end
|
2013-02-19 22:31:38 -05:00
|
|
|
|
|
|
|
def test_default_server_port
|
|
|
|
@server.app = proc do |env|
|
|
|
|
[200, {}, [env['SERVER_PORT']]]
|
|
|
|
end
|
|
|
|
|
2016-02-04 13:22:17 -05:00
|
|
|
@server.add_tcp_listener @host, @port
|
2013-02-19 22:31:38 -05:00
|
|
|
@server.run
|
|
|
|
|
|
|
|
req = Net::HTTP::Get.new("/")
|
|
|
|
req['HOST'] = "example.com"
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
res = Net::HTTP.start @host, @server.connected_port do |http|
|
2013-02-19 22:31:38 -05:00
|
|
|
http.request(req)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "80", res.body
|
|
|
|
end
|
2013-06-18 17:27:46 -04:00
|
|
|
|
|
|
|
def test_HEAD_has_no_body
|
|
|
|
@server.app = proc { |env| [200, {"Foo" => "Bar"}, ["hello"]] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2013-06-18 17:27:46 -04:00
|
|
|
sock << "HEAD / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
2013-07-06 00:35:26 -04:00
|
|
|
assert_equal "HTTP/1.0 200 OK\r\nFoo: Bar\r\nContent-Length: 5\r\n\r\n", data
|
2013-06-18 17:27:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_GET_with_empty_body_has_sane_chunking
|
|
|
|
@server.app = proc { |env| [200, {}, [""]] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2013-06-18 17:27:46 -04:00
|
|
|
sock << "HEAD / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
2013-07-06 00:35:26 -04:00
|
|
|
assert_equal "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n", data
|
2013-06-18 17:27:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_GET_with_no_body_has_sane_chunking
|
2013-07-06 00:35:26 -04:00
|
|
|
@server.app = proc { |env| [200, {}, []] }
|
2013-06-18 17:27:46 -04:00
|
|
|
|
2016-02-04 13:22:17 -05:00
|
|
|
@server.add_tcp_listener @host, @port
|
2013-06-18 17:27:46 -04:00
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2013-06-18 17:27:46 -04:00
|
|
|
sock << "HEAD / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.0 200 OK\r\n\r\n", data
|
|
|
|
end
|
2013-07-01 19:48:17 -04:00
|
|
|
|
|
|
|
def test_doesnt_print_backtrace_in_production
|
|
|
|
@events = Puma::Events.strings
|
|
|
|
@server = Puma::Server.new @app, @events
|
|
|
|
|
|
|
|
@server.app = proc { |e| raise "don't leak me bro" }
|
|
|
|
@server.leak_stack_on_error = false
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2013-07-01 19:48:17 -04:00
|
|
|
sock << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_not_match(/don't leak me bro/, data)
|
2014-02-17 12:07:17 -05:00
|
|
|
assert_match(/HTTP\/1.0 500 Internal Server Error/, data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_prints_custom_error
|
|
|
|
@events = Puma::Events.strings
|
|
|
|
re = lambda { |err| [302, {'Content-Type' => 'text', 'Location' => 'foo.html'}, ['302 found']] }
|
2015-07-29 10:16:24 -04:00
|
|
|
@server = Puma::Server.new @app, @events, {:lowlevel_error_handler => re}
|
2014-02-17 12:07:17 -05:00
|
|
|
|
|
|
|
@server.app = proc { |e| raise "don't leak me bro" }
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2016-02-25 16:33:32 -05:00
|
|
|
sock << "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
|
|
|
|
@events = Puma::Events.strings
|
|
|
|
re = lambda { |err,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
|
|
|
|
|
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2014-02-17 12:07:17 -05:00
|
|
|
sock << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
assert_match(/HTTP\/1.0 302 Found/, data)
|
2013-07-01 19:48:17 -04:00
|
|
|
end
|
2013-07-05 17:40:34 -04:00
|
|
|
|
|
|
|
def test_custom_http_codes_10
|
|
|
|
@server.app = proc { |env| [449, {}, [""]] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2013-07-06 00:35:26 -04:00
|
|
|
|
2013-07-05 17:40:34 -04:00
|
|
|
sock << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
2015-02-19 08:43:17 -05:00
|
|
|
assert_equal "HTTP/1.0 449 CUSTOM\r\nContent-Length: 0\r\n\r\n", data
|
2013-07-05 17:40:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_custom_http_codes_11
|
|
|
|
@server.app = proc { |env| [449, {}, [""]] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-11-06 14:00:58 -05:00
|
|
|
sock << "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"
|
2013-07-05 17:40:34 -04:00
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
2015-11-06 14:00:58 -05:00
|
|
|
assert_equal "HTTP/1.1 449 CUSTOM\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
|
2013-07-05 17:40:34 -04:00
|
|
|
end
|
2013-07-06 00:35:26 -04:00
|
|
|
|
|
|
|
def test_HEAD_returns_content_headers
|
|
|
|
@server.app = proc { |env| [200, {"Content-Type" => "application/pdf",
|
|
|
|
"Content-Length" => "4242"}, []] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2013-07-06 00:35:26 -04:00
|
|
|
|
|
|
|
sock << "HEAD / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.0 200 OK\r\nContent-Type: application/pdf\r\nContent-Length: 4242\r\n\r\n", data
|
|
|
|
end
|
2013-09-13 12:56:39 -04:00
|
|
|
|
|
|
|
def test_status_hook_fires_when_server_changes_states
|
|
|
|
|
|
|
|
states = []
|
|
|
|
|
|
|
|
@events.register(:state) { |s| states << s }
|
|
|
|
|
|
|
|
@server.app = proc { |env| [200, {}, [""]] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2013-09-13 12:56:39 -04:00
|
|
|
sock << "HEAD / HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
sock.read
|
|
|
|
|
|
|
|
assert_equal [:booting, :running], states
|
|
|
|
|
|
|
|
@server.stop(true)
|
|
|
|
|
|
|
|
assert_equal [:booting, :running, :stop, :done], states
|
|
|
|
end
|
2014-01-30 17:55:44 -05:00
|
|
|
|
|
|
|
def test_timeout_in_data_phase
|
|
|
|
@server.first_data_timeout = 2
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
client = TCPSocket.new @host, @server.connected_port
|
2014-01-30 17:55:44 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 408 Request Timeout\r\n", data
|
|
|
|
end
|
2015-02-19 08:43:17 -05:00
|
|
|
|
|
|
|
def test_http_11_keep_alive_with_body
|
2016-02-06 12:48:02 -05:00
|
|
|
@server.app = proc { |env| [200, {"Content-Type" => "plain/text"}, ["hello\n"]] }
|
2015-02-19 08:43:17 -05:00
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-02-19 08:43:17 -05:00
|
|
|
sock << "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\n\r\n"
|
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
h = header(sock)
|
|
|
|
|
|
|
|
body = sock.gets
|
|
|
|
|
|
|
|
assert_equal ["HTTP/1.1 200 OK", "Content-Type: plain/text", "Content-Length: 6"], h
|
|
|
|
assert_equal "hello\n", body
|
2015-02-19 08:43:17 -05:00
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
sock.close
|
2015-02-19 08:43:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_http_11_close_with_body
|
|
|
|
@server.app = proc { |env| [200, {"Content-Type" => "plain/text"}, ["hello"]] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-02-19 08:43:17 -05:00
|
|
|
sock << "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
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.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-02-19 08:43:17 -05:00
|
|
|
sock << "GET / HTTP/1.1\r\nConnection: Keep-Alive\r\n\r\n"
|
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
h = header(sock)
|
|
|
|
|
|
|
|
sock.close
|
2015-02-19 08:43:17 -05:00
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
assert_equal ["HTTP/1.1 204 No Content"], h
|
2015-02-19 08:43:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_http_11_close_without_body
|
|
|
|
@server.app = proc { |env| [204, {}, []] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-02-19 08:43:17 -05:00
|
|
|
sock << "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"
|
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
h = header(sock)
|
2015-02-19 08:43:17 -05:00
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
sock.close
|
|
|
|
|
|
|
|
assert_equal ["HTTP/1.1 204 No Content", "Connection: close"], h
|
2015-02-19 08:43:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_http_10_keep_alive_with_body
|
2016-02-06 12:48:02 -05:00
|
|
|
@server.app = proc { |env| [200, {"Content-Type" => "plain/text"}, ["hello\n"]] }
|
2015-02-19 08:43:17 -05:00
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-02-19 08:43:17 -05:00
|
|
|
sock << "GET / HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n"
|
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
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
|
2015-02-19 08:43:17 -05:00
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
sock.close
|
2015-02-19 08:43:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_http_10_close_with_body
|
|
|
|
@server.app = proc { |env| [200, {"Content-Type" => "plain/text"}, ["hello"]] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-02-19 08:43:17 -05:00
|
|
|
sock << "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.0 200 OK\r\nContent-Type: plain/text\r\nContent-Length: 5\r\n\r\nhello", data
|
|
|
|
end
|
2016-02-04 11:15:41 -05:00
|
|
|
|
2015-06-08 12:39:49 -04:00
|
|
|
def test_http_10_partial_hijack_with_content_length
|
|
|
|
body_parts = ['abc', 'de']
|
2016-02-04 11:15:41 -05:00
|
|
|
|
|
|
|
@server.app = proc do |env|
|
2015-06-08 12:39:49 -04:00
|
|
|
hijack_lambda = proc do | io |
|
|
|
|
io.write(body_parts[0])
|
|
|
|
io.write(body_parts[1])
|
|
|
|
io.close
|
|
|
|
end
|
|
|
|
[200, {"Content-Length" => "5", 'rack.hijack' => hijack_lambda}, nil]
|
|
|
|
end
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-06-08 12:39:49 -04:00
|
|
|
sock << "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"
|
2015-02-19 08:43:17 -05:00
|
|
|
|
2015-06-08 12:39:49 -04:00
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nabcde", data
|
|
|
|
end
|
2016-02-04 11:15:41 -05:00
|
|
|
|
2015-02-19 08:43:17 -05:00
|
|
|
def test_http_10_keep_alive_without_body
|
|
|
|
@server.app = proc { |env| [204, {}, []] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-02-19 08:43:17 -05:00
|
|
|
sock << "GET / HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n"
|
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
h = header(sock)
|
|
|
|
|
|
|
|
assert_equal ["HTTP/1.0 204 No Content", "Connection: Keep-Alive"], h
|
2015-02-19 08:43:17 -05:00
|
|
|
|
2016-02-06 12:48:02 -05:00
|
|
|
sock.close
|
2015-02-19 08:43:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_http_10_close_without_body
|
|
|
|
@server.app = proc { |env| [204, {}, []] }
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
2016-02-04 11:15:41 -05:00
|
|
|
sock = TCPSocket.new @host, @server.connected_port
|
2015-02-19 08:43:17 -05:00
|
|
|
sock << "GET / HTTP/1.0\r\nConnection: close\r\n\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.0 204 No Content\r\n\r\n", data
|
|
|
|
end
|
2016-07-24 23:58:16 -04:00
|
|
|
|
|
|
|
def test_Expect_100
|
|
|
|
@server.app = proc { |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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-07-25 01:02:23 -04:00
|
|
|
def test_chunked_request
|
|
|
|
body = nil
|
|
|
|
@server.app = proc { |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"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
|
|
|
|
assert_equal "hello", body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chunked_request_pause_before_value
|
|
|
|
body = nil
|
|
|
|
@server.app = proc { |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"
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
sock << "h\r\n4\r\nello\r\n0\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
|
|
|
|
assert_equal "hello", body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chunked_request_pause_between_chunks
|
|
|
|
body = nil
|
|
|
|
@server.app = proc { |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"
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
sock << "4\r\nello\r\n0\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
|
|
|
|
assert_equal "hello", body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chunked_request_pause_mid_count
|
|
|
|
body = nil
|
|
|
|
@server.app = proc { |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"
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
sock << "\nh\r\n4\r\nello\r\n0\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
|
|
|
|
assert_equal "hello", body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chunked_request_pause_before_count_newline
|
|
|
|
body = nil
|
|
|
|
@server.app = proc { |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"
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
sock << "\r\nh\r\n4\r\nello\r\n0\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
|
|
|
|
assert_equal "hello", body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chunked_request_pause_mid_value
|
|
|
|
body = nil
|
|
|
|
@server.app = proc { |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"
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
sock << "llo\r\n0\r\n"
|
|
|
|
|
|
|
|
data = sock.read
|
|
|
|
|
|
|
|
assert_equal "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", data
|
|
|
|
assert_equal "hello", body
|
|
|
|
end
|
2012-04-11 15:09:59 -04:00
|
|
|
end
|