added tests around the tcp connect errors

This commit is contained in:
HoneyryderChuck 2021-03-09 19:29:17 +00:00
parent 23310e5ebf
commit 00faafddc9
7 changed files with 46 additions and 22 deletions

View File

@ -13,7 +13,7 @@ class HTTPTest < Minitest::Test
include Headers
include ResponseBody
include IO
include Errors
include Errors if RUBY_ENGINE == "ruby"
include AltSvc if ENV.key?("HTTPBIN_ALTSVC_HOST")
include Plugins::Proxy unless ENV.key?("HTTPX_NO_PROXY")

View File

@ -11,7 +11,7 @@ class HTTPSTest < Minitest::Test
include Headers
include ResponseBody
include IO
include Errors
include Errors if RUBY_ENGINE == "ruby"
include Resolvers if ENV.key?("HTTPX_RESOLVER_URI")
# TODO: uncomment as soon as nghttpx supports altsvc for HTTP/2
# include AltSvc if ENV.key?("HTTPBIN_ALTSVC_HOST")

View File

@ -57,11 +57,16 @@ class SessionTest < Minitest::Test
end
def test_session_timeout_connect_timeout
uri = build_uri("/", origin("127.0.0.1:#{CONNECT_TIMEOUT_PORT}"))
session = HTTPX.with_timeout(connect_timeout: 0.5, operation_timeout: 30, total_timeout: 2)
response = session.get(uri)
verify_error_response(response)
# verify_error_response(response, HTTPX::ConnectTimeoutError)
server = TCPServer.new("127.0.0.1", CONNECT_TIMEOUT_PORT)
begin
uri = build_uri("/", origin("127.0.0.1:#{CONNECT_TIMEOUT_PORT}"))
session = HTTPX.with_timeout(connect_timeout: 0.5, operation_timeout: 30, total_timeout: 2)
response = session.get(uri)
verify_error_response(response)
verify_error_response(response, HTTPX::ConnectTimeoutError)
ensure
server.close
end
end
# def test_http_timeouts_operation_timeout

View File

@ -25,8 +25,17 @@ fi
# use port 9090 to test connection timeouts
CONNECT_TIMEOUT_PORT=9090
iptables -A OUTPUT -p tcp -m tcp --tcp-flags SYN SYN --sport $CONNECT_TIMEOUT_PORT -j DROP
export CONNECT_TIMEOUT_PORT=$CONNECT_TIMEOUT_PORT
ETIMEDOUT_PORT=9091
iptables -A INPUT -p tcp --sport $ETIMEDOUT_PORT -j DROP
export ETIMEDOUT_PORT=$ETIMEDOUT_PORT
# for errno EHOSTUNREACH error
EHOSTUNREACH_HOST=192.168.2.1
ip route add unreachable $EHOSTUNREACH_HOST
export EHOSTUNREACH_HOST=$EHOSTUNREACH_HOST
export PATH=$GEM_HOME/bin:$BUNDLE_PATH/gems/bin:$PATH
mkdir -p "$GEM_HOME" && chmod 777 "$GEM_HOME"
gem install bundler -v="1.17.3" --no-doc --conservative

View File

@ -6,7 +6,7 @@ module MinitestExtensions
TestTimeout = Class.new(Timeout::Error)
def run(*)
::Timeout.timeout(60, TestTimeout) { super }
::Timeout.timeout(60 * 5, TestTimeout) { super }
end
end

View File

@ -3,13 +3,33 @@
module Requests
module Errors
def test_errors_connection_refused
skip if RUBY_ENGINE == "jruby"
unavailable_host = URI(origin("localhost"))
unavailable_host.port = next_available_port
response = HTTPX.get(unavailable_host.to_s)
verify_error_response(response, /Connection refused| not available/)
end
def test_errors_host_unreachable
uri = URI(origin("localhost")).to_s
return unless uri.start_with?("http://")
response = HTTPX.get(uri, addresses: [EHOSTUNREACH_HOST] * 2)
verify_error_response(response, /Host is unreachable/)
end
def test_errors_host_etimedout
uri = URI(origin("etimedout:#{ETIMEDOUT_PORT}")).to_s
return unless uri.start_with?("http://")
server = TCPServer.new("127.0.0.1", ETIMEDOUT_PORT)
begin
response = HTTPX.get(uri, addresses: %w[127.0.0.1] * 2)
verify_error_response(response, /Operation timed out/)
ensure
server.close
end
end
private
def next_available_port

View File

@ -32,15 +32,5 @@ OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE.add_file(ENV["SSL_CERT_FILE"]) if R
# 9090 drops SYN packets for connect timeout tests, make sure there's a server binding there.
CONNECT_TIMEOUT_PORT = ENV.fetch("CONNECT_TIMEOUT_PORT", 9090).to_i
server = TCPServer.new("127.0.0.1", CONNECT_TIMEOUT_PORT)
Thread.start do
begin
sock = server.accept
sock.close
rescue StandardError => e
warn e.message
warn e.backtrace
end
end
ETIMEDOUT_PORT = ENV.fetch("ETIMEDOUT_PORT", 9091).to_i
EHOSTUNREACH_HOST = ENV.fetch("EHOSTUNREACH_HOST", "192.168.2.1")