|
| 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