From f69406b3fe79b8adf8799e53e5ce231f70639112 Mon Sep 17 00:00:00 2001 From: Haroon Ahmed Date: Mon, 16 Aug 2021 23:15:30 +0100 Subject: [PATCH] Add a test to catch regressions for render_to_string to not override subsequent render --- .../test/controller/render_json_test.rb | 9 --- .../test/controller/render_to_string_test.rb | 73 +++++++++++++++++++ 2 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 actionpack/test/controller/render_to_string_test.rb diff --git a/actionpack/test/controller/render_json_test.rb b/actionpack/test/controller/render_json_test.rb index 82c6aaafe5..87148cecd6 100644 --- a/actionpack/test/controller/render_json_test.rb +++ b/actionpack/test/controller/render_json_test.rb @@ -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 diff --git a/actionpack/test/controller/render_to_string_test.rb b/actionpack/test/controller/render_to_string_test.rb new file mode 100644 index 0000000000..c017b32d27 --- /dev/null +++ b/actionpack/test/controller/render_to_string_test.rb @@ -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: "Ruby", formats: [:xml]) + render plain: "Hello" + end + + def render_head_ok_with_inline_template_and_xml_format + render_to_string(inline: "Ruby", 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