spec/support/test_server.rb in patron-0.4.18 vs spec/support/test_server.rb in patron-0.4.20

- old
+ new

@@ -22,10 +22,13 @@ ## THE SOFTWARE. ## ## ------------------------------------------------------------------- require 'yaml' require 'webrick' +require 'webrick/https' +require 'openssl' + include WEBrick # This ugly little hack is necessary to make the specs pass when running # the test_server script under Ruby 1.9. URI::Parser#to_yaml generates # regexp representations that YAML.parse cannot parse. @@ -123,33 +126,44 @@ end end class PatronTestServer - def self.start( log_file = nil ) - new(log_file).start + def self.start( log_file = nil, ssl = false, port = 9001 ) + new(log_file, ssl, port).start end - def initialize( log_file = nil ) + def initialize( log_file = nil, ssl = false, port = 9001 ) log_file ||= StringIO.new log = WEBrick::Log.new(log_file) - @server = WEBrick::HTTPServer.new( - :Port => 9001, + options = { + :Port => port, :Logger => log, :AccessLog => [ [ log, WEBrick::AccessLog::COMMON_LOG_FORMAT ], [ log, WEBrick::AccessLog::REFERER_LOG_FORMAT ] ] - ) + } + + if ssl + options[:SSLEnable] = true + options[:SSLCertificate] = OpenSSL::X509::Certificate.new(File.open("spec/certs/cacert.pem").read) + options[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(File.open("spec/certs/privkey.pem").read) + options[:SSLCertName] = [ ["CN", WEBrick::Utils::getservername ] ] + end + + @server = WEBrick::HTTPServer.new(options) + @server.mount("/test", TestServlet) @server.mount("/testpost", TestPostBodyServlet) @server.mount("/timeout", TimeoutServlet) @server.mount("/redirect", RedirectServlet) @server.mount("/picture", PictureServlet) @server.mount("/setcookie", SetCookieServlet) @server.mount("/repetitiveheader", RepetitiveHeaderServlet) @server.mount("/wrongcontentlength", WrongContentLengthServlet) + end def start trap('INT') { begin