1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/controller/new_base/render_body_test.rb
Prem Sichanugrist 103e18c87d 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.
2014-02-18 12:08:36 -05:00

175 lines
4.3 KiB
Ruby

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