diff --git a/fixtures/protocol/http/body/a_writable_body.rb b/fixtures/protocol/http/body/a_writable_body.rb deleted file mode 100644 index dc58deb..0000000 --- a/fixtures/protocol/http/body/a_writable_body.rb +++ /dev/null @@ -1,110 +0,0 @@ -# frozen_string_literal: true - -# Released under the MIT License. -# Copyright, 2019-2023, by Samuel Williams. - -require 'protocol/http/body/deflate' - -module Protocol - module HTTP - module Body - AWritableBody = Sus::Shared("a writable body") do - 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 - - 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 - # reactor.async do - # expect do - # body.each do |chunk| - # raise RuntimeError.new("It was too big!") - # end - # end.to raise_exception(RuntimeError, message: be =~ /big/) - # end - - # expect{ - # body.write("Beep boop") # This will cause a failure. - # ::Async::Task.current.yield - # body.write("Beep boop") # This will fail. - # }.to raise_exception(RuntimeError, message: be =~ /big/) - # end - - # it "can propagate failures in nested bodies" do - # nested = ::Protocol::HTTP::Body::Deflate.for(body) - - # reactor.async do - # expect do - # nested.each do |chunk| - # raise RuntimeError.new("It was too big!") - # end - # end.to raise_exception(RuntimeError, message: be =~ /big/) - # end - - # expect{ - # body.write("Beep boop") # This will cause a failure. - # ::Async::Task.current.yield - # body.write("Beep boop") # This will fail. - # }.to raise_exception(RuntimeError, message: be =~ /big/) - # end - - # it "will stop after finishing" do - # output_task = reactor.async do - # body.each do |chunk| - # expect(chunk).to be == "Hello World!" - # end - # end - - # body.write("Hello World!") - # body.close - - # expect(body).not.to be(:empty?) - - # ::Async::Task.current.yield - - # expect(output_task).to be(:finished?) - # expect(body).to be(:empty?) - # end - end - end - end - end -end diff --git a/test/protocol/http/body/writable.rb b/test/protocol/http/body/writable.rb index 3c6ea4d..3da5cf6 100644 --- a/test/protocol/http/body/writable.rb +++ b/test/protocol/http/body/writable.rb @@ -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 @@ -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