diff --git a/bin/httparty b/bin/httparty old mode 100644 new mode 100755 diff --git a/lib/httparty.rb b/lib/httparty.rb index aef4ee6..9885e58 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -299,6 +299,17 @@ module HTTParty default_options[:query_string_normalizer] = normalizer end + # Allows setting of SSL version to use. This only works in Ruby 1.9. + # You can get a list of valid versions from OpenSSL::SSL::SSLContext::METHODS. + # + # class Foo + # include HTTParty + # ssl_version :SSLv3 + # end + def ssl_version(version) + default_options[:ssl_version] = version + end + # Allows setting an OpenSSL certificate authority file # # class Foo diff --git a/lib/httparty/connection_adapter.rb b/lib/httparty/connection_adapter.rb index 9c542b6..8865a23 100644 --- a/lib/httparty/connection_adapter.rb +++ b/lib/httparty/connection_adapter.rb @@ -105,6 +105,10 @@ module HTTParty http.ca_path = options[:ssl_ca_path] http.verify_mode = OpenSSL::SSL::VERIFY_PEER end + + if options[:ssl_version] && http.respond_to?(:ssl_version=) + http.ssl_version = options[:ssl_version] + end end end end diff --git a/spec/httparty/connection_adapter_spec.rb b/spec/httparty/connection_adapter_spec.rb index 4273efe..e919e9b 100644 --- a/spec/httparty/connection_adapter_spec.rb +++ b/spec/httparty/connection_adapter_spec.rb @@ -56,25 +56,20 @@ describe HTTParty::ConnectionAdapter do subject { adapter.connection } it { should be_an_instance_of Net::HTTP } + context "using port 80" do + let(:uri) { URI 'http://foobar.com' } + it { should_not use_ssl } + end + context "when dealing with ssl" do - Spec::Matchers.define :use_ssl do - match do |connection| - connection.use_ssl? - end - end + let(:uri) { URI 'https://foobar.com' } context "using port 443 for ssl" do let(:uri) { URI 'https://api.foo.com/v1:443' } it { should use_ssl } end - context "using port 80" do - let(:uri) { URI 'http://foobar.com' } - it { should_not use_ssl } - end - context "https scheme with default port" do - let(:uri) { URI 'https://foobar.com' } it { should use_ssl } end @@ -82,6 +77,14 @@ describe HTTParty::ConnectionAdapter do let(:uri) { URI 'https://foobar.com:123456' } it { should use_ssl } end + + context "when ssl version is set" do + let(:options) { {:ssl_version => :TLSv1} } + + it "sets ssl version" do + subject.ssl_version.should == :TLSv1 + end + end end context "when timeout is not set" do @@ -199,6 +202,5 @@ describe HTTParty::ConnectionAdapter do end end end - end end diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index a5516fb..f62a322 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -38,6 +38,13 @@ describe HTTParty do end end + describe 'ssl_version' do + it 'should set the ssl_version content' do + @klass.ssl_version :SSLv3 + @klass.default_options[:ssl_version].should == :SSLv3 + end + end + describe 'http_proxy' do it 'should set the address' do @klass.http_proxy 'proxy.foo.com', 80 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 514c490..a85b2b1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,3 +22,9 @@ Spec::Runner.configure do |config| FakeWeb.allow_net_connect = true end end + +Spec::Matchers.define :use_ssl do + match do |connection| + connection.use_ssl? + end +end