Skip to content

Commit

Permalink
Introduce render :body for render raw content
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sikachu committed Feb 18, 2014
1 parent 6c496a6 commit 103e18c
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 11 deletions.
12 changes: 12 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
* 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.

Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/abstract_controller/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
17 changes: 13 additions & 4 deletions actionpack/lib/action_controller/metal/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
175 changes: 175 additions & 0 deletions actionpack/test/controller/new_base/render_body_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
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
8 changes: 8 additions & 0 deletions actionpack/test/dispatch/response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions actionview/lib/action_view/helpers/rendering_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/layouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions actionview/lib/action_view/renderer/template_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 103e18c

Please sign in to comment.