Skip to content

Commit 85ffef0

Browse files
committed
Rework AWritableBody tests.
1 parent 1b1ccac commit 85ffef0

File tree

2 files changed

+88
-113
lines changed

2 files changed

+88
-113
lines changed

fixtures/protocol/http/body/a_writable_body.rb

Lines changed: 0 additions & 110 deletions
This file was deleted.

test/protocol/http/body/writable.rb

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
# Copyright, 2018-2023, by Samuel Williams.
55

66
require 'protocol/http/body/writable'
7-
require 'protocol/http/body/a_writable_body'
7+
require 'protocol/http/body/deflate'
88

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

12-
it_behaves_like Protocol::HTTP::Body::AWritableBody
13-
1412
with "#length" do
1513
it "should be unspecified by default" do
1614
expect(body.length).to be_nil
@@ -79,5 +77,92 @@
7977
body.write("Hello")
8078
end.to raise_exception(Protocol::HTTP::Body::Writable::Closed)
8179
end
80+
81+
it "can write and read data" do
82+
3.times do |i|
83+
body.write("Hello World #{i}")
84+
expect(body.read).to be == "Hello World #{i}"
85+
end
86+
end
87+
88+
it "can buffer data in order" do
89+
3.times do |i|
90+
body.write("Hello World #{i}")
91+
end
92+
93+
3.times do |i|
94+
expect(body.read).to be == "Hello World #{i}"
95+
end
96+
end
97+
end
98+
99+
with '#join' do
100+
it "can join chunks" do
101+
3.times do |i|
102+
body.write("#{i}")
103+
end
104+
105+
body.close
106+
107+
expect(body.join).to be == "012"
108+
end
109+
end
110+
111+
with '#each' do
112+
it "can read all data in order" do
113+
3.times do |i|
114+
body.write("Hello World #{i}")
115+
end
116+
117+
body.close
118+
119+
3.times do |i|
120+
chunk = body.read
121+
expect(chunk).to be == "Hello World #{i}"
122+
end
123+
end
124+
125+
it "can propagate failures" do
126+
body.write("Beep boop") # This will cause a failure.
127+
128+
expect do
129+
body.each do |chunk|
130+
raise RuntimeError.new("It was too big!")
131+
end
132+
end.to raise_exception(RuntimeError, message: be =~ /big/)
133+
134+
expect do
135+
body.write("Beep boop") # This will fail.
136+
end.to raise_exception(RuntimeError, message: be =~ /big/)
137+
end
138+
139+
it "can propagate failures in nested bodies" do
140+
nested = ::Protocol::HTTP::Body::Deflate.for(body)
141+
142+
body.write("Beep boop") # This will cause a failure.
143+
144+
expect do
145+
nested.each do |chunk|
146+
raise RuntimeError.new("It was too big!")
147+
end
148+
end.to raise_exception(RuntimeError, message: be =~ /big/)
149+
150+
expect do
151+
body.write("Beep boop") # This will fail.
152+
end.to raise_exception(RuntimeError, message: be =~ /big/)
153+
end
154+
155+
it "will stop after finishing" do
156+
body.write("Hello World!")
157+
body.close
158+
159+
expect(body).not.to be(:empty?)
160+
161+
body.each do |chunk|
162+
expect(chunk).to be == "Hello World!"
163+
end
164+
165+
expect(body).to be(:empty?)
166+
end
82167
end
83168
end

0 commit comments

Comments
 (0)