Skip to content

Commit

Permalink
move WEBrick SSL config to script/test
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Aug 16, 2012
1 parent db73953 commit 9b3923f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,6 +4,7 @@ rdoc
doc
log
pkg/*
tmp
.rvmrc
.rbenv*

Expand Down
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -6,6 +6,10 @@ matrix:
allow_failures:
- rvm: ruby-head

env:
- SSL=no
- SSL=yes

rvm:
- rbx-18mode
- rbx-19mode
Expand Down
11 changes: 8 additions & 3 deletions Rakefile
@@ -1,4 +1,5 @@
require 'date'
require 'fileutils'
require 'openssl'
require 'rake/testtask'

Expand Down Expand Up @@ -54,10 +55,14 @@ end
desc "Generate certificates for SSL tests"
task :'test:generate_certs' do
cert, key = create_self_signed_cert(1024, [['CN', 'localhost']], 'Faraday Test CA')
File.open('faraday.cert.crt', 'w') {|f| f.puts(cert.to_s) }
File.open('faraday.cert.key', 'w') {|f| f.puts(key) }
FileUtils.mkdir_p 'tmp'
File.open('tmp/faraday-cert.key', 'w') {|f| f.puts(key) }
File.open('tmp/faraday-cert.crt', 'w') {|f| f.puts(cert.to_s) }
end

file 'tmp/faraday-cert.key' => :'test:generate_certs'
file 'tmp/faraday-cert.crt' => :'test:generate_certs'

desc "Open an irb session preloaded with this library"
task :console do
sh "irb -rubygems -r ./lib/#{name}.rb"
Expand All @@ -76,7 +81,7 @@ end

desc "Build #{gem_file} into the pkg directory"
task :build => :gemspec do
sh "mkdir -p pkg"
FileUtils.mkdir_p 'pkg'
sh "gem build #{gemspec_file}"
sh "mv #{gem_file} pkg"
end
Expand Down
26 changes: 23 additions & 3 deletions script/test
Expand Up @@ -10,6 +10,7 @@
# $ script/test
# $ script/test test/env_test.rb
# $ script/test excon typhoeus
# $ SSL=yes script/test net_http -- -n /ssl/

require 'rubygems'
require 'bundler'
Expand All @@ -24,10 +25,19 @@ 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'

Expand Down Expand Up @@ -59,6 +69,14 @@ thread = Thread.new do
: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

Expand All @@ -78,9 +96,11 @@ if ARGV.any?
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.1
conn.use_ssl = ssl_mode
conn.verify_mode = OpenSSL::SSL::VERIFY_NONE

# test if test server is accepting requests
responsive = lambda { |path|
Expand All @@ -100,7 +120,7 @@ begin
abort "test server didn't manage to start" if server_pings >= 50
end until responsive.call('/echo')

ENV['LIVE'] = "http://#{host}:#{port}"
ENV['LIVE'] = "http#{ssl_mode ? 's' : ''}://#{host}:#{port}"
ok = system 'ruby', '-Ilib:test', '-S', 'testrb', *test_files

server.respond_to?(:stop!) ? server.stop! : server.stop
Expand Down
21 changes: 10 additions & 11 deletions test/adapters/integration.rb
Expand Up @@ -7,11 +7,11 @@ module Adapters
# `#adapter` required. returns a symbol for the adapter middleware name
# `#adapter_options` optional. extra arguments for building an adapter
module Integration
def self.apply(base, *extras)
def self.apply(base, *extra_features)
if base.live_server?
modules = [:Common]
modules << :SSL if base.live_server.scheme == 'https'
modules.concat(extras).each {|name| base.send(:include, self.const_get(name)) }
features = [:Common]
features.concat extra_features
features.each {|name| base.send(:include, self.const_get(name)) }
yield if block_given?
elsif !defined? @warned
warn "Warning: Not running integration tests against a live server."
Expand Down Expand Up @@ -61,12 +61,6 @@ def test_GET_handles_compression
end
end

module SSL
def test_ssl_secure
assert_equal "true", get('ssl').body
end
end

module Common
extend Forwardable
def_delegators :create_connection, :get, :head, :put, :post, :patch, :delete, :run_request
Expand Down Expand Up @@ -101,6 +95,11 @@ def test_GET_sends_user_agent
assert_equal 'Agent Faraday', response.body
end

def test_GET_ssl
expected = (ENV['SSL'] == 'yes').to_s
assert_equal expected, get('ssl').body
end

def test_POST_send_url_encoded_params
assert_equal %(post {"name"=>"zack"}), post('echo', :name => 'zack').body
end
Expand Down Expand Up @@ -195,7 +194,7 @@ def create_connection(options = {})
url = '%s://%s:%d' % [server.scheme, server.host, server.port]

options[:ssl] ||= {}
options[:ssl][:ca_file] ||= File.expand_path('../../../faraday.cert.crt', __FILE__)
options[:ssl][:ca_file] ||= ENV['SSL_FILE']

Faraday::Connection.new(url, options, &builder_block).tap do |conn|
conn.headers['X-Faraday-Adapter'] = adapter.to_s
Expand Down
6 changes: 0 additions & 6 deletions test/adapters/net_http_test.rb
Expand Up @@ -10,11 +10,5 @@ def adapter() :net_http end

Integration.apply(self, *behaviors)

def test_configure_ssl
http = Net::HTTP.new 'disney.com', 443
# this should not raise an error
Faraday::Adapter::NetHttp.new.configure_ssl(http, :ssl => {:verify => true})
end

end
end
22 changes: 1 addition & 21 deletions test/live_server.rb
Expand Up @@ -61,25 +61,5 @@ class LiveServer < Sinatra::Base
end

if $0 == __FILE__
options = {
:Port => Faraday::LiveServer.port
}

if (ENV['LIVE'] || '').index('https') == 0
require 'webrick/https'

key = OpenSSL::PKey::RSA.new(File.open(File.expand_path("../../faraday.cert.key", __FILE__)).read)
cert = OpenSSL::X509::Certificate.new(File.open(File.expand_path("../../faraday.cert.crt", __FILE__)).read)
options = {
:SSLEnable => true,
:SSLPrivateKey => key,
:SSLCertificate => cert,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_PEER
}.merge(options)
end

Rack::Handler::WEBrick.run(Faraday::LiveServer, options) do |server|
[:INT, :TERM].each { |sig| trap(sig) { server.stop } }
end
Faraday::LiveServer.run!
end

0 comments on commit 9b3923f

Please sign in to comment.