added test for copy_to; also, fixed it (rewind before copy)

This commit is contained in:
HoneyryderChuck 2020-03-15 19:59:39 +00:00
parent d4ec28e4a6
commit 41ca6fadbb
2 changed files with 25 additions and 0 deletions

View File

@ -150,6 +150,8 @@ module HTTPX
def copy_to(dest)
return unless @buffer
rewind
if dest.respond_to?(:path) && @buffer.respond_to?(:path)
FileUtils.mv(@buffer.path, dest.path)
else

View File

@ -49,6 +49,29 @@ class ResponseTest < Minitest::Test
assert body3 == "", "HEAD requets body must be empty"
end
def test_response_body_copy_to_memory
payload = "a" * 512
body = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body.write(payload)
memory = StringIO.new
body.copy_to(memory)
assert memory.string == payload, "didn't copy all bytes (expected #{payload.bytesize}, was #{memory.size})"
body.close
end
def test_response_body_copy_to_file
payload = "a" * 2048
body = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body.write(payload)
file = Tempfile.new("httpx-file-buffer")
body.copy_to(file)
assert File.read(file.path) == payload, "didn't copy all bytes (expected #{payload.bytesize}, was #{File.size(file.path)})"
body.close
file.unlink
end
def test_response_body_read
body1 = Response::Body.new(Response.new(request, 200, "2.0", {}), threshold_size: 1024)
body1.write("foo")