Skip to content

Commit

Permalink
Allow :net_http_connect_on_start to specify which hosts should be c…
Browse files Browse the repository at this point in the history
…onnected to
  • Loading branch information
rzane committed May 22, 2022
1 parent b1b5a2e commit de69d99
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/webmock/webmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ def self.net_connect_allowed?(uri = nil)
end

def self.net_http_connect_on_start?(uri)
Config.instance.net_http_connect_on_start
allowed = Config.instance.net_http_connect_on_start || false

if [true, false].include?(allowed)
allowed
else
net_connect_explicit_allowed?(allowed, uri)
end
end

def self.net_connect_explicit_allowed?(allowed, uri=nil)
Expand Down
54 changes: 54 additions & 0 deletions spec/unit/webmock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,58 @@
end
end
end

describe ".net_http_connect_on_start?" do
let(:uri) { Addressable::URI.parse("http://example.org:5432") }

it "will not connect on start when false" do
WebMock.disable_net_connect!
expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
end

it "will connect on start when true" do
WebMock.disable_net_connect!(net_http_connect_on_start: true)
expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
end

it "will connect on start when regexp matches" do
WebMock.disable_net_connect!(net_http_connect_on_start: /example/)
expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
end

it "will not connect on start when regexp does not match" do
WebMock.disable_net_connect!(net_http_connect_on_start: /nope/)
expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
end

it "will connect on start when host matches" do
WebMock.disable_net_connect!(net_http_connect_on_start: "example.org")
expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
end

it "will not connect on start when host does not match" do
WebMock.disable_net_connect!(net_http_connect_on_start: "localhost")
expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
end

it "will connect on start when host + port matches" do
WebMock.disable_net_connect!(net_http_connect_on_start: "example.org:5432")
expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
end

it "will not connect on start when host + port does not match" do
WebMock.disable_net_connect!(net_http_connect_on_start: "example.org:80")
expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
end

it "will connect on start when scheme + host + port matches" do
WebMock.disable_net_connect!(net_http_connect_on_start: "http://example.org:5432")
expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
end

it "will not connect on start when scheme + host + port does not match" do
WebMock.disable_net_connect!(net_http_connect_on_start: "https://example.org:5432")
expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
end
end
end

0 comments on commit de69d99

Please sign in to comment.