From dbc874adf55ff20120f88a9566a4faf487ada66f Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 3 Oct 2024 15:18:54 +1300 Subject: [PATCH] Invoke close on input stream when input is finished. --- lib/protocol/http/body/stream.rb | 9 ++++++++- test/protocol/http/body/streamable.rb | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/protocol/http/body/stream.rb b/lib/protocol/http/body/stream.rb index 34e96cd..742e18e 100644 --- a/lib/protocol/http/body/stream.rb +++ b/lib/protocol/http/body/stream.rb @@ -295,7 +295,14 @@ def empty? def read_next if @input - return @input.read + # User's may forget to call #close... + if chunk = @input.read + return chunk + else + # So if we are at the end of the stream, we close it automatically: + @input.close + @input = nil + end elsif @closed_read raise IOError, "Stream is not readable, input has been closed!" end diff --git a/test/protocol/http/body/streamable.rb b/test/protocol/http/body/streamable.rb index d62b702..e6bd189 100644 --- a/test/protocol/http/body/streamable.rb +++ b/test/protocol/http/body/streamable.rb @@ -175,9 +175,10 @@ with "#close" do it "can close the body" do - expect(input).not.to receive(:close) + expect(input).to receive(:close) expect(body.read).to be == "Hello" + body.close end end