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.
This commit is contained in:
HoneyryderChuck 2021-03-08 16:54:22 +00:00
parent 889cc80820
commit fbf9b12a0b
5 changed files with 71 additions and 35 deletions

View File

@ -7,6 +7,8 @@ module HTTPX
class TCP
include Loggable
using URIExtensions
attr_reader :ip, :port, :addresses, :state, :interests
alias_method :host, :ip

View File

@ -6,22 +6,22 @@ module HTTPX
class UNIX < TCP
extend Forwardable
def_delegator :@uri, :port, :scheme
using URIExtensions
attr_reader :path
alias_method :host, :path
def initialize(uri, addresses, options)
@uri = uri
def initialize(origin, addresses, options)
@addresses = addresses
@hostname = origin.host
@state = :idle
@options = Options.new(options)
@fallback_protocol = @options.fallback_protocol
if @options.io
@io = case @options.io
when Hash
@options.io[@path]
@options.io[origin.authority]
else
@options.io
end
@ -32,8 +32,10 @@ module HTTPX
@state = :connected
else
if @options.transport_options
# :nocov:
warn ":#{__method__} is deprecated, use :addresses instead"
@path = @options.transport_options[:path]
# :nocov:
else
@path = addresses.first
end

View File

@ -87,9 +87,10 @@ module HTTPX
def with_request_class: (singleton(Request)) -> instance
# io
attr_reader io: _ToIO?
def io=: (_ToIO) -> void
def with_io: (_ToIO) -> instance
type io_option = _ToIO | Hash[String, _ToIO]
attr_reader io: io_option?
def io=: (io_option) -> void
def with_io: (io_option) -> instance
# fallback_protocol
attr_reader fallback_protocol: String?

View File

@ -6,29 +6,45 @@ require_relative "../test_helper"
class UnixTest < Minitest::Test
include HTTPHelpers
def test_unix_session
skip if RUBY_ENGINE == "jruby"
on_unix_server(__method__) do |path|
HTTPX.with(transport: "unix", addresses: [path]).wrap do |http|
http.get("http://unix.com/ping", "http://unix.com/ping").each do |response|
verify_status(response, 200)
assert response.to_s == "pong", "unexpected body (#{response})"
using HTTPX::URIExtensions
unless RUBY_ENGINE == "jruby"
def test_unix_session
on_unix_server(__method__) do |path|
HTTPX.with(transport: "unix", addresses: [path]).wrap do |http|
http.get("http://unix.com/ping", "http://unix.com/ping").each do |response|
verify_status(response, 200)
assert response.to_s == "pong", "unexpected body (#{response})"
end
end
end
end
end
def test_unix_session_io
skip if RUBY_ENGINE == "jruby"
on_unix_server(__method__) do |path|
io = UNIXSocket.new(path)
HTTPX.with(transport: "unix", io: io).wrap do |http|
response = http.get("http://unix.com/ping")
verify_status(response, 200)
assert response.to_s == "pong", "unexpected body (#{response})"
def test_unix_session_io
on_unix_server(__method__) do |path|
io = UNIXSocket.new(path)
HTTPX.with(transport: "unix", io: io).wrap do |http|
response = http.get("http://unix.com/ping")
verify_status(response, 200)
assert response.to_s == "pong", "unexpected body (#{response})"
end
assert io.eof?, "io should have been used and closed (by the server)"
io.close
end
end
def test_unix_session_io_hash
on_unix_server(__method__) do |path|
io = UNIXSocket.new(path)
uri = URI("http://unix.com/ping")
HTTPX.with(transport: "unix", io: { uri.authority => io }).wrap do |http|
response = http.get(uri)
verify_status(response, 200)
assert response.to_s == "pong", "unexpected body (#{response})"
end
assert io.eof?, "io should have been used and closed (by the server)"
io.close
end
assert io.eof?, "io should have been used and closed (by the server)"
io.close
end
end

View File

@ -2,16 +2,31 @@
module Requests
module IO
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 unless defined?(HTTPX::TLS)
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