Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 28, 2024
1 parent 4b5a295 commit f6b06d1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/protocol/http/body/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def rewind
end

def rewindable?
@body.rewind?
@body.rewindable?
end

def length
Expand Down
4 changes: 4 additions & 0 deletions test/protocol/http/body/buffered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/protocol/http/body/completable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions test/protocol/http/body/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions test/protocol/http/body/rewindable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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 == "#<Protocol::HTTP::Body::Rewindable 0/0 chunks read>"
Expand Down
14 changes: 14 additions & 0 deletions test/protocol/http/body/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f6b06d1

Please sign in to comment.