diff --git a/lib/protocol/http/body/stream.rb b/lib/protocol/http/body/stream.rb index 62b0314..28aa01c 100644 --- a/lib/protocol/http/body/stream.rb +++ b/lib/protocol/http/body/stream.rb @@ -263,7 +263,12 @@ def gets(separator = NEWLINE, limit = nil, chomp: false) buffer = @buffer @buffer = nil - return @buffer + # Return nil for empty buffers, otherwise return the content: + if buffer && !buffer.empty? + return buffer + else + return nil + end end end diff --git a/test/protocol/http/body/stream.rb b/test/protocol/http/body/stream.rb index 2bec74a..3ad69ce 100644 --- a/test/protocol/http/body/stream.rb +++ b/test/protocol/http/body/stream.rb @@ -240,6 +240,16 @@ expect(stream.gets).to be == nil end end + + with "incomplete line at the end" do + let(:input) {Protocol::HTTP::Body::Buffered.new(["Hello\nWorld"])} + + it "returns the remaining buffer when there is no more data to read" do + expect(stream.gets).to be == "Hello\n" + expect(stream.gets).to be == "World" + expect(stream.gets).to be == nil + end + end end with "#close_read" do