diff options
author | Matthew Draper <matthew@trebex.net> | 2016-01-06 12:43:40 +1030 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2016-01-12 11:37:42 +1030 |
commit | 272c5838dfed0473acc11a9f97d797180015b242 (patch) | |
tree | 965424fdf3c4c069a6736a37a2c5debef41c5cf1 | |
parent | ddfd51f270a9d915c64e83f73390c8642de44e23 (diff) | |
download | rails-272c5838dfed0473acc11a9f97d797180015b242.tar.gz rails-272c5838dfed0473acc11a9f97d797180015b242.tar.bz2 rails-272c5838dfed0473acc11a9f97d797180015b242.zip |
Commit before freezing the headers
This shouldn't generally come up: under a standard flow, we don't start
sending until after the commit. But application code always finds a way.
-rw-r--r-- | actionpack/lib/action_dispatch/http/cache.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/http/response.rb | 7 | ||||
-rw-r--r-- | actionpack/test/dispatch/response_test.rb | 21 |
3 files changed, 29 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/http/cache.rb b/actionpack/lib/action_dispatch/http/cache.rb index 30ade14c26..842dfa5827 100644 --- a/actionpack/lib/action_dispatch/http/cache.rb +++ b/actionpack/lib/action_dispatch/http/cache.rb @@ -91,7 +91,7 @@ module ActionDispatch DATE = 'Date'.freeze LAST_MODIFIED = "Last-Modified".freeze - SPECIAL_KEYS = Set.new(%w[extras no-cache max-age public must-revalidate]) + SPECIAL_KEYS = Set.new(%w[extras no-cache max-age public private must-revalidate]) def cache_control_segments if cache_control = _cache_control diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index 9b11111a67..b426d272f2 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -412,6 +412,13 @@ module ActionDispatch # :nodoc: end def before_sending + # Normally we've already committed by now, but it's possible + # (e.g., if the controller action tries to read back its own + # response) to get here before that. In that case, we must force + # an "early" commit: we're about to freeze the headers, so this is + # our last chance. + commit! unless committed? + headers.freeze request.commit_cookie_jar! unless committed? end diff --git a/actionpack/test/dispatch/response_test.rb b/actionpack/test/dispatch/response_test.rb index c37679bc5f..43ee2efd9f 100644 --- a/actionpack/test/dispatch/response_test.rb +++ b/actionpack/test/dispatch/response_test.rb @@ -37,6 +37,27 @@ class ResponseTest < ActiveSupport::TestCase assert_equal "closed stream", e.message end + def test_read_body_during_action + @response.body = "Hello, World!" + + # even though there's no explicitly set content-type, + assert_equal nil, @response.content_type + + # after the action reads back @response.body, + assert_equal "Hello, World!", @response.body + + # the response can be built. + status, headers, body = @response.to_a + assert_equal 200, status + assert_equal({ + "Content-Type" => "text/html; charset=utf-8" + }, headers) + + parts = [] + body.each { |part| parts << part } + assert_equal ["Hello, World!"], parts + end + def test_response_body_encoding body = ["hello".encode(Encoding::UTF_8)] response = ActionDispatch::Response.new 200, {}, body |