2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2019-12-09 17:24:44 -05:00
|
|
|
require "test_component"
|
2015-01-17 19:06:10 -05:00
|
|
|
|
|
|
|
class RendererTest < ActiveSupport::TestCase
|
2016-08-06 12:54:50 -04:00
|
|
|
test "action controller base has a renderer" do
|
2015-09-14 18:29:19 -04:00
|
|
|
assert ActionController::Base.renderer
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "creating with a controller" do
|
2015-01-17 19:06:10 -05:00
|
|
|
controller = CommentsController
|
|
|
|
renderer = ActionController::Renderer.for controller
|
|
|
|
|
|
|
|
assert_equal controller, renderer.controller
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "creating from a controller" do
|
2015-01-17 19:06:10 -05:00
|
|
|
controller = AccountsController
|
|
|
|
renderer = controller.renderer
|
|
|
|
|
|
|
|
assert_equal controller, renderer.controller
|
|
|
|
end
|
|
|
|
|
2017-03-08 04:54:52 -05:00
|
|
|
test "creating with new defaults" do
|
|
|
|
renderer = ApplicationController.renderer
|
|
|
|
|
|
|
|
new_defaults = { https: true }
|
|
|
|
new_renderer = renderer.with_defaults(new_defaults).new
|
|
|
|
content = new_renderer.render(inline: "<%= request.ssl? %>")
|
|
|
|
|
|
|
|
assert_equal "true", content
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with a class renderer" do
|
2015-01-17 19:06:10 -05:00
|
|
|
renderer = ApplicationController.renderer
|
2016-08-06 12:54:50 -04:00
|
|
|
content = renderer.render template: "ruby_template"
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "Hello from Ruby code", content
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with an instance renderer" do
|
2015-01-17 19:06:10 -05:00
|
|
|
renderer = ApplicationController.renderer.new
|
2019-03-18 20:17:11 -04:00
|
|
|
content = assert_deprecated { renderer.render file: "test/hello_world" }
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "Hello world!", content
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with a controller class" do
|
|
|
|
assert_equal "Hello world!", ApplicationController.render("test/hello_world")
|
2015-01-21 16:23:22 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with locals" do
|
2015-01-17 19:06:10 -05:00
|
|
|
renderer = ApplicationController.renderer
|
2016-08-06 12:54:50 -04:00
|
|
|
content = renderer.render template: "test/render_file_with_locals",
|
|
|
|
locals: { secret: "bar" }
|
2015-01-17 19:06:10 -05:00
|
|
|
|
|
|
|
assert_equal "The secret is bar\n", content
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with assigns" do
|
2015-01-17 19:06:10 -05:00
|
|
|
renderer = ApplicationController.renderer
|
2016-08-06 12:54:50 -04:00
|
|
|
content = renderer.render template: "test/render_file_with_ivar",
|
|
|
|
assigns: { secret: "foo" }
|
2015-01-17 19:06:10 -05:00
|
|
|
|
|
|
|
assert_equal "The secret is foo\n", content
|
|
|
|
end
|
|
|
|
|
2019-12-09 17:24:44 -05:00
|
|
|
def test_render_component
|
|
|
|
renderer = ApplicationController.renderer
|
|
|
|
|
|
|
|
assert_equal(
|
2020-03-13 18:52:49 -04:00
|
|
|
%(Hello, World!),
|
|
|
|
renderer.render(TestComponent.new)
|
2019-12-09 17:24:44 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with custom env" do
|
|
|
|
renderer = ApplicationController.renderer.new method: "post"
|
|
|
|
content = renderer.render inline: "<%= request.post? %>"
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "true", content
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
2016-10-13 18:53:41 -04:00
|
|
|
test "rendering with custom env using a key that is not in RACK_KEY_TRANSLATION" do
|
|
|
|
value = "warden is here"
|
|
|
|
renderer = ApplicationController.renderer.new warden: value
|
|
|
|
content = renderer.render inline: "<%= request.env['warden'] %>"
|
|
|
|
|
|
|
|
assert_equal value, content
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with defaults" do
|
2015-09-14 18:58:12 -04:00
|
|
|
renderer = ApplicationController.renderer.new https: true
|
2016-08-06 12:54:50 -04:00
|
|
|
content = renderer.render inline: "<%= request.ssl? %>"
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "true", content
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "same defaults from the same controller" do
|
2015-01-31 20:48:55 -05:00
|
|
|
renderer_defaults = ->(controller) { controller.renderer.defaults }
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2015-09-14 18:58:12 -04:00
|
|
|
assert_equal renderer_defaults[AccountsController], renderer_defaults[AccountsController]
|
|
|
|
assert_equal renderer_defaults[AccountsController], renderer_defaults[CommentsController]
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with different formats" do
|
|
|
|
html = "Hello world!"
|
2015-01-17 19:06:10 -05:00
|
|
|
xml = "<p>Hello world!</p>\n"
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal html, render["respond_to/using_defaults"]
|
2020-04-21 18:59:46 -04:00
|
|
|
assert_equal xml, render["respond_to/using_defaults.xml.builder"]
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal xml, render["respond_to/using_defaults", formats: :xml]
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with helpers" do
|
2015-01-17 19:06:10 -05:00
|
|
|
assert_equal "<p>1\n<br />2</p>", render[inline: '<%= simple_format "1\n2" %>']
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "rendering with user specified defaults" do
|
2016-08-06 13:44:11 -04:00
|
|
|
ApplicationController.renderer.defaults.merge!(hello: "hello", https: true)
|
2016-04-20 19:47:16 -04:00
|
|
|
renderer = ApplicationController.renderer.new
|
2016-08-06 12:54:50 -04:00
|
|
|
content = renderer.render inline: "<%= request.ssl? %>"
|
2016-04-20 19:47:16 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "true", content
|
2016-04-20 19:47:16 -04:00
|
|
|
end
|
|
|
|
|
2019-04-01 13:28:02 -04:00
|
|
|
test "return valid asset URL with defaults" do
|
2017-03-02 04:54:58 -05:00
|
|
|
renderer = ApplicationController.renderer
|
|
|
|
content = renderer.render inline: "<%= asset_url 'asset.jpg' %>"
|
|
|
|
|
|
|
|
assert_equal "http://example.org/asset.jpg", content
|
|
|
|
end
|
|
|
|
|
2019-04-01 13:28:02 -04:00
|
|
|
test "return valid asset URL when https is true" do
|
2017-03-02 04:54:58 -05:00
|
|
|
renderer = ApplicationController.renderer.new https: true
|
|
|
|
content = renderer.render inline: "<%= asset_url 'asset.jpg' %>"
|
|
|
|
|
|
|
|
assert_equal "https://example.org/asset.jpg", content
|
|
|
|
end
|
|
|
|
|
2015-01-17 19:06:10 -05:00
|
|
|
private
|
|
|
|
def render
|
|
|
|
@render ||= ApplicationController.renderer.method(:render)
|
|
|
|
end
|
|
|
|
end
|