mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
response: do not alias #write to #<<, makes it hard to extend; #each now yields predictable-size chunks, instead of deferring to buffer#each, which can do whatever
This commit is contained in:
parent
b55911277a
commit
957fc46bcf
@ -24,11 +24,12 @@ module HTTPX
|
||||
@request = request
|
||||
@status = Integer(status)
|
||||
@headers = @options.headers_class.new(headers)
|
||||
@body = @options.response_body_class.new(self, threshold_size: @options.body_threshold_size)
|
||||
@body = @options.response_body_class.new(self, threshold_size: @options.body_threshold_size,
|
||||
window_size: @options.window_size)
|
||||
end
|
||||
|
||||
def <<(data)
|
||||
@body << data
|
||||
@body.write(data)
|
||||
end
|
||||
|
||||
def bodyless?
|
||||
@ -45,10 +46,11 @@ module HTTPX
|
||||
end
|
||||
|
||||
class Body
|
||||
def initialize(response, threshold_size: )
|
||||
def initialize(response, threshold_size: , window_size: 1 << 14)
|
||||
@response = response
|
||||
@headers = response.headers
|
||||
@threshold_size = threshold_size
|
||||
@window_size = window_size
|
||||
@encoding = response.content_type.charset || Encoding::BINARY
|
||||
@length = 0
|
||||
@buffer = nil
|
||||
@ -60,7 +62,6 @@ module HTTPX
|
||||
transition
|
||||
@buffer.write(chunk)
|
||||
end
|
||||
alias :<< :write
|
||||
|
||||
def read(*args)
|
||||
return unless @buffer
|
||||
@ -76,8 +77,8 @@ module HTTPX
|
||||
begin
|
||||
unless @state == :idle
|
||||
rewind
|
||||
@buffer.each do |*args|
|
||||
yield(*args)
|
||||
while chunk = @buffer.read(@window_size)
|
||||
yield(chunk)
|
||||
end
|
||||
end
|
||||
ensure
|
||||
|
@ -26,10 +26,10 @@ class ResponseTest < Minitest::Test
|
||||
opts = { threshold_size: 1024 }
|
||||
body1 = Response::Body.new(Response.new(request, 200, {}), opts)
|
||||
assert body1.empty?, "body must be empty after initialization"
|
||||
body1 << "foo"
|
||||
body1.write("foo")
|
||||
assert body1 == "foo", "body must be updated"
|
||||
body1 << "foo"
|
||||
body1 << "bar"
|
||||
body1.write("foo")
|
||||
body1.write("bar")
|
||||
assert body1 == "foobar", "body must buffer subsequent chunks"
|
||||
|
||||
body3 = Response::Body.new(Response.new(request("head"), 200, {}), opts)
|
||||
@ -41,10 +41,10 @@ class ResponseTest < Minitest::Test
|
||||
def test_response_body_each
|
||||
opts = { threshold_size: 1024 }
|
||||
body1 = Response::Body.new(Response.new(request, 200, {}), opts)
|
||||
body1 << "foo"
|
||||
body1.write("foo")
|
||||
assert body1.each.to_a == %w(foo), "must yield buffer"
|
||||
body1 << "foo"
|
||||
body1 << "bar"
|
||||
body1.write("foo")
|
||||
body1.write("bar")
|
||||
assert body1.each.to_a == %w(foobar), "must yield buffers"
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user