Skip to content

Commit ce15810

Browse files
bruno-ioquatix
authored andcommitted
Body::Readable#each returns enumerator w/o block
1 parent 8f8ca2d commit ce15810

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/protocol/http/body/readable.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ def finish
6666

6767
# Enumerate all chunks until finished, then invoke `#close`.
6868
def each
69-
while chunk = self.read
70-
yield chunk
69+
return to_enum(:each) unless block_given?
70+
71+
begin
72+
while chunk = self.read
73+
yield chunk
74+
end
75+
ensure
76+
self.close($!)
7177
end
72-
ensure
73-
self.close($!)
7478
end
7579

7680
# Read all remaining chunks into a single binary string using `#each`.

test/protocol/http/body/buffered.rb

+31-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,37 @@
122122
expect(body.read).to be == "Hello"
123123
end
124124
end
125-
125+
126+
with "#each" do
127+
with "a block" do
128+
it "iterates over chunks" do
129+
result = []
130+
body.each{|chunk| result << chunk}
131+
expect(result).to be == source
132+
end
133+
end
134+
135+
with "no block" do
136+
it "returns an enumerator" do
137+
expect(body.each).to be_a(Enumerator)
138+
end
139+
140+
it "can be chained with enumerator methods" do
141+
result = []
142+
143+
body.each.with_index do |chunk, index|
144+
if index.zero?
145+
result << chunk.upcase
146+
else
147+
result << chunk.downcase
148+
end
149+
end
150+
151+
expect(result).to be == ["HELLO", "world"]
152+
end
153+
end
154+
end
155+
126156
with '#inspect' do
127157
it "can be inspected" do
128158
expect(body.inspect).to be =~ /\d+ chunks, \d+ bytes/

0 commit comments

Comments
 (0)