HoneyryderChuck fbf9b12a0b bugfix: ensuring :io option as hash works
while adding tests, found out that io as hash of authority => io wasn't
working due to missing uri extensions. Made sure the same works for unix
sockets.
2021-03-08 17:04:36 +00:00

52 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module Requests
module IO
unless defined?(HTTPX::TLS)
using HTTPX::URIExtensions
def test_http_io
io = origin_io
uri = build_uri("/get")
response = HTTPX.get(uri, io: io)
verify_status(response, 200)
verify_body_length(response)
assert !io.closed?, "io should have been left open"
ensure
io.close if io
end
def test_http_io_hash
io = origin_io
uri = build_uri("/get")
response = HTTPX.get(uri, io: { URI(origin).authority => io })
verify_status(response, 200)
verify_body_length(response)
assert !io.closed?, "io should have been left open"
ensure
io.close if io
end
end
end
private
def origin_io
uri = URI(origin)
case uri.scheme
when "http"
TCPSocket.new(uri.host, uri.port)
when "https"
ctx = OpenSSL::SSL::SSLContext.new
ctx.alpn_protocols = %w[h2 http/1.1] if ctx.respond_to?(:alpn_protocols)
sock = OpenSSL::SSL::SSLSocket.new(TCPSocket.new(uri.host, uri.port), ctx)
sock.hostname = uri.host
sock.sync_close = true
sock.connect
sock
else
raise "#{uri.scheme}: unsupported scheme"
end
end
end