diff --git a/lib/protocol/rack/body/enumerable.rb b/lib/protocol/rack/body/enumerable.rb index eaf43cb..0a33c18 100644 --- a/lib/protocol/rack/body/enumerable.rb +++ b/lib/protocol/rack/body/enumerable.rb @@ -103,7 +103,7 @@ def close(error = nil) # @yields {|chunk| ...} # @parameter chunk [String] A chunk of the response body. def each(&block) - @body.each(&block) + @body&.each(&block) rescue => error raise ensure diff --git a/releases.md b/releases.md index c903c8e..1ab6cc0 100644 --- a/releases.md +++ b/releases.md @@ -3,6 +3,7 @@ ## Unreleased - Respect the result of rewinding the underlying request body. + - Avoid enumerating a response body after it has been closed. ## v0.22.1 diff --git a/test/protocol/rack/body/enumerable.rb b/test/protocol/rack/body/enumerable.rb index a564b52..9c851c9 100644 --- a/test/protocol/rack/body/enumerable.rb +++ b/test/protocol/rack/body/enumerable.rb @@ -58,6 +58,16 @@ end end.to raise_exception(RuntimeError, message: be =~ /Bad Enumerable/) end + + it "does not enumerate after being closed" do + body = subject.new(["Hello World"], 11) + body.close + + chunks = [] + body.each{|chunk| chunks << chunk} + + expect(chunks).to be(:empty?) + end end with "#call" do