Skip to content

Commit

Permalink
Disabled Net::HTTP::Persistent::SSLReuse for Ruby 2.0+. The feature i…
Browse files Browse the repository at this point in the history
…s now built-in
  • Loading branch information
drbrain committed Oct 17, 2012
1 parent dad5cb7 commit 3491813
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
2 changes: 2 additions & 0 deletions History.txt
Expand Up @@ -8,6 +8,8 @@
#26 by dlee. #26 by dlee.
* The artifice gem now disables SSL session reuse to prevent breakage of * The artifice gem now disables SSL session reuse to prevent breakage of
testing frameworks. Pull Request #29 by Christopher Cooke. testing frameworks. Pull Request #29 by Christopher Cooke.
* Disabled Net::HTTP::Persistent::SSLReuse on Ruby 2+. This feature is now
built-in to Net::HTTP.
* Bug fixes * Bug fixes
* Socket options are set again following connection reset. Pull request #28 * Socket options are set again following connection reset. Pull request #28
by cmaion. by cmaion.
Expand Down
7 changes: 5 additions & 2 deletions lib/net/http/persistent.rb
Expand Up @@ -642,8 +642,11 @@ def finish connection, thread = Thread.current
end end


def http_class # :nodoc: def http_class # :nodoc:
if [:Artifice, :FakeWeb, :WebMock].any? { |klass| Object.const_defined?(klass) } or if RUBY_VERSION > '2.0' then
not @reuse_ssl_sessions then Net::HTTP
elsif [:Artifice, :FakeWeb, :WebMock].any? { |klass|
Object.const_defined?(klass)
} or not @reuse_ssl_sessions then
Net::HTTP Net::HTTP
else else
Net::HTTP::Persistent::SSLReuse Net::HTTP::Persistent::SSLReuse
Expand Down
52 changes: 42 additions & 10 deletions test/test_net_http_persistent.rb
Expand Up @@ -4,6 +4,38 @@
require 'openssl' require 'openssl'
require 'stringio' require 'stringio'


class Net::HTTP
alias orig_connect connect

def test_connect
unless use_ssl? then
io = Object.new
def io.setsockopt(*a) @setsockopts ||= []; @setsockopts << a end

@socket = Net::BufferedIO.new io

return
end

io = open '/dev/null'
def io.setsockopt(*a) @setsockopts ||= []; @setsockopts << a end

@ssl_context ||= OpenSSL::SSL::SSLContext.new

@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER unless
@ssl_context.verify_mode

s = OpenSSL::SSL::SSLSocket.new io, @ssl_context

@socket = Net::BufferedIO.new s
end

def self.use_connect which
self.send :remove_method, :connect
self.send :alias_method, :connect, which
end
end

class Net::HTTP::Persistent::SSLReuse class Net::HTTP::Persistent::SSLReuse
alias orig_connect connect alias orig_connect connect


Expand Down Expand Up @@ -39,7 +71,14 @@ def self.use_connect which
class TestNetHttpPersistent < MiniTest::Unit::TestCase class TestNetHttpPersistent < MiniTest::Unit::TestCase


def setup def setup
@http_class = if RUBY_VERSION > '2.0' then
Net::HTTP
else
Net::HTTP::Persistent::SSLReuse
end

@http = Net::HTTP::Persistent.new @http = Net::HTTP::Persistent.new

@uri = URI.parse 'http://example.com/path' @uri = URI.parse 'http://example.com/path'


ENV.delete 'http_proxy' ENV.delete 'http_proxy'
Expand All @@ -51,6 +90,7 @@ def setup
ENV.delete 'no_proxy' ENV.delete 'no_proxy'
ENV.delete 'NO_PROXY' ENV.delete 'NO_PROXY'


Net::HTTP.use_connect :test_connect
Net::HTTP::Persistent::SSLReuse.use_connect :test_connect Net::HTTP::Persistent::SSLReuse.use_connect :test_connect
end end


Expand All @@ -59,6 +99,7 @@ def teardown
Thread.current[key] = nil Thread.current[key] = nil
end end


Net::HTTP.use_connect :orig_connect
Net::HTTP::Persistent::SSLReuse.use_connect :orig_connect Net::HTTP::Persistent::SSLReuse.use_connect :orig_connect
end end


Expand Down Expand Up @@ -209,7 +250,7 @@ def test_connection_for
@http.read_timeout = 321 @http.read_timeout = 321
c = @http.connection_for @uri c = @http.connection_for @uri


assert_kind_of Net::HTTP::Persistent::SSLReuse, c assert_kind_of @http_class, c


assert c.started? assert c.started?
refute c.proxy? refute c.proxy?
Expand All @@ -219,15 +260,6 @@ def test_connection_for


assert_includes conns[0].keys, 'example.com:80' assert_includes conns[0].keys, 'example.com:80'
assert_same c, conns[0]['example.com:80'] assert_same c, conns[0]['example.com:80']

socket = c.instance_variable_get :@socket
expected = if Socket.const_defined? :TCP_NODELAY then
[[Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1]]
else
[]
end

assert_equal expected, socket.io.instance_variable_get(:@setsockopts)
end end


def test_connection_for_cached def test_connection_for_cached
Expand Down

0 comments on commit 3491813

Please sign in to comment.