Skip to content

Commit d97698b

Browse files
Fix streaming body close.
1 parent 33d42cb commit d97698b

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

lib/protocol/rack/body/enumerable.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ def ready?
6969
#
7070
# @parameter error [Exception] Optional error that occurred during processing.
7171
def close(error = nil)
72-
if @body and @body.respond_to?(:close)
73-
@body.close
74-
end
75-
76-
@body = nil
7772
@chunks = nil
7873

74+
if body = @body
75+
@body = nil
76+
if body.respond_to?(:close)
77+
body.close
78+
end
79+
end
80+
7981
super
8082
end
8183

lib/protocol/rack/body/streaming.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,24 @@
88
module Protocol
99
module Rack
1010
module Body
11-
Streaming = ::Protocol::HTTP::Body::Streamable::ResponseBody
11+
class Streaming < ::Protocol::HTTP::Body::Streamable::ResponseBody
12+
def initialize(body, input = nil)
13+
@body = body
14+
15+
super
16+
end
17+
18+
def close(error = nil)
19+
if body = @body
20+
@body = nil
21+
if body.respond_to?(:close)
22+
body.close
23+
end
24+
end
25+
26+
super
27+
end
28+
end
1229
end
1330
end
1431
end

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Fix missing `body#close` for streaming bodies.
6+
37
## v0.21.0
48

59
- For the purpose of constructing the rack request environment, trailers are ignored.

test/protocol/rack/body/streaming.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,29 @@
5050
expect(body.read).to be == "Hello"
5151
end
5252
end
53+
54+
with "#close" do
55+
it "closes the wrapped body if it responds to close" do
56+
close_called = false
57+
wrapped_body = Object.new
58+
wrapped_body.define_singleton_method(:close) do
59+
close_called = true
60+
end
61+
wrapped_body.define_singleton_method(:call) do |stream|
62+
stream.write("Hello")
63+
end
64+
65+
body = subject.new(wrapped_body)
66+
body.close
67+
68+
expect(close_called).to be == true
69+
end
70+
71+
it "does not fail if wrapped body does not respond to close" do
72+
wrapped_body = proc { |stream| stream.write("Hello") }
73+
74+
body = subject.new(wrapped_body)
75+
body.close
76+
end
77+
end
5378
end

0 commit comments

Comments
 (0)