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..8a1e7e8 100644 --- a/lib/fake_web/ext/net_http.rb +++ b/lib/fake_web/ext/net_http.rb @@ -45,8 +45,15 @@ 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 - request_without_fakeweb(request, body, &block) + unless FakeWeb.allow_all_connections? + connect_without_fakeweb + end + begin + @in_request_without_fakeweb = true + request_without_fakeweb(request, body, &block) + ensure + @in_request_without_fakeweb = nil + end else uri = FakeWeb::Utility.strip_default_port_from_uri(uri) raise FakeWeb::NetConnectNotAllowedError, @@ -62,7 +69,9 @@ 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 + if FakeWeb.allow_all_connections? || @in_request_without_fakeweb + connect_without_fakeweb + end 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..50c8c8b --- /dev/null +++ b/test/test_allow_all_connections.rb @@ -0,0 +1,46 @@ +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_net_http_can_reconnect_on_keep_alive_timeout_for_passthrough_uris + uri = URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss") + FakeWeb.allow_net_connect = uri + 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