Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not ignore encoding just because a read length is given #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/down/chunked_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def read(length = nil, outbuf = nil)

buffer.clear if buffer # deallocate string

data.force_encoding(@encoding) unless length
data.force_encoding(@encoding)
data unless data.empty? && length && length > 0
end

Expand Down
12 changes: 6 additions & 6 deletions test/chunked_io_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,16 @@ def chunked_io(**options)
assert_equal "bc", io.read(2)
end

it "returns content in binary encoding" do
it "returns content in requested encoding" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal Encoding::BINARY, io.read(1).encoding
io.rewind
assert_equal Encoding::BINARY, io.read(1).encoding

io = chunked_io(chunks: ["ab", "c"].each, encoding: "utf-8")
assert_equal Encoding::BINARY, io.read(1).encoding
assert_equal Encoding::UTF_8, io.read(1).encoding
io.rewind
assert_equal Encoding::BINARY, io.read(1).encoding
assert_equal Encoding::UTF_8, io.read(1).encoding
end
end

Expand Down Expand Up @@ -249,16 +249,16 @@ def chunked_io(**options)
assert_equal "bc", io.read(2, "")
end

it "returns content in binary encoding" do
it "returns content in requested encoding" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal Encoding::BINARY, io.read(1, "").encoding
io.rewind
assert_equal Encoding::BINARY, io.read(1, "").encoding

io = chunked_io(chunks: ["ab", "c"].each, encoding: "utf-8")
assert_equal Encoding::BINARY, io.read(1, "").encoding
assert_equal Encoding::UTF_8, io.read(1, "").encoding
io.rewind
assert_equal Encoding::BINARY, io.read(1, "").encoding
assert_equal Encoding::UTF_8, io.read(1, "").encoding
end
end

Expand Down