From f6b06d1569bb4d12f8f6f6da0b98d6fa1ce01a57 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 29 Aug 2024 10:34:32 +1200 Subject: [PATCH] Add tests. --- lib/protocol/http/body/wrapper.rb | 2 +- test/protocol/http/body/buffered.rb | 4 ++++ test/protocol/http/body/completable.rb | 9 +++++++++ test/protocol/http/body/readable.rb | 7 +++++++ test/protocol/http/body/rewindable.rb | 27 ++++++++++++++++++++++++++ test/protocol/http/body/wrapper.rb | 14 +++++++++++++ 6 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/protocol/http/body/wrapper.rb b/lib/protocol/http/body/wrapper.rb index db2297e..82bb252 100644 --- a/lib/protocol/http/body/wrapper.rb +++ b/lib/protocol/http/body/wrapper.rb @@ -51,7 +51,7 @@ def rewind end def rewindable? - @body.rewind? + @body.rewindable? end def length diff --git a/test/protocol/http/body/buffered.rb b/test/protocol/http/body/buffered.rb index c53e3c2..679ba6e 100644 --- a/test/protocol/http/body/buffered.rb +++ b/test/protocol/http/body/buffered.rb @@ -117,6 +117,10 @@ end with "#rewind" do + it "is rewindable" do + expect(body).to be(:rewindable?) + end + it "positions the cursor to the beginning" do expect(body.read).to be == "Hello" body.rewind diff --git a/test/protocol/http/body/completable.rb b/test/protocol/http/body/completable.rb index e09c91d..e1130fa 100644 --- a/test/protocol/http/body/completable.rb +++ b/test/protocol/http/body/completable.rb @@ -83,4 +83,13 @@ expect(completable.read).to be_nil end end + + with "#rewindable?" do + it "is not rewindable" do + # Because completion can only happen once, we can't rewind the body. + expect(body).to be(:rewindable?) + expect(completable).not.to be(:rewindable?) + expect(completable.rewind).to be == false + end + end end diff --git a/test/protocol/http/body/readable.rb b/test/protocol/http/body/readable.rb index d19945e..65ec4e6 100644 --- a/test/protocol/http/body/readable.rb +++ b/test/protocol/http/body/readable.rb @@ -58,4 +58,11 @@ expect(JSON.dump(body)).to be == body.to_json end end + + with "#rewindable?" do + it "is not rewindable" do + expect(body).not.to be(:rewindable?) + expect(body.rewind).to be == false + end + end end diff --git a/test/protocol/http/body/rewindable.rb b/test/protocol/http/body/rewindable.rb index 7768340..b45ae30 100644 --- a/test/protocol/http/body/rewindable.rb +++ b/test/protocol/http/body/rewindable.rb @@ -4,6 +4,7 @@ # Copyright, 2019-2023, by Samuel Williams. require 'protocol/http/body/rewindable' +require 'protocol/http/request' describe Protocol::HTTP::Body::Rewindable do let(:source) {Protocol::HTTP::Body::Buffered.new} @@ -47,6 +48,26 @@ end end + with ".wrap" do + with "a buffered body" do + let(:body) {Protocol::HTTP::Body::Buffered.new} + let(:message) {Protocol::HTTP::Request.new(nil, nil, 'GET', '/', nil, Protocol::HTTP::Headers.new, body)} + + it "returns the body" do + expect(subject.wrap(message)).to be == body + end + end + + with "a non-rewindable body" do + let(:body) {Protocol::HTTP::Body::Readable.new} + let(:message) {Protocol::HTTP::Request.new(nil, nil, 'GET', '/', nil, Protocol::HTTP::Headers.new, body)} + + it "returns a new rewindable body" do + expect(subject.wrap(message)).to be_a(Protocol::HTTP::Body::Rewindable) + end + end + end + with '#buffered' do it "can generate buffered representation" do 3.times do |i| @@ -76,6 +97,12 @@ end end + with "#rewindable?" do + it "is rewindable" do + expect(body).to be(:rewindable?) + end + end + with '#inspect' do it "can generate string representation" do expect(body.inspect).to be == "#" diff --git a/test/protocol/http/body/wrapper.rb b/test/protocol/http/body/wrapper.rb index 849c3e0..55bdf15 100644 --- a/test/protocol/http/body/wrapper.rb +++ b/test/protocol/http/body/wrapper.rb @@ -64,6 +64,20 @@ end end + with "#rewindable?" do + it "should proxy rewindable?" do + expect(source).to receive(:rewindable?).and_return(true) + expect(body.rewindable?).to be == true + end + end + + with "#rewind" do + it "should proxy rewind" do + expect(source).to receive(:rewind).and_return(true) + expect(body.rewind).to be == true + end + end + with "#as_json" do it "generates a JSON representation" do expect(body.as_json).to have_keys(