1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #43027 from hahmed/ha/add-regression-test-for-render-to-string

Add a test to catch regressions for render_to_string
This commit is contained in:
John Hawthorn 2021-08-23 13:43:21 -07:00 committed by GitHub
commit ea5cf6db7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 9 deletions

View file

@ -28,10 +28,6 @@ class RenderJsonTest < ActionController::TestCase
render json: nil
end
def render_json_render_to_string
render plain: render_to_string(json: "[]")
end
def render_json_hello_world
render json: ActiveSupport::JSON.encode(hello: "world")
end
@ -82,11 +78,6 @@ class RenderJsonTest < ActionController::TestCase
assert_equal "application/json", @response.media_type
end
def test_render_json_render_to_string
get :render_json_render_to_string
assert_equal "[]", @response.body
end
def test_render_json
get :render_json_hello_world
assert_equal '{"hello":"world"}', @response.body

View file

@ -0,0 +1,73 @@
# frozen_string_literal: true
require "abstract_unit"
require "controller/fake_models"
require "active_support/logger"
class RenderToStringTest < ActionController::TestCase
class TestController < ActionController::Base
protect_from_forgery
def self.controller_path
"test"
end
def render_plain_text_response_with_inline_template
render plain: render_to_string(inline: "hello")
end
def render_json_response_with_partial
render json: { hello: render_to_string(partial: "partial") }
end
def render_json_render_to_string
render plain: render_to_string(json: "[]")
end
def test_render_json_render_to_string
get :render_json_render_to_string
assert_equal "[]", @response.body
end
def render_plain_text_response_with_inline_template_and_xml_format
render_to_string(inline: "<language>Ruby</language>", formats: [:xml])
render plain: "Hello"
end
def render_head_ok_with_inline_template_and_xml_format
render_to_string(inline: "<language>Ruby</language>", formats: [:xml])
head :ok
end
end
tests TestController
def test_render_plain_text_response
get :render_plain_text_response_with_inline_template
assert_equal "hello", @response.body
assert_equal "text/plain", @response.media_type
end
def test_render_json_response
get :render_json_response_with_partial
assert_equal '{"hello":"partial html"}', @response.body
assert_equal "application/json", @response.media_type
end
def render_json_render_to_string
render plain: render_to_string(json: "[]")
assert_equal "text/plain", @response.media_type
end
def test_response_type_does_not_change_by_render_to_string_with_xml_format
get :render_plain_text_response_with_inline_template_and_xml_format
assert_equal "Hello", @response.body
assert_equal "text/plain", @response.media_type
end
def test_response_ok_for_render_to_string_with_xml_format
get :render_head_ok_with_inline_template_and_xml_format
assert_equal "text/html", @response.media_type
assert_response :ok
end
end