Skip to content

Commit

Permalink
Handle response_body= when body is nil
Browse files Browse the repository at this point in the history
There are some cases when the `body` in `response_body=` can be set to
nil. One of those cases is in `actionpack-action_caching` which I found
while upgrading it for Rails 5.

It's not possible to run `body.each` on a `nil` body so we have to
return after we run `response.reset_body!`.
  • Loading branch information
eileencodes committed Jan 30, 2016
1 parent 6162c49 commit c4d85df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions actionpack/lib/action_controller/metal.rb
Expand Up @@ -174,6 +174,7 @@ def url_for(string)
def response_body=(body)
body = [body] unless body.nil? || body.respond_to?(:each)
response.reset_body!
return unless body
body.each { |part|
next if part.empty?
response.write part
Expand Down
16 changes: 16 additions & 0 deletions actionpack/test/controller/new_base/bare_metal_test.rb
Expand Up @@ -40,6 +40,22 @@ class BareTest < ActiveSupport::TestCase
end
end

class BareEmptyController < ActionController::Metal
def index
self.response_body = nil
end
end

class BareEmptyTest < ActiveSupport::TestCase
test "response body is nil" do
controller = BareEmptyController.new
controller.set_request!(ActionDispatch::Request.empty)
controller.set_response!(BareController.make_response!(controller.request))
controller.index
assert_equal nil, controller.response_body
end
end

class HeadController < ActionController::Metal
include ActionController::Head

Expand Down

0 comments on commit c4d85df

Please sign in to comment.