Skip to content

Commit

Permalink
Merge pull request #988 from rzane/socket-methods
Browse files Browse the repository at this point in the history
Make the StubSocket slightly more believable
  • Loading branch information
bblimke committed Aug 5, 2022
2 parents 36a7a69 + 786cd11 commit fc798ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/webmock/http_lib_adapters/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ def start_without_connect


def ensure_actual_connection
do_start if @socket.is_a?(StubSocket)
if @socket.is_a?(StubSocket)
@socket&.close
@socket = nil
do_start
end
end

alias_method :start_with_connect, :start
Expand Down Expand Up @@ -232,13 +236,16 @@ class StubSocket #:nodoc:
attr_accessor :read_timeout, :continue_timeout, :write_timeout

def initialize(*args)
@closed = false
end

def closed?
@closed ||= true
@closed
end

def close
@closed = true
nil
end

def readuntil(*args)
Expand All @@ -251,6 +258,9 @@ def io
class StubIO
def setsockopt(*args); end
def peer_cert; end
def peeraddr; ["AF_INET", 443, "127.0.0.1", "127.0.0.1"] end
def ssl_version; "TLSv1.3" end
def cipher; ["TLS_AES_128_GCM_SHA256", "TLSv1.3", 128, 128] end
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/acceptance/net_http/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ class TestMarshalingInWebMockNetHTTP
end
end

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
it "uses the StubSocket to provide IP address" do
Net::HTTP.start("http://example.com") do |http|
expect(http.ipaddr).to eq("127.0.0.1")
end
end
end

it "defines common socket methods" do
Net::HTTP.start("http://example.com") do |http|
socket = http.instance_variable_get(:@socket)
expect(socket.io.ssl_version).to eq("TLSv1.3")
expect(socket.io.cipher).to eq(["TLS_AES_128_GCM_SHA256", "TLSv1.3", 128, 128])
end
end

describe "connecting on Net::HTTP.start" do
before(:each) do
@http = Net::HTTP.new('www.google.com', 443)
Expand Down

0 comments on commit fc798ea

Please sign in to comment.