提交 103e18c8 编写于 作者: P Prem Sichanugrist

Introduce `render :body` for render raw content

This is an option for sending a raw content back to browser. Note that
this rendering option will unset the default content type and does not
include "Content-Type" header back in the response.

You should only use this option if you are expecting the "Content-Type"
header to not be set. More information on "Content-Type" header can be
found on RFC 2616, section 7.2.1.

Please see #12374 for more detail.
上级 6c496a6d
* Introduce `render :body` as an option for sending a raw content back to
browser. Note that this rendering option will unset the default content type
and does not include "Content-Type" header back in the response.
You should only use this option if you are expecting the "Content-Type"
header to not be set. More information on "Content-Type" header can be found
on RFC 2616, section 7.2.1.
Please see #12374 for more detail.
*Prem Sichanugrist*
* Set stream status to 500 (or 400 on BadRequest) when an error is thrown
before commiting.
......
......@@ -23,7 +23,7 @@ module Rendering
def render(*args, &block)
options = _normalize_render(*args, &block)
self.response_body = render_to_body(options)
_process_format(rendered_format) if rendered_format
_process_format(rendered_format, options) if rendered_format
self.response_body
end
......@@ -98,7 +98,7 @@ def _process_options(options)
# Process the rendered format.
# :api: private
def _process_format(format)
def _process_format(format, options = {})
end
# Normalize args and options.
......
......@@ -27,14 +27,19 @@ def render_to_string(*)
end
def render_to_body(options = {})
super || options[:text].presence || ' '
super || options[:body].presence || options[:text].presence || ' '
end
private
def _process_format(format)
def _process_format(format, options = {})
super
self.content_type ||= format.to_s
if options[:body].present?
self.content_type = "none"
self.headers.delete "Content-Type"
end
end
# Normalize arguments by catching blocks and setting them on :update.
......@@ -46,12 +51,16 @@ def _normalize_args(action=nil, options={}, &blk) #:nodoc:
# Normalize both text and status options.
def _normalize_options(options) #:nodoc:
if options.key?(:body) && options[:body].respond_to?(:to_text)
options[:body] = options[:body].to_text
end
if options.key?(:text) && options[:text].respond_to?(:to_text)
options[:text] = options[:text].to_text
end
if options.delete(:nothing) || (options.key?(:text) && options[:text].nil?)
options[:text] = " "
if options.delete(:nothing) || (options.key?(:body) && options[:body].nil?) || (options.key?(:text) && options[:text].nil?)
options[:body] = " "
end
if options[:status]
......
......@@ -288,7 +288,7 @@ def munge_body_object(body)
end
def assign_default_content_type_and_charset!(headers)
return if headers[CONTENT_TYPE].present?
return if headers[CONTENT_TYPE].present? || @content_type == "none"
@content_type ||= Mime::HTML
@charset ||= self.class.default_charset unless @charset == false
......
require 'abstract_unit'
module RenderBody
class MinimalController < ActionController::Metal
include AbstractController::Rendering
include ActionController::Rendering
def index
render body: "Hello World!"
end
end
class SimpleController < ActionController::Base
self.view_paths = [ActionView::FixtureResolver.new]
def index
render body: "hello david"
end
end
class WithLayoutController < ::ApplicationController
self.view_paths = [ActionView::FixtureResolver.new(
"layouts/application.erb" => "<%= yield %>, I'm here!",
"layouts/greetings.erb" => "<%= yield %>, I wish thee well.",
"layouts/ivar.erb" => "<%= yield %>, <%= @ivar %>"
)]
def index
render body: "hello david"
end
def custom_code
render body: "hello world", status: 404
end
def with_custom_code_as_string
render body: "hello world", status: "404 Not Found"
end
def with_nil
render body: nil
end
def with_nil_and_status
render body: nil, status: 403
end
def with_false
render body: false
end
def with_layout_true
render body: "hello world", layout: true
end
def with_layout_false
render body: "hello world", layout: false
end
def with_layout_nil
render body: "hello world", layout: nil
end
def with_custom_layout
render body: "hello world", layout: "greetings"
end
def with_ivar_in_layout
@ivar = "hello world"
render body: "hello world", layout: "ivar"
end
end
class RenderBodyTest < Rack::TestCase
test "rendering body from a minimal controller" do
get "/render_body/minimal/index"
assert_body "Hello World!"
assert_status 200
end
test "rendering body from an action with default options renders the body with the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
get "/render_body/simple"
assert_body "hello david"
assert_status 200
end
end
test "rendering body from an action with default options renders the body without the layout" do
with_routing do |set|
set.draw { get ':controller', action: 'index' }
get "/render_body/with_layout"
assert_body "hello david"
assert_status 200
end
end
test "rendering body, while also providing a custom status code" do
get "/render_body/with_layout/custom_code"
assert_body "hello world"
assert_status 404
end
test "rendering body with nil returns an empty body padded for Safari" do
get "/render_body/with_layout/with_nil"
assert_body " "
assert_status 200
end
test "Rendering body with nil and custom status code returns an empty body padded for Safari and the status" do
get "/render_body/with_layout/with_nil_and_status"
assert_body " "
assert_status 403
end
test "rendering body with false returns the string 'false'" do
get "/render_body/with_layout/with_false"
assert_body "false"
assert_status 200
end
test "rendering body with layout: true" do
get "/render_body/with_layout/with_layout_true"
assert_body "hello world, I'm here!"
assert_status 200
end
test "rendering body with layout: 'greetings'" do
get "/render_body/with_layout/with_custom_layout"
assert_body "hello world, I wish thee well."
assert_status 200
end
test "rendering body with layout: false" do
get "/render_body/with_layout/with_layout_false"
assert_body "hello world"
assert_status 200
end
test "rendering body with layout: nil" do
get "/render_body/with_layout/with_layout_nil"
assert_body "hello world"
assert_status 200
end
test "rendering from minimal controller returns response with no content type" do
get "/render_body/minimal/index"
assert_header_no_content_type
end
test "rendering from normal controller returns response with no content type" do
get "/render_body/simple/index"
assert_header_no_content_type
end
def assert_header_no_content_type
assert_not response.headers.has_key?("Content-Type"),
%(Expect response not to have Content-Type header, got "#{response.headers["Content-Type"]}")
end
end
end
......@@ -235,6 +235,14 @@ def test_response_body_encoding
assert_equal @response.body, body.each.to_a.join
end
end
test "does not add default content-type if Content-Type is none" do
resp = ActionDispatch::Response.new.tap { |response|
response.content_type = 'none'
}
assert_not resp.headers.has_key?('Content-Type')
end
end
class ResponseIntegrationTest < ActionDispatch::IntegrationTest
......
......@@ -12,6 +12,8 @@ module RenderingHelper
# * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
# * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller.
# * <tt>:text</tt> - Renders the text passed in out.
# * <tt>:body</tt> - Renders the text passed in, and does not set content
# type in the response.
#
# If no options hash is passed or :update specified, the default is to render a partial and use the second parameter
# as the locals hash.
......
......@@ -420,7 +420,7 @@ def _default_layout(require_layout = false)
end
def _include_layout?(options)
(options.keys & [:text, :inline, :partial]).empty? || options.key?(:layout)
(options.keys & [:body, :text, :inline, :partial]).empty? || options.key?(:layout)
end
end
end
......@@ -21,7 +21,9 @@ def render(context, options)
def determine_template(options) #:nodoc:
keys = options.fetch(:locals, {}).keys
if options.key?(:text)
if options.key?(:body)
Template::Text.new(options[:body])
elsif options.key?(:text)
Template::Text.new(options[:text], formats.first)
elsif options.key?(:file)
with_fallbacks { find_template(options[:file], nil, false, keys, @details) }
......@@ -35,7 +37,7 @@ def determine_template(options) #:nodoc:
find_template(options[:template], options[:prefixes], false, keys, @details)
end
else
raise ArgumentError, "You invoked render but did not give any of :partial, :template, :inline, :file or :text option."
raise ArgumentError, "You invoked render but did not give any of :partial, :template, :inline, :file, :text or :body option."
end
end
......
......@@ -100,7 +100,7 @@ def _render_template(options) #:nodoc:
end
# Assign the rendered format to lookup context.
def _process_format(format) #:nodoc:
def _process_format(format, options = {}) #:nodoc:
super
lookup_context.formats = [format.to_sym]
lookup_context.rendered_format = lookup_context.formats.first
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册