File tree 8 files changed +57
-0
lines changed
8 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,8 @@ def finish
59
59
# Ensure that future reads return nil, but allow for rewinding.
60
60
def close ( error = nil )
61
61
@index = @chunks . length
62
+
63
+ return nil
62
64
end
63
65
64
66
def clear
@@ -90,6 +92,10 @@ def read
90
92
end
91
93
end
92
94
95
+ def discard
96
+ self . close
97
+ end
98
+
93
99
def write ( chunk )
94
100
@chunks << chunk
95
101
end
Original file line number Diff line number Diff line change @@ -133,6 +133,16 @@ def finish
133
133
Buffered . read ( self )
134
134
end
135
135
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.
141
+ def discard
142
+ while chunk = self . read
143
+ end
144
+ end
145
+
136
146
def as_json ( ...)
137
147
{
138
148
class : self . class . name ,
Original file line number Diff line number Diff line change @@ -40,6 +40,14 @@ def finish
40
40
end
41
41
end
42
42
43
+ # Discard the body as efficiently as possible.
44
+ def discard
45
+ if body = @body
46
+ @body = nil
47
+ body . discard
48
+ end
49
+ end
50
+
43
51
# Buffer the entire request/response body.
44
52
# @returns [Reader] itself.
45
53
def buffered!
Original file line number Diff line number Diff line change @@ -59,6 +59,10 @@ def read
59
59
@body . read
60
60
end
61
61
62
+ def discard
63
+ @body . discard
64
+ end
65
+
62
66
def as_json ( ...)
63
67
{
64
68
class : self . class . name ,
Original file line number Diff line number Diff line change 175
175
expect ( body . inspect ) . to be =~ /\d + chunks, \d + bytes/
176
176
end
177
177
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
178
186
end
Original file line number Diff line number Diff line change 58
58
end
59
59
end
60
60
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
+
61
68
with "#as_json" do
62
69
it "generates a JSON representation" do
63
70
expect ( body . as_json ) . to have_keys (
Original file line number Diff line number Diff line change @@ -29,6 +29,13 @@ def initialize(body)
29
29
end
30
30
end
31
31
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
+
32
39
with '#buffered!' do
33
40
it 'buffers the body' do
34
41
expect ( reader . buffered! ) . to be_equal ( reader )
Original file line number Diff line number Diff line change 104
104
body . call ( stream )
105
105
end
106
106
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
107
114
end
You can’t perform that action at this time.
0 commit comments