From eeb13929f806be9987c055b200c7712b9b476eb1 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Mon, 23 May 2022 21:52:06 -0400 Subject: [PATCH] Reuse persistent connection 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. --- lib/webmock/http_lib_adapters/net_http.rb | 16 ++++------------ spec/acceptance/net_http/net_http_shared.rb | 5 ++++- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/webmock/http_lib_adapters/net_http.rb b/lib/webmock/http_lib_adapters/net_http.rb index 7e779502..20fdee92 100644 --- a/lib/webmock/http_lib_adapters/net_http.rb +++ b/lib/webmock/http_lib_adapters/net_http.rb @@ -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 @@ -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 diff --git a/spec/acceptance/net_http/net_http_shared.rb b/spec/acceptance/net_http/net_http_shared.rb index 9d540418..ea261b71 100644 --- a/spec/acceptance/net_http/net_http_shared.rb +++ b/spec/acceptance/net_http/net_http_shared.rb @@ -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)