diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-11-03 21:01:42 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-11-03 21:01:42 +0900 |
commit | a7fddee76f78a97059d798d76c2d8d85c643c7aa (patch) | |
tree | 1d535c7096aeac4b046984b6ad66502a6a293ef9 | |
parent | 2e9abcb889e625ad1dab8f37fc20b12dfcd6d3fc (diff) | |
download | plum-a7fddee76f78a97059d798d76c2d8d85c643c7aa.tar.gz |
client: add test
-rw-r--r-- | test/plum/client/test_client.rb | 81 | ||||
-rw-r--r-- | test/test_helper.rb | 6 |
2 files changed, 85 insertions, 2 deletions
diff --git a/test/plum/client/test_client.rb b/test/plum/client/test_client.rb new file mode 100644 index 0000000..c169286 --- /dev/null +++ b/test/plum/client/test_client.rb @@ -0,0 +1,81 @@ +require "test_helper" + +using Plum::BinaryString +class ClientTest < Minitest::Test + def test_request + server_thread = start_tls_server + client = Client.start("127.0.0.1", LISTEN_PORT, https: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) + res1 = client.request({ ":path" => "/", ":method" => "POST", ":scheme" => "https", "header" => "ccc" }, "abc") + assert_equal("POSTcccabc", res1.body) + res2 = client.put("/", "aaa", { ":scheme" => "https", "header" => "ccc" }) + assert_equal("PUTcccaaa", res2.body) + client.close + ensure + server_thread.join + end + + def test_request_async + server_thread = start_tls_server + res2 = nil + Client.start("127.0.0.1", LISTEN_PORT, https: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) { |client| + res1 = client.request_async({ ":path" => "/", ":method" => "GET", ":scheme" => "https", "header" => "ccc" }) { |res1| + assert(res1.headers) + } + assert_nil(res1.headers) + + res2 = client.get_async("/", "header" => "ccc") + } + assert(res2.headers) + assert_equal("GETccc", res2.body) + ensure + server_thread.join + end + + def test_verify + server_thread = start_tls_server + assert_raises(OpenSSL::SSL::SSLError) { + Client.start("127.0.0.1", LISTEN_PORT, https: true, verify_mode: OpenSSL::SSL::VERIFY_PEER) + } + ensure + server_thread.join + end + + private + def start_tls_server(&block) + ctx = OpenSSL::SSL::SSLContext.new + ctx.alpn_select_cb = -> protocols { "h2" } + ctx.cert = TLS_CERT + ctx.key = TLS_KEY + tcp_server = TCPServer.new("127.0.0.1", LISTEN_PORT) + ssl_server = OpenSSL::SSL::SSLServer.new(tcp_server, ctx) + + server_thread = Thread.new { + begin + Timeout.timeout(3) { + sock = ssl_server.accept + plum = HTTPSServerConnection.new(sock) + + plum.on(:stream) { |stream| + headers = data = nil + stream.on(:headers) { |h| + headers = h } + stream.on(:data) { |d| + data = d } + stream.on(:end_stream) { + stream.respond({ ":status" => 200 }, headers.to_h[":method"] + headers.to_h["header"] + data.to_s) } + } + + yield plum if block_given? + plum.run + } + rescue OpenSSL::SSL::SSLError + rescue Timeout::Error + flunk "server timeout" + rescue => e + flunk e + ensure + tcp_server.close + end + } + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index c5fae66..2e3271b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,3 @@ -LISTEN_PORT = ENV["PLUM_LISTEN_PORT"] || 40444 - unless ENV["SKIP_COVERAGE"] begin require "simplecov" @@ -26,3 +24,7 @@ include Plum Dir.glob(File.expand_path("../utils/*.rb", __FILE__)).each do |file| require file end + +LISTEN_PORT = ENV["PLUM_LISTEN_PORT"] || 40444 +TLS_CERT = OpenSSL::X509::Certificate.new File.read(File.expand_path("../server.crt", __FILE__)) +TLS_KEY = OpenSSL::PKey::RSA.new File.read(File.expand_path("../server.key", __FILE__)) |