mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
added test for copy_to; also, fixed it (rewind before copy)
This commit is contained in:
parent
d4ec28e4a6
commit
41ca6fadbb
@ -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
|
||||
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user