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
|
|
|
|
@port = 3212
|
|
|
|
@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
|
|
|
|
|
2012-08-26 16:55:36 -04:00
|
|
|
if defined?(JRUBY_VERSION)
|
|
|
|
@ssl_key = File.expand_path "../../examples/puma/keystore.jks", __FILE__
|
|
|
|
@ssl_cert = @ssl_key
|
|
|
|
else
|
|
|
|
@ssl_key = File.expand_path "../../examples/puma/puma_keypair.pem", __FILE__
|
|
|
|
@ssl_cert = File.expand_path "../../examples/puma/cert_puma.pem", __FILE__
|
|
|
|
end
|
2012-04-11 15:09:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2012-05-15 18:38:22 -04:00
|
|
|
@server.stop(true)
|
2012-04-11 15:09:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_url_scheme_for_https
|
2012-08-23 01:12:12 -04:00
|
|
|
ctx = Puma::MiniSSL::Context.new
|
2012-04-11 15:09:59 -04:00
|
|
|
|
2012-08-23 00:43:40 -04:00
|
|
|
ctx.key = @ssl_key
|
|
|
|
ctx.cert = @ssl_cert
|
2012-04-11 15:09:59 -04:00
|
|
|
|
2012-08-23 01:12:12 -04:00
|
|
|
ctx.verify_mode = Puma::MiniSSL::VERIFY_NONE
|
2012-04-11 15:09:59 -04:00
|
|
|
|
|
|
|
@server.add_ssl_listener @host, @port, ctx
|
|
|
|
@server.run
|
|
|
|
|
|
|
|
http = Net::HTTP.new @host, @port
|
|
|
|
http.use_ssl = true
|
2012-08-23 01:12:12 -04:00
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
2012-04-11 15:09:59 -04:00
|
|
|
|
|
|
|
body = nil
|
|
|
|
http.start do
|
|
|
|
req = Net::HTTP::Get.new "/", {}
|
|
|
|
|
|
|
|
http.request(req) do |rep|
|
|
|
|
body = rep.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "https", body
|
2012-11-29 14:58:08 -05:00
|
|
|
end unless defined? JRUBY_VERSION
|
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
|
|
|
|
|
|
|
|
sock = TCPSocket.new @host, @port
|
|
|
|
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
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
|
|
|
sock = TCPSocket.new @host, @port
|
|
|
|
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
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
|
|
|
sock = TCPSocket.new @host, @port
|
|
|
|
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
|
|
|
|
|
|
|
|
@server.add_tcp_listener @host, @port
|
|
|
|
@server.run
|
|
|
|
|
|
|
|
req = Net::HTTP::Get.new("/")
|
|
|
|
req['HOST'] = "example.com"
|
|
|
|
req['X_FORWARDED_PROTO'] = "https"
|
|
|
|
|
|
|
|
res = Net::HTTP.start @host, @port do |http|
|
|
|
|
http.request(req)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "443", res.body
|
|
|
|
end
|
2012-04-11 15:09:59 -04:00
|
|
|
end
|