From d34fabd611fb047083f6af44b2eecaebf6ff2842 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 22 Sep 2024 12:49:57 +1200 Subject: [PATCH] Reading all chunks should close the underlying body. --- lib/protocol/rack/input.rb | 9 ++++++++- test/protocol/rack/input.rb | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/protocol/rack/input.rb b/lib/protocol/rack/input.rb index 12b10fb..a7bc997 100644 --- a/lib/protocol/rack/input.rb +++ b/lib/protocol/rack/input.rb @@ -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 diff --git a/test/protocol/rack/input.rb b/test/protocol/rack/input.rb index 26aa1a6..2ac76c4 100644 --- a/test/protocol/rack/input.rb +++ b/test/protocol/rack/input.rb @@ -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 @@ -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