Skip to content

Commit

Permalink
Reading all chunks should close the underlying body.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 22, 2024
1 parent 62adf4d commit d34fabd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/protocol/rack/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ def empty?

def read_next
if @body
@body.read
# User's may forget to call #close...
if chunk = @body.read
return chunk
else
# So if we are at the end of the stream, we close it automatically:
@body.close
@body = nil
end
elsif @closed
raise IOError, "Stream is not readable, input has been closed!"
end
Expand Down
6 changes: 6 additions & 0 deletions test/protocol/rack/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@

with '#read' do
it "can read all input" do
expect(body).to receive(:close)

expect(input.read).to be == sample_data.join
expect(input.read).to be == ""

expect(input.body).to be_nil
end

it "can read no input" do
Expand All @@ -64,6 +68,8 @@
expect(input.read).to be == sample_data.join[15..-1]

expect(input.read(1)).to be == nil

expect(input.body).to be_nil
end

it "can read partial input with buffer" do
Expand Down

0 comments on commit d34fabd

Please sign in to comment.