trimming, and testing passing the io when the transport is to be a unix socket

This commit is contained in:
HoneyryderChuck 2021-03-08 12:49:33 +00:00
parent 0fb343056d
commit 0a0300c5eb
3 changed files with 55 additions and 15 deletions

View File

@ -26,13 +26,13 @@ module HTTPX
else
@options.io
end
raise Error, "Given IO objects do not match the request authority" unless @io
_, _, _, @ip = @io.addr
@addresses ||= [@ip]
@ip_index = @addresses.size - 1
unless @io.nil?
@keep_open = true
@state = :connected
end
@keep_open = true
@state = :connected
else
@ip_index = @addresses.size - 1
@ip = @addresses[@ip_index]

View File

@ -54,10 +54,26 @@ module HTTPX
::IO::WaitReadable
end
# :nocov:
def inspect
id = @io.closed? ? "closed" : @io.fileno
"#<UNIX(path: #{@path}): (state: #{@state})>"
end
# :nocov:
private
def build_socket
Socket.new(Socket::PF_UNIX, :STREAM, 0)
end
def log_transition_state(nextstate)
case nextstate
when :connected
"Connected to #{@path} (##{@io.fileno})"
else
"#{@path} #{@state} -> #{nextstate}"
end
end
end
end

View File

@ -8,10 +8,27 @@ class UnixTest < Minitest::Test
def test_unix_session
skip if RUBY_ENGINE == "jruby"
on_unix_server do |path|
response = HTTPX.with(transport: "unix", transport_options: { path: path }).get("http://unix.com/ping")
verify_status(response, 200)
assert response.to_s == "pong", "unexpected body (#{response})"
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
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})"
end
assert io.eof?, "io should have been used and closed (by the server)"
io.close
end
end
@ -25,21 +42,28 @@ class UnixTest < Minitest::Test
Connection: close
HTTP
def on_unix_server
def on_unix_server(sockname)
mutex = Mutex.new
resource = ConditionVariable.new
path = File.join(Dir.tmpdir, "httpx-unix.sock")
path = File.join(Dir.tmpdir, "httpx-unix-#{sockname}.sock")
server = UNIXServer.new(path)
begin
th = Thread.start do
mutex.synchronize do
resource.signal
end
socket = server.accept
socket.readpartial(4096) # drain the socket for the request
socket.write(RESPONSE_HEADER)
socket.write("pong")
socket.close
loop do
begin
socket = server.accept
socket.readpartial(4096) # drain the socket for the request
socket.write(RESPONSE_HEADER)
socket.write("pong")
socket.close
rescue IOError
break
end
end
end
mutex.synchronize do
resource.wait(mutex)