added test for trailer headers (http/1 only)

This commit is contained in:
HoneyryderChuck 2021-02-19 10:15:42 +00:00
parent c7251d9a17
commit 75c9f08964
4 changed files with 54 additions and 2 deletions

View File

@ -117,7 +117,7 @@ module HTTPX
response = @request.response
log(level: 2) { "trailer headers received" }
log(color: :yellow) { h.each.map { |f, v| "-> HEADER: #{f}: #{v}" }.join("\n") }
log(color: :yellow) { h.each.map { |f, v| "-> HEADER: #{f}: #{v.join(", ")}" }.join("\n") }
response.merge_headers(h)
end

View File

@ -31,7 +31,7 @@ module HTTPX
def on_headers: (Hash[String, Array[String]] headers) -> void
def on_trailers: (Array[String, String] headers) -> void
def on_trailers: (Hash[String, Array[String]] headers) -> void
def on_data: (string chunk) -> void

View File

@ -64,6 +64,24 @@ class HTTPTest < Minitest::Test
end
end
def test_trailers
server = HTTPTrailersServer.new
th = Thread.new { server.start }
begin
uri = "#{server.origin}/"
HTTPX.plugin(SessionWithPool).wrap do |http|
response = http.get(uri)
assert response.to_s == "trailers", "expected trailers endpoint"
verify_header(response.headers, "trailer", "x-trailer,x-trailer-2")
verify_header(response.headers, "x-trailer", "hello")
verify_header(response.headers, "x-trailer-2", "world")
end
ensure
server.shutdown
th.join
end
end
private
def origin(orig = httpbin)

View File

@ -101,3 +101,37 @@ class NoContentLengthServer < TestServer
mount("/", NoContentLengthApp)
end
end
class HTTPTrailersServer < TestServer
module Trailers
def self.extended(obj)
super
obj.singleton_class.class_eval do
alias_method(:send_body_without_trailers, :send_body)
alias_method(:send_body, :send_body_with_trailers)
end
end
def send_body_with_trailers(socket)
send_body_without_trailers(socket)
socket.write(+"x-trailer: hello" << "\r\n")
socket.write(+"x-trailer-2: world" << "\r\n" << "\r\n")
end
end
class HTTPTrailersApp < WEBrick::HTTPServlet::AbstractServlet
def do_GET(_req, res) # rubocop:disable Naming/MethodName
res.status = 200
res["trailer"] = "x-trailer,x-trailer-2"
res.body = "trailers"
res.extend(Trailers)
end
end
def initialize(options = {})
super
mount("/", HTTPTrailersApp)
end
end