httpx/test/support/requests/with_chunked_body.rb
HoneyryderChuck 092e594a4b Request.verb is now an upcased string (ex: "GET")
The reference for a request verb is now the string which is used
everywhere else, instead of the symbol corresponding to it. This was an
artifact from the import from httprb, and there is no advantage in it,
since these strings are frozen in most use cases, and the
transformations from symbol to strings being performed everywhere are
prooof that keeping the atom isn't really bringing any benefit.
2023-04-17 16:54:31 +03:00

48 lines
1.8 KiB
Ruby

# frozen_string_literal: true
module Requests
module WithChunkedBody
%w[post put patch delete].each do |meth|
define_method :"test_#{meth}_chunked_body_params" do
uri = build_uri("/#{meth}")
response = HTTPX.with_headers("transfer-encoding" => "chunked")
.send(meth, uri, body: %w[this is a chunked response])
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Transfer-Encoding", "chunked")
assert body.key?("data")
# assert body["data"] == "thisisachunkedresponse",
# "unexpected body (#{body["data"]})"
end
define_method :"test_#{meth}_chunked_body_trailer" do
uri = build_uri("/#{meth}")
http = HTTPX.with_headers("transfer-encoding" => "chunked")
total_time = start_time = nil
trailered = false
request = http.build_request(meth.upcase, uri, headers: { "trailer" => "X-Time-Spent" }, body: %w[this is a chunked response])
request.on(:headers) do |_written_request|
start_time = HTTPX::Utils.now
end
request.on(:trailers) do |written_request|
total_time = HTTPX::Utils.elapsed_time(start_time)
written_request.trailers["x-time-spent"] = total_time
trailered = true
end
response = http.request(request)
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Transfer-Encoding", "chunked")
# httpbin sadly doesn't receive trailers...
# verify_header(body["headers"], "X-Time-Spent", total_time.to_s)
assert body.key?("data")
assert trailered, "trailer callback wasn't called"
# assert body["data"] == "thisisachunkedresponse",
# "unexpected body (#{body["data"]})"
end
end
end
end