verifyig error responses annd deltas using internal methods

This commit is contained in:
HoneyryderChuck 2021-01-05 18:57:39 +00:00
parent a6176ec01f
commit fab44b5993
9 changed files with 51 additions and 50 deletions

View File

@ -103,9 +103,7 @@ class HTTPSTest < Minitest::Test
uri = build_uri("/get")
HTTPX.plugin(SessionWithPool).plugin(SessionWithFrameDelay).wrap do |http|
response = http.get(uri)
assert response.is_a?(HTTPX::ErrorResponse), "expected to fail for settings timeout"
assert response.status =~ /settings_timeout/,
"connection should have terminated due to HTTP/2 settings timeout"
verify_error_response(response, /settings_timeout/)
end
end
end

View File

@ -53,26 +53,22 @@ class SessionTest < Minitest::Test
uri = build_uri("/delay/3")
session = HTTPX.with_timeout(total_timeout: 2)
response = session.get(uri)
assert response.is_a?(HTTPX::ErrorResponse), "response should have failed"
assert response.error.is_a?(HTTPX::TotalTimeoutError), "response should have timed out (instead, we got \"#{response.status}\")"
verify_error_response(response, HTTPX::TotalTimeoutError)
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)
assert response.is_a?(HTTPX::ErrorResponse), "response should have failed (#{response.class})"
# assert response.error.is_a?(HTTPX::ConnectTimeoutError),
# "response should have failed on connection (#{response.error.class}: #{response.error})"
verify_error_response(response)
# verify_error_response(response, HTTPX::ConnectTimeoutError)
end
# def test_http_timeouts_operation_timeout
# uri = build_uri("/delay/2")
# session = HTTPX.with_timeout(operation_timeout: 1)
# response = session.get(uri)
# assert response.is_a?(HTTPX::ErrorResponse), "response should have failed"
# assert response.error =~ /timed out while waiting/, "response should have timed out"
# verify_error_response(response, /timed out while waiting/)
# end
def test_session_timeout_keep_alive_timeout

View File

@ -31,4 +31,28 @@ module ResponseHelpers
len = response.body.to_s.bytesize
assert len == expect, "length assertion failed: #{len} (expected: #{expect})"
end
def verify_execution_delta(expected, actual, delta = 0)
assert_in_delta expected, actual, delta, "expected to have executed in #{expected} secs (actual: #{actual} secs)"
end
def verify_error_response(response, expectation = nil)
assert response.is_a?(HTTPX::ErrorResponse), "expected an error response (instead got: #{response.inspect})"
return unless expectation
case expectation
when Regexp
assert response.error.message =~ expectation,
"expected to match \/#{expectation}\/ in \"#{response.error.message}\""
when String
assert response.error.message.include?(expectation),
"expected \"#{response.error.message}\" to include \"#{expectation}\""
when Class
assert response.error.is_a?(expectation),
"expected #{response.error} to be a #{expectation}"
else
raise "unexpected expectation (#{expectation})"
end
end
end

View File

@ -7,9 +7,7 @@ module Requests
unavailable_host = URI(origin("localhost"))
unavailable_host.port = next_available_port
response = HTTPX.get(unavailable_host.to_s)
assert response.is_a?(HTTPX::ErrorResponse), "response should contain errors"
assert response.status =~ /Connection refused| not available/,
"connection should have been refused (#{response.error.class}: #{response.status})"
verify_error_response(response, /Connection refused| not available/)
end
private

View File

@ -49,7 +49,8 @@ module Requests
# I test for greater than 2 due to the concurrent test, which affect the times.
# However, most important is, it takes certainly more than 2 seconds.
assert (date2 - date1).abs >= 2, "time between requests took < 2 seconds"
time_it_took = (date2 - date1).abs
assert time_it_took >= 2, "time between requests took < 2 secs (actual: #{time_it_took} secs)"
end
def test_multiple_get_max_requests

View File

@ -54,7 +54,7 @@ module Requests
session = HTTPX.plugin(:follow_redirects).max_redirects(1)
response = session.get(insecure_redirect_uri)
assert response.is_a?(HTTPX::ErrorResponse), "request should not follow insecure URLs"
verify_error_response(response)
insecure_session = HTTPX.plugin(:follow_redirects)
.max_redirects(1)

View File

@ -68,8 +68,7 @@ module Requests
session = HTTPX.plugin(:proxy).with_proxy(uri: [proxy])
uri = build_uri("/get")
response = session.get(uri)
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::Socks4Error), "should be a socks 4 error"
verify_error_response(response, HTTPX::Socks4Error)
end
def test_plugin_socks4a_proxy
@ -95,8 +94,7 @@ module Requests
session = HTTPX.plugin(:proxy).with_proxy(uri: [proxy])
uri = build_uri("/get")
response = session.get(uri)
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::Socks5Error), "should be a socks 5 error"
verify_error_response(response, HTTPX::Socks5Error)
end
def test_plugin_ssh_proxy

View File

@ -6,14 +6,14 @@ module Requests
def test_plugin_retries
no_retries_session = HTTPX.plugin(RequestInspector).with_timeout(total_timeout: 3)
no_retries_response = no_retries_session.get(build_uri("/delay/10"))
assert verify_error_response(no_retries_response)
verify_error_response(no_retries_response)
assert no_retries_session.calls.zero?, "expect request to be built 1 times (was #{no_retries_session.calls})"
retries_session = HTTPX
.plugin(RequestInspector)
.plugin(:retries)
.with_timeout(total_timeout: 3)
retries_response = retries_session.get(build_uri("/delay/10"))
assert verify_error_response(retries_response)
verify_error_response(retries_response)
assert retries_session.calls == 3, "expect request to be built 3 times (was #{retries_session.calls})"
end
@ -43,7 +43,7 @@ module Requests
.max_retries(2)
retries_response = retries_session.get(build_uri("/delay/10"))
assert verify_error_response(retries_response)
verify_error_response(retries_response)
# we're comparing against max-retries + 1, because the calls increment will happen
# also in the last call, where the request is not going to be retried.
assert retries_session.calls == 2, "expect request to be built 2 times (was #{retries_session.calls})"
@ -61,7 +61,7 @@ module Requests
.max_retries(2)
retries_response = retries_session.get(build_uri("/delay/10"))
assert verify_error_response(retries_response)
verify_error_response(retries_response)
assert retries_session.calls.zero?, "expect request not to be retried (it was, #{retries_session.calls} times)"
end
@ -76,8 +76,8 @@ module Requests
after_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
total_time = after_time - before_time
assert verify_error_response(retries_response)
assert_in_delta 3 + 2 + 3, total_time, 1, "request didn't take as expected to retry (#{total_time} secs)"
verify_error_response(retries_response)
verify_execution_delta(3 + 2 + 3, total_time, 1)
end
def test_plugin_retries_retry_after_callable
@ -93,14 +93,8 @@ module Requests
after_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
total_time = after_time - before_time
assert verify_error_response(retries_response)
assert_in_delta 3 + 2 + 3 + 4 + 3, total_time, 1, "request didn't take as expected to retry (#{total_time} secs)"
end
private
def verify_error_response(response)
assert response.is_a?(HTTPX::ErrorResponse), "expected an error response, instead got #{response.inspect}"
verify_error_response(retries_response)
verify_execution_delta(3 + 2 + 3 + 4 + 3, total_time, 1)
end
end
end

View File

@ -12,8 +12,7 @@ module Requests
session = HTTPX.plugin(SessionWithPool)
unknown_uri = "http://www.sfjewjfwigiewpgwwg-native-#{i}.com"
response = session.get(unknown_uri, resolver_class: resolver, resolver_options: options)
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::ResolveError), "should be a resolving error"
verify_error_response(response, HTTPX::ResolveError)
end
end
@ -40,8 +39,7 @@ module Requests
session = HTTPX.plugin(SessionWithPool)
uri = build_uri("/get")
response = session.head(uri, resolver_class: resolver, resolver_options: options.merge(uri: "https://unexisting-doh/dns-query"))
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::ResolveError), "should be a resolving error"
verify_error_response(response, HTTPX::ResolveError)
end
define_method :"test_resolver_#{resolver}_server_error" do
@ -53,8 +51,7 @@ module Requests
end
end
response = session.head(uri, resolver_class: resolver_class, resolver_options: options)
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::ResolveError), "should be a resolving error"
verify_error_response(response, HTTPX::ResolveError)
end
define_method :"test_resolver_#{resolver}_decoding_error" do
@ -66,8 +63,7 @@ module Requests
end
end
response = session.head(uri, resolver_class: resolver_class, resolver_options: options.merge(record_types: %w[]))
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::ResolveError), "should be a resolving error"
verify_error_response(response, HTTPX::ResolveError)
end
when :native
@ -90,8 +86,7 @@ module Requests
after_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
total_time = after_time - before_time
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::ResolveTimeoutError), "should be a resolving timeout error"
verify_error_response(response, HTTPX::ResolveTimeoutError)
assert_in_delta 2 + 1, total_time, 6, "request didn't take as expected to retry dns queries (#{total_time} secs)"
end
@ -101,8 +96,7 @@ module Requests
uri = build_uri("/get")
response = session.head(uri, resolver_class: resolver, resolver_options: options.merge(nameserver: nil))
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::ResolveError), "should be a resolving error"
verify_error_response(response, HTTPX::ResolveError)
end
# this test mocks a DNS server invalid messages back
@ -115,8 +109,7 @@ module Requests
end
end
response = session.head(uri, resolver_class: resolver_class, resolver_options: options.merge(record_types: %w[]))
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::NativeResolveError), "should be a resolving error"
verify_error_response(response, HTTPX::NativeResolveError)
end
# this test mocks a DNS server breaking the socket with Errno::EHOSTUNREACH
@ -135,8 +128,7 @@ module Requests
end
end
response = session.head(uri, resolver_class: resolver_class, resolver_options: options.merge(nameserver: %w[127.0.0.1] * 3))
assert response.is_a?(HTTPX::ErrorResponse), "should be a response error"
assert response.error.is_a?(HTTPX::ResolveError), "should be a resolving error"
verify_error_response(response, HTTPX::ResolveError)
assert resolver_class.attempts == 3, "should have attempted to use all 3 nameservers"
end
end