aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/response_test.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-05-06 14:22:05 -0500
committerSean Griffin <sean@seantheprogrammer.com>2016-05-06 14:22:05 -0500
commit21a3b180f11e49e1cd296aacba3bec8120dc7c2e (patch)
tree80053ca78b371d2f9647626c38d328dcb8a4f302 /actionpack/test/dispatch/response_test.rb
parent40d5c3370f8e5e0d3d03a7a25a64fde5122a069e (diff)
parentb43158afba7fb4de6a3530d4f4e940f0c89bd057 (diff)
downloadrails-21a3b180f11e49e1cd296aacba3bec8120dc7c2e.tar.gz
rails-21a3b180f11e49e1cd296aacba3bec8120dc7c2e.tar.bz2
rails-21a3b180f11e49e1cd296aacba3bec8120dc7c2e.zip
Merge pull request #24029 from rthbound/dont-call-each-when-calling-body-on-response
Dont call each when calling body on response to fix #23964 Fixes #23964
Diffstat (limited to 'actionpack/test/dispatch/response_test.rb')
-rw-r--r--actionpack/test/dispatch/response_test.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/actionpack/test/dispatch/response_test.rb b/actionpack/test/dispatch/response_test.rb
index 658e0d004b..aa90433505 100644
--- a/actionpack/test/dispatch/response_test.rb
+++ b/actionpack/test/dispatch/response_test.rb
@@ -37,6 +37,39 @@ class ResponseTest < ActiveSupport::TestCase
assert_equal "closed stream", e.message
end
+ def test_each_isnt_called_if_str_body_is_written
+ # Controller writes and reads response body
+ each_counter = 0
+ @response.body = Object.new.tap {|o| o.singleton_class.send(:define_method, :each) { |&block| each_counter += 1; block.call 'foo' } }
+ @response['X-Foo'] = @response.body
+
+ assert_equal 1, each_counter, "#each was not called once"
+
+ # Build response
+ status, headers, body = @response.to_a
+
+ assert_equal 200, status
+ assert_equal "foo", headers['X-Foo']
+ assert_equal "foo", body.each.to_a.join
+
+ # Show that #each was not called twice
+ assert_equal 1, each_counter, "#each was not called once"
+ end
+
+ def test_set_header_after_read_body_during_action
+ @response.body
+
+ # set header after the action reads back @response.body
+ @response['x-header'] = "Best of all possible worlds."
+
+ # the response can be built.
+ status, headers, body = @response.to_a
+ assert_equal 200, status
+ assert_equal "", body.body
+
+ assert_equal "Best of all possible worlds.", headers['x-header']
+ end
+
def test_read_body_during_action
@response.body = "Hello, World!"