Permalink
Browse files

Allow Webmock.disable_net_connect! to accept a RegEx as :allow option

  • Loading branch information...
1 parent ad9ea7f commit 7a38e8adf738dde257df8d1b4efd8b86bc51c74f @thenoseman thenoseman committed Feb 3, 2012
Showing with 16 additions and 2 deletions.
  1. +5 −1 README.md
  2. +4 −1 lib/webmock/webmock.rb
  3. +7 −0 spec/acceptance/net_http/net_http_spec.rb
View
@@ -319,7 +319,7 @@ You can also use WebMock outside a test framework:
Net::HTTP.get('localhost:9887', '/') # ===> Allowed. Perhaps to Selenium?
-### External requests can be disabled while allowing any hostname or port
+### External requests can be disabled while allowing any hostname or port or parts thereof
WebMock.disable_net_connect!(:allow => "www.example.org:8080")
@@ -329,6 +329,10 @@ You can also use WebMock outside a test framework:
RestClient.get('www.example.org:8080', '/') # ===> Allowed
+ WebMock.disable_net_connect!(:allow => /ample.org/)
+
+ RestClient.get('www.example.org', '/') # ===> Allowed
+
## Connecting on Net::HTTP.start
HTTP protocol has 3 steps: connect, request and response (or 4 with close). Most Ruby HTTP client libraries
View
@@ -59,7 +59,10 @@ def self.net_connect_allowed?(uri = nil)
end
Config.instance.allow_net_connect ||
(Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri)) ||
- Config.instance.allow && (Config.instance.allow.include?(uri.host) || Config.instance.allow.include?("#{uri.host}:#{uri.port}"))
+ Config.instance.allow && (
+ (Config.instance.allow.kind_of?(Regexp) && uri.to_s =~ Config.instance.allow) ||
+ (Config.instance.allow.respond_to?(:include?) &&
+ Config.instance.allow.include?(uri.host) || Config.instance.allow.include?("#{uri.host}:#{uri.port}")))
end
def self.reset!
@@ -184,7 +184,14 @@ class TestMarshalingInWebMockNetHTTP
cert.should be_a(OpenSSL::X509::Certificate)
}
end
+
+ it "should connect to the server if the URI matches an regex", :net_connect => true do
+ WebMock.disable_net_connect!(:allow => /google.com/)
+ response = Net::HTTP.get('www.google.com','/')
+ end
+
end
+
end
describe "when net_http_connect_on_start is true" do

0 comments on commit 7a38e8a

Please sign in to comment.