Skip to content

Commit

Permalink
Reuse persistent connection
Browse files Browse the repository at this point in the history
When using a persistent connection, the point is to reuse the underlying
socket. Prior to this commit, the socket would be thrown away before
each request unless `net_http_connect_on_start` is `true`.

This commit will preserve the previously started socket if the socket
represents an actual connection.
  • Loading branch information
rzane committed May 24, 2022
1 parent 3d3bdc2 commit eeb1392
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
16 changes: 4 additions & 12 deletions lib/webmock/http_lib_adapters/net_http.rb
Expand Up @@ -98,12 +98,8 @@ def request(request, body = nil, &block)
after_request.call(response)
}
if started?
if WebMock::Config.instance.net_http_connect_on_start
super_with_after_request.call
else
start_with_connect_without_finish
super_with_after_request.call
end
ensure_actual_connection
super_with_after_request.call
else
start_with_connect {
super_with_after_request.call
Expand Down Expand Up @@ -131,12 +127,8 @@ def start_without_connect
end


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

alias_method :start_with_connect, :start
Expand Down
5 changes: 4 additions & 1 deletion spec/acceptance/net_http/net_http_shared.rb
Expand Up @@ -68,8 +68,11 @@

if WebMock::Config.instance.net_http_connect_on_start
expect(sockets.length).to eq(1)
expect(sockets.to_a[0]).to be_a(Net::BufferedIO)
else
expect(sockets.length).to eq(4)
expect(sockets.length).to eq(2)
expect(sockets.to_a[0]).to be_a(StubSocket)
expect(sockets.to_a[1]).to be_a(Net::BufferedIO)
end

expect(sockets.all?(&:closed?)).to be(true)
Expand Down

0 comments on commit eeb1392

Please sign in to comment.