script/test in faraday-0.8.4 vs script/test in faraday-0.8.5

- old
+ new

@@ -8,10 +8,11 @@ # Examples # # $ script/test # $ script/test test/env_test.rb # $ script/test excon typhoeus +# $ SSL=yes script/test net_http -- -n /ssl/ require 'rubygems' require 'bundler' begin Bundler.setup @@ -22,14 +23,23 @@ exit 1 end $VERBOSE = true -host = '127.0.0.1' +host = 'localhost' logfile = 'log/test.log' test_glob = 'test/**/*_test.rb' +if ssl_mode = ENV['SSL'] == 'yes' + unless ENV['SSL_KEY'] and ENV['SSL_FILE'] + key_file = ENV['SSL_KEY'] = 'tmp/faraday-cert.key' + cert_file = ENV['SSL_FILE'] = 'tmp/faraday-cert.crt' + system 'rake', key_file, cert_file + abort unless $?.success? + end +end + require 'fileutils' FileUtils.mkdir_p 'log' # find available port require 'socket' @@ -40,10 +50,12 @@ server.close if server end server = nil +Thread.abort_on_exception = true + # start test server in a separate thread thread = Thread.new do old_verbose, $VERBOSE = $VERBOSE, nil begin require File.expand_path('../../test/live_server', __FILE__) @@ -55,10 +67,18 @@ log_io.sync = true webrick_opts = { :Port => port, :Logger => WEBrick::Log::new(log_io), :AccessLog => [[log_io, "[%{X-Faraday-Adapter}i] %m %U -> %s %b"]] } + if ssl_mode + require 'webrick/https' + webrick_opts.update \ + :SSLEnable => true, + :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(key_file)), + :SSLCertificate => OpenSSL::X509::Certificate.new(File.read(cert_file)), + :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE + end Rack::Handler::WEBrick.run(Faraday::LiveServer, webrick_opts) {|serv| server = serv } end # find files to test test_files = Dir[test_glob] @@ -74,30 +94,38 @@ test_files.concat all_files.select { |f| f =~ re } end test_files.concat extra_args end -require 'net/http' +require 'net/https' conn = Net::HTTP.new host, port -conn.open_timeout = conn.read_timeout = 0.05 +conn.open_timeout = conn.read_timeout = 0.1 +conn.use_ssl = ssl_mode +conn.verify_mode = OpenSSL::SSL::VERIFY_NONE # test if test server is accepting requests responsive = lambda { |path| begin res = conn.start { conn.get(path) } res.is_a?(Net::HTTPSuccess) - rescue Errno::ECONNREFUSED, Errno::EBADF, Timeout::Error + rescue Errno::ECONNREFUSED, Errno::EBADF, Timeout::Error, Net::HTTPBadResponse false end } -require 'timeout' -Timeout.timeout 40 do +server_pings = 0 +begin # block until test server is ready - thread.join 0.05 until responsive.call('/echo') -end + thread.join 0.05 + server_pings += 1 + abort "test server didn't manage to start" if server_pings >= 50 +end until responsive.call('/echo') -ENV['LIVE'] = "http://#{host}:#{port}" -system 'ruby', '-Ilib:test', '-S', 'testrb', *test_files +ENV['LIVE'] = "http#{ssl_mode ? 's' : ''}://#{host}:#{port}" +ok = system 'ruby', '-Ilib:test', + '-e', 'load(ARGV.shift) while ARGV.first =~ /^[^-]/', + *test_files server.respond_to?(:stop!) ? server.stop! : server.stop thread.join + +exit 1 unless ok