Skip to content

Commit a954011

Browse files
committed
Refactor test server to use TestServer struct
1 parent 7894f8d commit a954011

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

spec/lib/twingly/http_spec.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -537,23 +537,19 @@ class CustomError < StandardError; end
537537

538538
RSpec.shared_examples "verifies proxy functionality" do
539539
context "when a proxy is provided" do
540-
let(:proxy_pid_and_url) { HttpTestServer.spawn("proxy_server") }
541-
let(:proxy_pid) { proxy_pid_and_url[0] }
542-
let(:proxy_url) { proxy_pid_and_url[1] }
543-
let(:target_pid_and_url) { HttpTestServer.spawn("echoed_headers_in_body") }
544-
let(:target_pid) { target_pid_and_url[0] }
545-
let(:target_url) { target_pid_and_url[1] }
540+
let(:proxy_server) { HttpTestServer.spawn("proxy_server") }
541+
let(:target_server) { HttpTestServer.spawn("echoed_headers_in_body") }
546542
let(:client) do
547543
described_class.new(
548544
base_user_agent: base_user_agent,
549-
proxy: proxy_url
545+
proxy: proxy_server.url
550546
)
551547
end
552-
let(:url) { target_url }
548+
let(:url) { target_server.url }
553549

554550
after do
555-
HttpTestServer.stop(proxy_pid)
556-
HttpTestServer.stop(target_pid)
551+
HttpTestServer.stop(proxy_server.pid)
552+
HttpTestServer.stop(target_server.pid)
557553
end
558554

559555
it "routes requests through the proxy", vcr: false do
@@ -633,6 +629,7 @@ class CustomError < StandardError; end
633629
describe "#get", vcr: Fixture.example_org do
634630
include_examples "common HTTP behaviour for", :get, "example.org"
635631
include_examples "verifies proxy functionality"
632+
636633
let(:request_response) do
637634
client.get(url)
638635
end

spec/spec_help/http_test_server.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
module HttpTestServer
66
module_function
77

8+
TestServer = Struct.new(:pid, :url)
9+
810
def spawn(server_name, env: {}) # rubocop:disable Metrics/MethodLength
911
ip_address = PortProber.localhost
1012
port = PortProber.random(ip_address)
@@ -23,7 +25,7 @@ def spawn(server_name, env: {}) # rubocop:disable Metrics/MethodLength
2325
sleep 0.05 until started?(pid) && PortProber.port_open?(ip_address, port)
2426
end
2527

26-
[pid, url]
28+
TestServer.new(pid, url)
2729
end
2830

2931
def stop(pid)

0 commit comments

Comments
 (0)