File tree 6 files changed +37
-2
lines changed
6 files changed +37
-2
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
@@ -91,7 +93,7 @@ def read
91
93
end
92
94
93
95
def discard
94
- clear
96
+ self . close
95
97
end
96
98
97
99
def write ( chunk )
Original file line number Diff line number Diff line change @@ -133,9 +133,13 @@ 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.
136
141
def discard
137
142
while chunk = self . read
138
- chunk . clear
139
143
end
140
144
end
141
145
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