Skip to content

Commit 33b1976

Browse files
authored
Merge pull request rails#49616 from zzak/49588
Support handling Enumerator for non-buffered responses
2 parents c9da269 + 6635543 commit 33b1976

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

actionpack/lib/action_dispatch/http/response.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ def initialize(response, buf)
104104
end
105105

106106
def to_ary
107-
@buf.to_ary
107+
@buf.respond_to?(:to_ary) ?
108+
@buf.to_ary :
109+
@buf.each
108110
end
109111

110112
def body

actionpack/test/dispatch/response_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,40 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest
617617
assert_equal("text/csv; header=present", @response.media_type)
618618
assert_equal("utf-16", @response.charset)
619619
end
620+
621+
test "response body with enumerator" do
622+
@app = lambda { |env|
623+
[
624+
200,
625+
{ "Content-Type" => "text/plain" },
626+
Enumerator.new { |enumerator| 10.times { |n| enumerator << n.to_s } }
627+
]
628+
}
629+
get "/"
630+
assert_response :success
631+
632+
assert_equal("text/plain", @response.headers["Content-Type"])
633+
assert_equal("text/plain", @response.content_type)
634+
assert_equal("text/plain", @response.media_type)
635+
assert_equal("utf-8", @response.charset)
636+
assert_equal("0123456789", @response.body)
637+
end
638+
639+
test "response body with lazy enumerator" do
640+
@app = lambda { |env|
641+
[
642+
200,
643+
{ "Content-Type" => "text/plain" },
644+
(0..10).lazy
645+
]
646+
}
647+
get "/"
648+
assert_response :success
649+
650+
assert_equal("text/plain", @response.headers["Content-Type"])
651+
assert_equal("text/plain", @response.content_type)
652+
assert_equal("text/plain", @response.media_type)
653+
assert_equal("utf-8", @response.charset)
654+
assert_equal("012345678910", @response.body)
655+
end
620656
end

0 commit comments

Comments
 (0)