Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Return a valid Rack response from bare ActionController::Metal

  • Loading branch information...
commit 1dacc19702f88a18a57615d1a5eeab3d0f5f9007 1 parent edb5991
Carlhuda authored
View
14 actionpack/lib/abstract_controller/rendering.rb
@@ -76,7 +76,7 @@ def render_to_body(options = {})
# :api: plugin
def render_to_string(options={})
_normalize_options(options)
- AbstractController::Rendering.body_to_s(render_to_body(options))
+ render_to_body(options)
end
# Find and renders a template based on the options given.
@@ -90,18 +90,6 @@ def _prefix
controller_path
end
- # Return a string representation of a Rack-compatible response body.
- def self.body_to_s(body)
- if body.respond_to?(:to_str)
- body
- else
- strings = []
- body.each { |part| strings << part.to_s }
- body.close if body.respond_to?(:close)
- strings.join
- end
- end
-
private
# This method should return a hash with assigns.
View
8 actionpack/lib/action_controller/metal.rb
@@ -38,7 +38,8 @@ def controller_name
delegate :session, :to => "@_request"
def initialize(*)
- @_headers = {}
+ @_headers = {"Content-Type" => "text/html"}
+ @_status = 200
super
end
@@ -70,6 +71,11 @@ def status=(status)
@_status = Rack::Utils.status_code(status)
end
+ def response_body=(val)
+ body = val.respond_to?(:each) ? val : [val]
+ super body
+ end
+
# :api: private
def dispatch(name, request)
@_request = request
View
27 actionpack/test/controller/new_base/bare_metal_test.rb
@@ -0,0 +1,27 @@
+require "abstract_unit"
+
+module BareMetalTest
+ class BareController < ActionController::Metal
+ def index
+ self.response_body = "Hello world"
+ end
+ end
+
+ class BareTest < ActiveSupport::TestCase
+ test "response body is a Rack-compatible response" do
+ status, headers, body = BareController.action(:index).call(Rack::MockRequest.env_for("/"))
+ assert_equal 200, status
+ string = ""
+
+ body.each do |part|
+ assert part.is_a?(String), "Each part of the body must be a String"
+ string << part
+ end
+
+ assert headers.is_a?(Hash), "Headers must be a Hash"
+ assert headers["Content-Type"], "Content-Type must exist"
+
+ assert_equal "Hello world", string
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.