Skip to content

Commit d5de7ae

Browse files
committed
Introduce support for discard.
1 parent ddcb2df commit d5de7ae

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

lib/protocol/http/body/buffered.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def finish
5959
# Ensure that future reads return nil, but allow for rewinding.
6060
def close(error = nil)
6161
@index = @chunks.length
62+
63+
return nil
6264
end
6365

6466
def clear
@@ -91,7 +93,7 @@ def read
9193
end
9294

9395
def discard
94-
clear
96+
self.close
9597
end
9698

9799
def write(chunk)

lib/protocol/http/body/readable.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ def finish
133133
Buffered.read(self)
134134
end
135135

136+
# Discard the body as efficiently as possible.
137+
#
138+
# The default implementation simply reads all chunks until the body is empty.
139+
#
140+
# Useful for discarding the body when it is not needed, but preserving the underlying connection.
136141
def discard
137142
while chunk = self.read
138-
chunk.clear
139143
end
140144
end
141145

test/protocol/http/body/buffered.rb

+8
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,12 @@
175175
expect(body.inspect).to be =~ /\d+ chunks, \d+ bytes/
176176
end
177177
end
178+
179+
with "#discard" do
180+
it "closes the body" do
181+
expect(body).to receive(:close)
182+
183+
expect(body.discard).to be == nil
184+
end
185+
end
178186
end

test/protocol/http/body/readable.rb

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@
5858
end
5959
end
6060

61+
with "#discard" do
62+
it "should read all chunks" do
63+
expect(body).to receive(:read).and_return(nil)
64+
expect(body.discard).to be_nil
65+
end
66+
end
67+
6168
with "#as_json" do
6269
it "generates a JSON representation" do
6370
expect(body.as_json).to have_keys(

test/protocol/http/body/reader.rb

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def initialize(body)
2929
end
3030
end
3131

32+
with "#discard" do
33+
it 'discards the body' do
34+
expect(body).to receive(:discard)
35+
expect(reader.discard).to be_nil
36+
end
37+
end
38+
3239
with '#buffered!' do
3340
it 'buffers the body' do
3441
expect(reader.buffered!).to be_equal(reader)

test/protocol/http/body/wrapper.rb

+7
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,11 @@
104104
body.call(stream)
105105
end
106106
end
107+
108+
with "#discard" do
109+
it "should proxy discard" do
110+
expect(source).to receive(:discard).and_return(nil)
111+
expect(body.discard).to be_nil
112+
end
113+
end
107114
end

0 commit comments

Comments
 (0)