Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add option to disable_net_connect! to allow an exception for localhost

  • Loading branch information...
commit dc623d43f0e013cd05bddd3d60dcf57dc9202abc 1 parent 497e397
@macksmind macksmind authored
View
3  lib/webmock/config.rb
@@ -3,5 +3,6 @@ class Config
include Singleton
attr_accessor :allow_net_connect
+ attr_accessor :allow_localhost
end
-end
+end
View
4 lib/webmock/http_lib_adapters/httpclient.rb
@@ -19,7 +19,7 @@ def do_get_with_webmock(req, proxy, conn, stream = false, &block)
webmock_response = WebMock.response_for_request(request_signature)
response = build_httpclient_response(webmock_response, stream, &block)
conn.push(response)
- elsif WebMock.net_connect_allowed?
+ elsif WebMock.net_connect_allowed?(req.header.request_uri)
if stream
do_get_stream_without_webmock(req, proxy, conn, &block)
else
@@ -35,7 +35,7 @@ def do_request_async_with_webmock(method, uri, query, body, extheader)
req = create_request(method, uri, query, body, extheader)
request_signature = build_request_signature(req)
- if WebMock.registered_request?(request_signature) || WebMock.net_connect_allowed?
+ if WebMock.registered_request?(request_signature) || WebMock.net_connect_allowed?(uri)
do_request_async_without_webmock(method, uri, query, body, extheader)
else
message = "Real HTTP connections are disabled. Unregistered request: #{request_signature}"
View
2  lib/webmock/http_lib_adapters/net_http.rb
@@ -91,7 +91,7 @@ def request_with_webmock(request, body = nil, &block)
@socket = Net::HTTP.socket_type.new
webmock_response = WebMock.response_for_request(request_signature)
build_net_http_response(webmock_response, &block)
- elsif WebMock.net_connect_allowed?
+ elsif WebMock.net_connect_allowed?(uri)
connect_without_webmock
request_without_webmock(request, nil, &block)
else
View
2  lib/webmock/http_lib_adapters/patron.rb
@@ -12,7 +12,7 @@ def handle_request_with_webmock(req)
webmock_response = WebMock.response_for_request(request_signature)
handle_file_name(req, webmock_response)
build_patron_response(webmock_response)
- elsif WebMock.net_connect_allowed?
+ elsif WebMock.net_connect_allowed?(req.url)
handle_request_without_webmock(req)
else
message = "Real HTTP connections are disabled. Unregistered request: #{request_signature}"
View
10 lib/webmock/webmock.rb
@@ -28,12 +28,16 @@ def allow_net_connect!
Config.instance.allow_net_connect = true
end
- def disable_net_connect!
+ def disable_net_connect!(localhost = false)
Config.instance.allow_net_connect = false
+ Config.instance.allow_localhost = localhost
end
- def net_connect_allowed?
- Config.instance.allow_net_connect
+ def net_connect_allowed?(uri = nil)
+ if uri.class == String
+ uri = URI::parse(uri)
+ end
+ Config.instance.allow_net_connect || ( Config.instance.allow_localhost && uri.is_a?(URI) && uri.host == 'localhost' )
end
def registered_request?(request_signature)
View
4 spec/httpclient_spec_helper.rb
@@ -29,6 +29,10 @@ def client_timeout_exception_class
HTTPClient::TimeoutError
end
+ def connection_refused_exception_class
+ Errno::ECONNREFUSED
+ end
+
def default_client_request_headers(request_method = nil, has_body = false)
{'Content-Type'=>'application/x-www-form-urlencoded'} if request_method == 'POST' && has_body
end
View
4 spec/net_http_spec_helper.rb
@@ -37,6 +37,10 @@ def client_timeout_exception_class
Timeout::Error
end
+ def connection_refused_exception_class
+ Errno::ECONNREFUSED
+ end
+
# Sets several expectations that a real HTTP request makes it
# past WebMock to the socket layer. You can use this when you need to check
# that a request isn't handled by WebMock
View
4 spec/patron_spec_helper.rb
@@ -29,6 +29,10 @@ def client_timeout_exception_class
Patron::TimeoutError
end
+ def connection_refused_exception_class
+ Patron::ConnectionFailed
+ end
+
def setup_expectations_for_real_request(options = {})
#TODO
end
View
22 spec/webmock_spec.rb
@@ -60,6 +60,28 @@ class MyException < StandardError; end;
end
end
+ describe "is not allowed with exception for localhost" do
+ before(:each) do
+ WebMock.disable_net_connect!(true)
+ end
+
+ it "should return stubbed response if request was stubbed" do
+ stub_http_request(:get, "www.example.com").to_return(:body => "abc")
+ http_request(:get, "http://www.example.com/").body.should == "abc"
+ end
+
+ it "should raise exception if request was not stubbed" do
+ lambda {
+ http_request(:get, "http://www.example.com/")
+ }.should fail_with(client_specific_request_string("Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/"))
+ end
+
+ it "should allow a real request localhost" do
+ lambda {
+ http_request(:get, "http://localhost:12345/")
+ }.should raise_error(connection_refused_exception_class)
+ end
+ end
end
describe "when matching requests" do
Please sign in to comment.
Something went wrong with that request. Please try again.