Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

multiple regex in allow #263

Merged
merged 2 commits into from

2 participants

@bastien

At the moment you can either specify a regex, a string or an array of strings in the allow parameter of WebMock.disable_net_connect!.

I added the possibility to also have an array of regex.

@bblimke
Owner

Nice improvement. Thanks!

@bblimke bblimke merged commit d85e2cf into bblimke:master
@bastien

My pleasure :smile:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 12 deletions.
  1. +14 −12 lib/webmock/webmock.rb
  2. +5 −0 spec/acceptance/net_http/net_http_spec.rb
View
26 lib/webmock/webmock.rb
@@ -59,18 +59,20 @@ 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.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}")
- )
- )
- )
+ ( Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri) ||
+ Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri) )
+ end
+
+ def self.net_connect_explicit_allowed?(allowed, uri=nil)
+ case allowed
+ when Array
+ allowed.any? { |allowed_item| net_connect_explicit_allowed?(allowed_item, uri) }
+ when Regexp
+ uri.to_s =~ allowed
+ when String
+ allowed == uri.host ||
+ allowed == "#{uri.host}:#{uri.port}"
+ end
end
def self.reset!
View
5 spec/acceptance/net_http/net_http_spec.rb
@@ -203,6 +203,11 @@ class TestMarshalingInWebMockNetHTTP
response = Net::HTTP.get('www.google.com','/')
end
+ it "should connect to the server if the URI matches any regex the array", :net_connect => true do
+ WebMock.disable_net_connect!(:allow => [/google.com/, /yahoo.com/])
+ response = Net::HTTP.get('www.google.com','/')
+ end
+
end
end
Something went wrong with that request. Please try again.