From c8e7cc12dcdbdd6f128a6f860ee61e93c2385b0e Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 28 Apr 2025 19:50:58 +0900 Subject: [PATCH] Return the buffer unless empty. --- lib/protocol/http/body/stream.rb | 7 ++++++- test/protocol/http/body/stream.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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