response body now holds the full options from the request

This commit is contained in:
HoneyryderChuck 2021-03-05 18:23:45 +00:00
parent 3fd4cd98cf
commit 12d4885136
7 changed files with 23 additions and 20 deletions

View File

@ -61,7 +61,7 @@ module HTTPX
module ResponseBodyMethods
attr_reader :encodings
def initialize(*, **)
def initialize(*)
@encodings = []
super

View File

@ -31,7 +31,7 @@ module HTTPX
end
module ResponseBodyMethods
def initialize(*, **)
def initialize(*)
super
@stream = @response.stream
end

View File

@ -5,7 +5,7 @@ module HTTPX
module Upgrade
extend Registry
def self.load_dependencies(klass)
def self.configure(klass)
klass.plugin(:"upgrade/h2")
end

View File

@ -27,8 +27,7 @@ module HTTPX
@version = version
@status = Integer(status)
@headers = @options.headers_class.new(headers)
@body = @options.response_body_class.new(self, threshold_size: @options.body_threshold_size,
window_size: @options.window_size)
@body = @options.response_body_class.new(self, @options)
end
def merge_headers(h)
@ -83,11 +82,12 @@ module HTTPX
end
class Body
def initialize(response, threshold_size:, window_size: 1 << 14)
def initialize(response, options)
@response = response
@headers = response.headers
@threshold_size = threshold_size
@window_size = window_size
@options = options
@threshold_size = options.body_threshold_size
@window_size = options.window_size
@encoding = response.content_type.charset || Encoding::BINARY
@length = 0
@buffer = nil

View File

@ -37,6 +37,8 @@ module HTTPX
include _ToStr
@state: :idle | :memory | :buffer
@threshold_size: Integer
@window_size: Integer
def each: () { (String) -> void } -> void
| () -> Enumerable[String]
@ -48,7 +50,7 @@ module HTTPX
private
def initialize: (Response, ?threshold_size: Integer, ?window_size: Integer) -> untyped
def initialize: (Response, options) -> untyped
def rewind: () -> void
def transition: () -> void
end

View File

@ -36,22 +36,23 @@ class ResponseTest < Minitest::Test
end
def test_response_body_to_s
body1 = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body1 = Response::Body.new(Response.new(request, 200, "2.0", {}), Options.new(body_threshold_size: 1024))
assert body1.empty?, "body must be empty after initialization"
body1.write("foo")
assert body1 == "foo", "body must be updated"
body2 = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body2 = Response::Body.new(Response.new(request, 200, "2.0", {}), Options.new(body_threshold_size: 1024))
body2.write("foo")
body2.write("bar")
assert body2 == "foobar", "body buffers chunks"
body3 = Response::Body.new(Response.new(request("head"), 200, "2.0", {}), threshold_size: 1024)
body3 = Response::Body.new(Response.new(request("head"), 200, "2.0", {}), Options.new(body_threshold_size: 1024))
assert body3.empty?, "body must be empty after initialization"
assert body3 == "", "HEAD request body must be empty (#{body3})"
text = +"heãd"
text.force_encoding(Encoding::BINARY)
body4 = Response::Body.new(Response.new(request, 200, "2.0", { "content-type" => "text/html; charset=utf" }), threshold_size: 1024)
body4 = Response::Body.new(Response.new(request, 200, "2.0", { "content-type" => "text/html; charset=utf" }),
Options.new(body_threshold_size: 1024))
body4.write(text)
req_text = body4.to_s
assert text == req_text, "request body must be in original encoding (#{req_text})"
@ -59,7 +60,7 @@ class ResponseTest < Minitest::Test
def test_response_body_copy_to_memory
payload = "a" * 512
body = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body = Response::Body.new(Response.new(request, 200, "2.0", {}), Options.new(body_threshold_size: 1024))
body.write(payload)
memory = StringIO.new
@ -70,7 +71,7 @@ class ResponseTest < Minitest::Test
def test_response_body_copy_to_file
payload = "a" * 2048
body = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body = Response::Body.new(Response.new(request, 200, "2.0", {}), Options.new(body_threshold_size: 1024))
body.write(payload)
file = Tempfile.new("httpx-file-buffer")
@ -81,7 +82,7 @@ class ResponseTest < Minitest::Test
end
def test_response_body_read
body1 = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body1 = Response::Body.new(Response.new(request, 200, "2.0", {}), Options.new(body_threshold_size: 1024))
body1.write("foo")
assert body1.bytesize == 3
assert body1.read(1), "f"
@ -90,17 +91,17 @@ class ResponseTest < Minitest::Test
end
def test_response_body_each
body1 = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body1 = Response::Body.new(Response.new(request, 200, "2.0", {}), Options.new(body_threshold_size: 1024))
body1.write("foo")
assert body1.each.to_a == %w[foo], "must yield buffer"
body2 = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body2 = Response::Body.new(Response.new(request, 200, "2.0", {}), Options.new(body_threshold_size: 1024))
body2.write("foo")
body2.write("bar")
assert body2.each.to_a == %w[foobar], "must yield buffers"
end
def test_response_body_buffer
body = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 10)
body = Response::Body.new(Response.new(request, 200, "2.0", {}), Options.new(body_threshold_size: 10))
body.extend(Module.new do
attr_reader :buffer
end)

View File

@ -37,7 +37,7 @@ module Requests
custom_body = Class.new(HTTPX::Response::Body) do
attr_reader :file
def initialize(_response, **)
def initialize(_response, _opts)
@file = Tempfile.new("httpx-test")
end