Accept certificate and private_key as strings #236

Merged
merged 2 commits into from Jun 12, 2013
View
2 lib/excon/constants.rb
@@ -35,6 +35,8 @@ module Excon
:chunk_size,
:client_key,
:client_cert,
+ :certificate,
+ :private_key,
:connect_timeout,
:connection,
:error,
View
13 lib/excon/ssl_socket.rb
@@ -24,9 +24,16 @@ def initialize(data = {})
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
- if @data.has_key?(:client_cert) && @data.has_key?(:client_key)
- ssl_context.cert = OpenSSL::X509::Certificate.new(File.read(@data[:client_cert]))
- ssl_context.key = OpenSSL::PKey::RSA.new(File.read(@data[:client_key]))
+ # maintain existing API
+ certificate_path = @data[:client_cert] || @data[:certificate_path]
+ private_key_path = @data[:client_key] || @data[:private_key_path]
+
+ if certificate_path && private_key_path
+ ssl_context.cert = OpenSSL::X509::Certificate.new(File.read(certificate_path))
+ ssl_context.key = OpenSSL::PKey::RSA.new(File.read(private_key_path))
+ elsif @data.has_key?(:certificate) && @data.has_key?(:private_key)
+ ssl_context.cert = OpenSSL::X509::Certificate.new(@data[:certificate])
+ ssl_context.key = OpenSSL::PKey::RSA.new(@data[:private_key])
end
if @data[:proxy]
View
38 tests/basic_tests.rb
@@ -52,7 +52,7 @@
end
with_rackup('ssl_verify_peer.ru') do
- Shindo.tests('Excon basics (ssl)',['focus']) do
+ Shindo.tests('Excon basics (ssl file)',['focus']) do
connection = Excon::Connection.new({
:host => '127.0.0.1',
:nonblock => false,
@@ -69,5 +69,41 @@
:client_key => File.join(File.dirname(__FILE__), 'data', 'excon.cert.key'),
:client_cert => File.join(File.dirname(__FILE__), 'data', 'excon.cert.crt')
)
+
+ end
+
+ Shindo.tests('Excon basics (ssl file paths)',['focus']) do
+ connection = Excon::Connection.new({
+ :host => '127.0.0.1',
+ :nonblock => false,
+ :port => 8443,
+ :scheme => 'https',
+ :ssl_verify_peer => false
+ })
+
+ tests('GET /content-length/100').raises(Excon::Errors::SocketError) do
+ connection.request(:method => :get, :path => '/content-length/100')
+ end
+
+ basic_tests('https://127.0.0.1:8443',
+ :private_key_path => File.join(File.dirname(__FILE__), 'data', 'excon.cert.key'),
+ :certificate_path => File.join(File.dirname(__FILE__), 'data', 'excon.cert.crt')
+ )
+
+ end
+
+ Shindo.tests('Excon basics (ssl string)', ['focus']) do
+ connection = Excon::Connection.new({
+ :host => '127.0.0.1',
+ :nonblock => false,
+ :port => 8443,
+ :scheme => 'https',
+ :ssl_verify_peer => false
+ })
+
+ basic_tests('https://127.0.0.1:8443',
+ :private_key => File.read(File.join(File.dirname(__FILE__), 'data', 'excon.cert.key')),
+ :certificate => File.read(File.join(File.dirname(__FILE__), 'data', 'excon.cert.crt'))
+ )
end
end