Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reconnect on keep alive timeout when allowing all connections. #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/fake_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>FakeWeb.allow_net_connect =
# false</tt> and subsequently try to make a request to a URI you haven't
# stubbed.
Expand Down
4 changes: 2 additions & 2 deletions lib/fake_web/ext/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/test_allow_all_connections.rb
Original file line number Diff line number Diff line change
@@ -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