mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-13 00:02:57 -04:00
added test for trailer headers (http/1 only)
This commit is contained in:
parent
c7251d9a17
commit
75c9f08964
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user