@@ -108,18 +108,17 @@ class RendererTest < ActiveSupport::TestCase
108108 html = "Hello world!"
109109 xml = "<p>Hello world!</p>\n "
110110
111- assert_equal html , render [ "respond_to/using_defaults" ]
112- assert_equal xml , render [ "respond_to/using_defaults" , formats : :xml ]
111+ assert_equal html , render ( "respond_to/using_defaults" )
112+ assert_equal xml , render ( "respond_to/using_defaults" , formats : :xml )
113113 end
114114
115115 test "rendering with helpers" do
116- assert_equal "<p>1\n <br />2</p>" , render [ inline : '<%= simple_format "1\n2" %>' ]
116+ assert_equal "<p>1\n <br />2</p>" , render ( inline : '<%= simple_format "1\n2" %>' )
117117 end
118118
119119 test "rendering with user specified defaults" do
120- ApplicationController . renderer . defaults . merge! ( hello : "hello" , https : true )
121- renderer = ApplicationController . renderer . new
122- content = renderer . render inline : "<%= request.ssl? %>"
120+ renderer . defaults . merge! ( hello : "hello" , https : true )
121+ content = renderer . new . render inline : "<%= request.ssl? %>"
123122
124123 assert_equal "true" , content
125124 end
@@ -138,8 +137,81 @@ class RendererTest < ActiveSupport::TestCase
138137 assert_equal "https://example.org/asset.jpg" , content
139138 end
140139
140+ test "uses default_url_options from the controller's routes when env[:http_host] not specified" do
141+ with_default_url_options (
142+ protocol : "https" ,
143+ host : "foo.example.com" ,
144+ port : 9001 ,
145+ script_name : "/bar" ,
146+ ) do
147+ assert_equal "https://foo.example.com:9001/bar/posts" , render_url_for ( controller : :posts )
148+ end
149+ end
150+
151+ test "uses config.force_ssl when env[:http_host] not specified" do
152+ with_default_url_options ( host : "foo.example.com" ) do
153+ with_force_ssl do
154+ assert_equal "https://foo.example.com/posts" , render_url_for ( controller : :posts )
155+ end
156+ end
157+ end
158+
159+ test "can specify env[:https] when using default_url_options" do
160+ with_default_url_options ( host : "foo.example.com" ) do
161+ @renderer = renderer . new ( https : true )
162+ assert_equal "https://foo.example.com/posts" , render_url_for ( controller : :posts )
163+ end
164+ end
165+
166+ test "env[:https] overrides default_url_options[:protocol]" do
167+ with_default_url_options ( host : "foo.example.com" , protocol : "https" ) do
168+ @renderer = renderer . new ( https : false )
169+ assert_equal "http://foo.example.com/posts" , render_url_for ( controller : :posts )
170+ end
171+ end
172+
173+ test "can specify env[:script_name] when using default_url_options" do
174+ with_default_url_options ( host : "foo.example.com" ) do
175+ @renderer = renderer . new ( script_name : "/bar" )
176+ assert_equal "http://foo.example.com/bar/posts" , render_url_for ( controller : :posts )
177+ end
178+ end
179+
180+ test "env[:script_name] overrides default_url_options[:script_name]" do
181+ with_default_url_options ( host : "foo.example.com" , script_name : "/bar" ) do
182+ @renderer = renderer . new ( script_name : "" )
183+ assert_equal "http://foo.example.com/posts" , render_url_for ( controller : :posts )
184+ end
185+ end
186+
141187 private
142- def render
143- @render ||= ApplicationController . renderer . method ( :render )
188+ def renderer
189+ @renderer ||= ApplicationController . renderer . new
190+ end
191+
192+ def render ( ...)
193+ renderer . render ( ...)
194+ end
195+
196+ def render_url_for ( *args )
197+ render inline : "<%= full_url_for(*#{ args . inspect } ) %>"
198+ end
199+
200+ def with_default_url_options ( default_url_options )
201+ original_default_url_options = renderer . controller . _routes . default_url_options
202+ renderer . controller . _routes . default_url_options = default_url_options
203+ yield
204+ ensure
205+ renderer . controller . _routes . default_url_options = original_default_url_options
206+ renderer . controller . _routes . default_env # refresh
207+ end
208+
209+ def with_force_ssl ( force_ssl = true )
210+ # In a real app, an initializer will set `URL.secure_protocol = app.config.force_ssl`.
211+ original_secure_protocol = ActionDispatch ::Http ::URL . secure_protocol
212+ ActionDispatch ::Http ::URL . secure_protocol = force_ssl
213+ yield
214+ ensure
215+ ActionDispatch ::Http ::URL . secure_protocol = original_secure_protocol
144216 end
145217end
0 commit comments