From b18a2468088ea0d88db07a6774ff094a75225c54 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 5 Sep 2024 19:29:51 +1200 Subject: [PATCH] More test coverage / compatibility. --- lib/protocol/http/body/file.rb | 2 +- lib/protocol/http/body/readable.rb | 5 +++++ lib/protocol/http/body/wrapper.rb | 5 ----- test/protocol/http/body/file.rb | 12 ++++++++++++ test/protocol/http/body/readable.rb | 1 + 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/protocol/http/body/file.rb b/lib/protocol/http/body/file.rb index f19ffc5..996463b 100644 --- a/lib/protocol/http/body/file.rb +++ b/lib/protocol/http/body/file.rb @@ -73,7 +73,7 @@ def stream? end def call(stream) - IO.copy_stream(@file, stream) + IO.copy_stream(@file, stream, @remaining) end def join diff --git a/lib/protocol/http/body/readable.rb b/lib/protocol/http/body/readable.rb index 4f22477..226c897 100644 --- a/lib/protocol/http/body/readable.rb +++ b/lib/protocol/http/body/readable.rb @@ -94,6 +94,10 @@ def join end end + def stream? + false + end + # Write the body to the given stream. # # In some cases, the stream may also be readable, such as when hijacking an HTTP/1 connection. In that case, it may be acceptable to read and write to the stream directly. @@ -123,6 +127,7 @@ def as_json(...) { class: self.class.name, length: self.length, + stream: self.stream?, ready: self.ready?, empty: self.empty? } diff --git a/lib/protocol/http/body/wrapper.rb b/lib/protocol/http/body/wrapper.rb index 2d9895f..7c4189b 100644 --- a/lib/protocol/http/body/wrapper.rb +++ b/lib/protocol/http/body/wrapper.rb @@ -50,11 +50,6 @@ def rewindable? @body.rewindable? end - def stream? - # Most wrappers are not streamable by default. - false - end - def length @body.length end diff --git a/test/protocol/http/body/file.rb b/test/protocol/http/body/file.rb index ead3ec8..e54d090 100644 --- a/test/protocol/http/body/file.rb +++ b/test/protocol/http/body/file.rb @@ -85,5 +85,17 @@ expect(output.string).to be == "Hello World" end + + with "/dev/zero" do + it "can stream partial output" do + skip unless File.exist?('/dev/zero') + + body = subject.open('/dev/zero', 0...10) + + body.call(output) + + expect(output.string).to be == "\x00" * 10 + end + end end end diff --git a/test/protocol/http/body/readable.rb b/test/protocol/http/body/readable.rb index 78b133d..fadba64 100644 --- a/test/protocol/http/body/readable.rb +++ b/test/protocol/http/body/readable.rb @@ -63,6 +63,7 @@ expect(body.as_json).to have_keys( class: be == subject.name, length: be_nil, + stream: be == false, ready: be == false, empty: be == false, )