Skip to content

Commit f69406b

Browse files
committed
Add a test to catch regressions for render_to_string to not override subsequent render
1 parent 2344463 commit f69406b

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

actionpack/test/controller/render_json_test.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ def render_json_nil
2828
render json: nil
2929
end
3030

31-
def render_json_render_to_string
32-
render plain: render_to_string(json: "[]")
33-
end
34-
3531
def render_json_hello_world
3632
render json: ActiveSupport::JSON.encode(hello: "world")
3733
end
@@ -82,11 +78,6 @@ def test_render_json_nil
8278
assert_equal "application/json", @response.media_type
8379
end
8480

85-
def test_render_json_render_to_string
86-
get :render_json_render_to_string
87-
assert_equal "[]", @response.body
88-
end
89-
9081
def test_render_json
9182
get :render_json_hello_world
9283
assert_equal '{"hello":"world"}', @response.body
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
require "abstract_unit"
4+
require "controller/fake_models"
5+
require "active_support/logger"
6+
7+
class RenderToStringTest < ActionController::TestCase
8+
class TestController < ActionController::Base
9+
protect_from_forgery
10+
11+
def self.controller_path
12+
"test"
13+
end
14+
15+
def render_plain_text_response_with_inline_template
16+
render plain: render_to_string(inline: "hello")
17+
end
18+
19+
def render_json_response_with_partial
20+
render json: { hello: render_to_string(partial: "partial") }
21+
end
22+
23+
def render_json_render_to_string
24+
render plain: render_to_string(json: "[]")
25+
end
26+
27+
def test_render_json_render_to_string
28+
get :render_json_render_to_string
29+
assert_equal "[]", @response.body
30+
end
31+
32+
def render_plain_text_response_with_inline_template_and_xml_format
33+
render_to_string(inline: "<language>Ruby</language>", formats: [:xml])
34+
render plain: "Hello"
35+
end
36+
37+
def render_head_ok_with_inline_template_and_xml_format
38+
render_to_string(inline: "<language>Ruby</language>", formats: [:xml])
39+
head :ok
40+
end
41+
end
42+
43+
tests TestController
44+
45+
def test_render_plain_text_response
46+
get :render_plain_text_response_with_inline_template
47+
assert_equal "hello", @response.body
48+
assert_equal "text/plain", @response.media_type
49+
end
50+
51+
def test_render_json_response
52+
get :render_json_response_with_partial
53+
assert_equal '{"hello":"partial html"}', @response.body
54+
assert_equal "application/json", @response.media_type
55+
end
56+
57+
def render_json_render_to_string
58+
render plain: render_to_string(json: "[]")
59+
assert_equal "text/plain", @response.media_type
60+
end
61+
62+
def test_response_type_does_not_change_by_render_to_string_with_xml_format
63+
get :render_plain_text_response_with_inline_template_and_xml_format
64+
assert_equal "Hello", @response.body
65+
assert_equal "text/plain", @response.media_type
66+
end
67+
68+
def test_response_ok_for_render_to_string_with_xml_format
69+
get :render_head_ok_with_inline_template_and_xml_format
70+
assert_equal "text/html", @response.media_type
71+
assert_response :ok
72+
end
73+
end

0 commit comments

Comments
 (0)