Skip to content

Commit

Permalink
Rework AWritableBody tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 5, 2024
1 parent 1b1ccac commit 85ffef0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 113 deletions.
110 changes: 0 additions & 110 deletions fixtures/protocol/http/body/a_writable_body.rb

This file was deleted.

91 changes: 88 additions & 3 deletions test/protocol/http/body/writable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
# Copyright, 2018-2023, by Samuel Williams.

require 'protocol/http/body/writable'
require 'protocol/http/body/a_writable_body'
require 'protocol/http/body/deflate'

describe Protocol::HTTP::Body::Writable do
let(:body) {subject.new}

it_behaves_like Protocol::HTTP::Body::AWritableBody

with "#length" do
it "should be unspecified by default" do
expect(body.length).to be_nil
Expand Down Expand Up @@ -79,5 +77,92 @@
body.write("Hello")
end.to raise_exception(Protocol::HTTP::Body::Writable::Closed)
end

it "can write and read data" do
3.times do |i|
body.write("Hello World #{i}")
expect(body.read).to be == "Hello World #{i}"
end
end

it "can buffer data in order" do
3.times do |i|
body.write("Hello World #{i}")
end

3.times do |i|
expect(body.read).to be == "Hello World #{i}"
end
end
end

with '#join' do
it "can join chunks" do
3.times do |i|
body.write("#{i}")
end

body.close

expect(body.join).to be == "012"
end
end

with '#each' do
it "can read all data in order" do
3.times do |i|
body.write("Hello World #{i}")
end

body.close

3.times do |i|
chunk = body.read
expect(chunk).to be == "Hello World #{i}"
end
end

it "can propagate failures" do
body.write("Beep boop") # This will cause a failure.

expect do
body.each do |chunk|
raise RuntimeError.new("It was too big!")
end
end.to raise_exception(RuntimeError, message: be =~ /big/)

expect do
body.write("Beep boop") # This will fail.
end.to raise_exception(RuntimeError, message: be =~ /big/)
end

it "can propagate failures in nested bodies" do
nested = ::Protocol::HTTP::Body::Deflate.for(body)

body.write("Beep boop") # This will cause a failure.

expect do
nested.each do |chunk|
raise RuntimeError.new("It was too big!")
end
end.to raise_exception(RuntimeError, message: be =~ /big/)

expect do
body.write("Beep boop") # This will fail.
end.to raise_exception(RuntimeError, message: be =~ /big/)
end

it "will stop after finishing" do
body.write("Hello World!")
body.close

expect(body).not.to be(:empty?)

body.each do |chunk|
expect(chunk).to be == "Hello World!"
end

expect(body).to be(:empty?)
end
end
end

0 comments on commit 85ffef0

Please sign in to comment.