From 0abfb4f67ddbf9cc8fc892aa060fcf8e0eb6496e Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Wed, 8 Jan 2014 18:06:00 -0500 Subject: [PATCH] Fix reconnect on keep alive timeout when allowing all connections. --- lib/fake_web.rb | 6 +++++ lib/fake_web/ext/net_http.rb | 4 ++-- test/test_allow_all_connections.rb | 35 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/test_allow_all_connections.rb diff --git a/lib/fake_web.rb b/lib/fake_web.rb index 1539441..8b57d39 100644 --- a/lib/fake_web.rb +++ b/lib/fake_web.rb @@ -68,6 +68,12 @@ def self.allow_net_connect?(uri = nil) end end + # Returns +true+ if requests to all URIs are passed through to Net::HTTP + # for normal processing (the default), otherwise, returns +false+. + def self.allow_all_connections? + Registry.instance.passthrough_uri_map.empty? && Registry.instance.uri_map.empty? && @allow_all_connections + end + # This exception is raised if you set FakeWeb.allow_net_connect = # false and subsequently try to make a request to a URI you haven't # stubbed. diff --git a/lib/fake_web/ext/net_http.rb b/lib/fake_web/ext/net_http.rb index fa00b17..04496f4 100644 --- a/lib/fake_web/ext/net_http.rb +++ b/lib/fake_web/ext/net_http.rb @@ -45,7 +45,7 @@ def request_with_fakeweb(request, body = nil, &block) FakeWeb::Utility.produce_side_effects_of_net_http_request(request, body) FakeWeb.response_for(method, uri, &block) elsif FakeWeb.allow_net_connect?(uri) - connect_without_fakeweb + connect_without_fakeweb unless FakeWeb.allow_all_connections? request_without_fakeweb(request, body, &block) else uri = FakeWeb::Utility.strip_default_port_from_uri(uri) @@ -62,7 +62,7 @@ def connect_with_fakeweb FakeWeb::Utility.puts_warning_for_net_http_replacement_libs_if_needed @@alredy_checked_for_net_http_replacement_libs = true end - nil + FakeWeb.allow_all_connections? ? connect_without_fakeweb : nil end alias_method :connect_without_fakeweb, :connect alias_method :connect, :connect_with_fakeweb diff --git a/test/test_allow_all_connections.rb b/test/test_allow_all_connections.rb new file mode 100644 index 0000000..36326cd --- /dev/null +++ b/test/test_allow_all_connections.rb @@ -0,0 +1,35 @@ +require 'test_helper' + +class TestAllowAllConnections < Test::Unit::TestCase + def test_net_http_can_reconnect_on_keep_alive_timeout_when_allow_all_connections + FakeWeb.allow_net_connect = true + uri = URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss") + req = Net::HTTP::Get.new(uri) + Net::HTTP.new(uri.host, uri.port).start do |http| + http.keep_alive_timeout = 0 + http.request(req) + http.request(req) + end + end if RUBY_VERSION >= "2.0.0" + + def test_allow_all_connections_returns_true_without_registered_uris_or_passthroughs + FakeWeb.allow_net_connect = true + assert_equal true, FakeWeb.allow_all_connections? + end + + def test_allow_all_connections_returns_false_with_passthrough + FakeWeb.allow_net_connect = "http://example.com" + assert_equal false, FakeWeb.allow_all_connections? + end + + def test_allow_all_connections_returns_false_without_allow_net_connect + FakeWeb.allow_net_connect = false + assert_equal false, FakeWeb.allow_all_connections? + end + + def test_allow_all_connections_returns_false_registered_uris + FakeWeb.allow_net_connect = true + FakeWeb.register_uri(:get, "http://example.com", :status => [404, "Not Found"]) + assert_equal false, FakeWeb.allow_all_connections? + end +end