Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move WEBrick SSL config to script/test

  • Loading branch information...
commit 9b3923f62c1e0c15172439d077c45305598f037b 1 parent db73953
@mislav mislav authored
View
1  .gitignore
@@ -4,6 +4,7 @@ rdoc
doc
log
pkg/*
+tmp
.rvmrc
.rbenv*
View
4 .travis.yml
@@ -6,6 +6,10 @@ matrix:
allow_failures:
- rvm: ruby-head
+env:
+ - SSL=no
+ - SSL=yes
+
rvm:
- rbx-18mode
- rbx-19mode
View
11 Rakefile
@@ -1,4 +1,5 @@
require 'date'
+require 'fileutils'
require 'openssl'
require 'rake/testtask'
@@ -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"
@@ -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
View
26 script/test
@@ -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'
@@ -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'
@@ -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
@@ -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|
@@ -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
View
21 test/adapters/integration.rb
@@ -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."
@@ -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
@@ -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
@@ -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
View
6 test/adapters/net_http_test.rb
@@ -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
View
22 test/live_server.rb
@@ -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.
Something went wrong with that request. Please try again.