Skip to content

Commit

Permalink
Merge branch 'rzane-net-http-start-allowlist'
Browse files Browse the repository at this point in the history
  • Loading branch information
bblimke committed Aug 2, 2022
2 parents dc6ff71 + 15cb11c commit 179d807
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/webmock/http_lib_adapters/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def ensure_actual_connection
alias_method :start_with_connect, :start

def start(&block)
if WebMock::Config.instance.net_http_connect_on_start
uri = Addressable::URI.parse(WebMock::NetHTTPUtility.get_uri(self))

if WebMock.net_http_connect_on_start?(uri)
super(&block)
else
start_without_connect(&block)
Expand Down Expand Up @@ -332,7 +334,7 @@ def self.request_signature_from_request(net_http, request, body = nil)
WebMock::RequestSignature.new(method, uri, body: request.body, headers: headers)
end

def self.get_uri(net_http, path)
def self.get_uri(net_http, path = nil)
protocol = net_http.use_ssl? ? "https" : "http"

hostname = net_http.address
Expand Down
10 changes: 10 additions & 0 deletions lib/webmock/webmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ def self.net_connect_allowed?(uri = nil)
Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri) )
end

def self.net_http_connect_on_start?(uri)
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)
case allowed
when Array
Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/net_http/net_http_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
sockets << @http.instance_variable_get(:@socket)
@http.finish

if WebMock::Config.instance.net_http_connect_on_start
if WebMock.net_http_connect_on_start?(Addressable::URI.parse("http://example.com/"))
expect(sockets.length).to eq(1)
expect(sockets.to_a[0]).to be_a(Net::BufferedIO)
else
Expand Down
27 changes: 27 additions & 0 deletions spec/acceptance/net_http/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ class TestMarshalingInWebMockNetHTTP
}
end

it "should connect to the server on start when allowlisted", net_connect: true do
WebMock.disable_net_connect!(allow: "www.google.com", net_http_connect_on_start: "www.google.com")
@http.start {|conn|
cert = OpenSSL::X509::Certificate.new conn.peer_cert
expect(cert).to be_a(OpenSSL::X509::Certificate)
}
end

it "should not connect to the server on start when not allowlisted", net_connect: true do
WebMock.disable_net_connect!(allow: "www.google.com", net_http_connect_on_start: "www.yahoo.com")
@http.start {|conn|
expect(conn.peer_cert).to be_nil
}
end

it "should connect to the server if the URI matches an regex", net_connect: true do
WebMock.disable_net_connect!(allow: /google.com/)
Net::HTTP.get('www.google.com','/')
Expand Down Expand Up @@ -282,6 +297,13 @@ class TestMarshalingInWebMockNetHTTP
it_should_behave_like "Net::HTTP"
end

describe "when net_http_connect_on_start is a specific host" do
before(:each) do
WebMock.allow_net_connect!(net_http_connect_on_start: "localhost")
end
it_should_behave_like "Net::HTTP"
end

describe 'after_request callback support', net_connect: true do
let(:expected_body_regex) { /hello world/ }

Expand Down Expand Up @@ -365,5 +387,10 @@ def perform_get_with_returning_block
path = '/example.jpg'
expect(WebMock::NetHTTPUtility.get_uri(net_http, path)).to eq('http://www.example.com:80/example.jpg')
end

it "does not require a path" do
net_http = Net::HTTP.new('www.example.com', 80)
expect(WebMock::NetHTTPUtility.get_uri(net_http)).to eq('http://www.example.com:80')
end
end
end
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 179d807

Please sign in to comment.