From 480cbbb9f43f4bee9b76e608654a3ca4d51da432 Mon Sep 17 00:00:00 2001 From: Sarun Rattanasiri Date: Wed, 13 Nov 2024 21:01:39 +0700 Subject: [PATCH] Alias default_socket_class to Socket.tcp --- lib/http/network/tcp_socket.rb | 19 +++++++++++++++++++ lib/http/options.rb | 4 ++-- spec/lib/http/client_spec.rb | 4 ++-- spec/support/http_handling_shared.rb | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 lib/http/network/tcp_socket.rb diff --git a/lib/http/network/tcp_socket.rb b/lib/http/network/tcp_socket.rb new file mode 100644 index 00000000..ee654868 --- /dev/null +++ b/lib/http/network/tcp_socket.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "socket" + +module HTTP + module Network + class TCPSocket + class << self + def new(...) + ::Socket.tcp(...) + end + + def open(...) + new(...) + end + end + end + end +end diff --git a/lib/http/options.rb b/lib/http/options.rb index 6f99e3d7..e952d18a 100644 --- a/lib/http/options.rb +++ b/lib/http/options.rb @@ -2,12 +2,12 @@ require "http/headers" require "openssl" -require "socket" +require "http/network/tcp_socket" require "http/uri" module HTTP class Options # rubocop:disable Metrics/ClassLength - @default_socket_class = TCPSocket + @default_socket_class = Network::TCPSocket @default_ssl_socket_class = OpenSSL::SSL::SSLSocket @default_timeout_class = HTTP::Timeout::Null @available_features = {} diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index dc86a2fd..b7a25c83 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -385,7 +385,7 @@ def on_error(request, error) end it "is given a chance to handle a connection timeout error" do - allow(TCPSocket).to receive(:open) { sleep 1 } + allow(HTTP::Network::TCPSocket).to receive(:open) { sleep 1 } sleep_url = "#{dummy.endpoint}/sleep" feature_instance = feature_class.new @@ -570,7 +570,7 @@ def wrap_response(res) allow(socket_spy).to receive(:readpartial) { chunks.shift || :eof } allow(socket_spy).to receive(:write) { chunks[0].length } - allow(TCPSocket).to receive(:open) { socket_spy } + allow(HTTP::Network::TCPSocket).to receive(:open) { socket_spy } end it "properly reads body" do diff --git a/spec/support/http_handling_shared.rb b/spec/support/http_handling_shared.rb index 2f28179d..d0db4aa4 100644 --- a/spec/support/http_handling_shared.rb +++ b/spec/support/http_handling_shared.rb @@ -73,7 +73,7 @@ let(:response) { client.get(server.endpoint).body.to_s } it "errors if connecting takes too long" do - expect(TCPSocket).to receive(:open) do + expect(HTTP::Network::TCPSocket).to receive(:open) do sleep 1.25 end