Skip to content

Commit e3a43ec

Browse files
committed
Merge pull request rails#25817 from javan/fix-namespaced-implicit-render-etag-template-digest
Fix adding implicitly rendered namespaced template digests to ETags
1 parent c9b080d commit e3a43ec

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

actionpack/lib/action_controller/metal/etag_with_template_digest.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def determine_template_etag(options)
4545
# template digest from the ETag.
4646
def pick_template_for_etag(options)
4747
unless options[:template] == false
48-
options[:template] || "#{controller_name}/#{action_name}"
48+
options[:template] || "#{controller_path}/#{action_name}"
4949
end
5050
end
5151

actionpack/test/controller/render_test.rb

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ def empty_action_with_template
4242
end
4343
end
4444

45+
module Namespaced
46+
class ImplicitRenderTestController < ActionController::Base
47+
def hello_world
48+
fresh_when(etag: 'abc')
49+
end
50+
end
51+
end
52+
4553
class TestController < ActionController::Base
4654
protect_from_forgery
4755

@@ -258,6 +266,19 @@ def determine_layout
258266
end
259267
end
260268

269+
module TemplateModificationHelper
270+
private
271+
def modify_template(name)
272+
path = File.expand_path("../../fixtures/#{name}.erb", __FILE__)
273+
original = File.read(path)
274+
File.write(path, "#{original} Modified!")
275+
ActionView::LookupContext::DetailsKey.clear
276+
yield
277+
ensure
278+
File.write(path, original)
279+
end
280+
end
281+
261282
class MetalTestController < ActionController::Metal
262283
include AbstractController::Rendering
263284
include ActionView::Rendering
@@ -487,6 +508,7 @@ def test_last_modified_works_with_less_than_too
487508

488509
class EtagRenderTest < ActionController::TestCase
489510
tests TestControllerWithExtraEtags
511+
include TemplateModificationHelper
490512

491513
def test_strong_etag
492514
@request.if_none_match = strong_etag(['strong', 'ab', :cde, [:f]])
@@ -535,7 +557,7 @@ def test_etag_reflects_template_digest
535557
get :with_template
536558
assert_response :not_modified
537559

538-
modify_template(:hello_world) do
560+
modify_template("test/hello_world") do
539561
request.if_none_match = etag
540562
get :with_template
541563
assert_response :ok
@@ -552,7 +574,7 @@ def test_etag_reflects_implicit_template_digest
552574
get :with_implicit_template
553575
assert_response :not_modified
554576

555-
modify_template(:with_implicit_template) do
577+
modify_template("test/with_implicit_template") do
556578
request.if_none_match = etag
557579
get :with_implicit_template
558580
assert_response :ok
@@ -568,16 +590,28 @@ def weak_etag(record)
568590
def strong_etag(record)
569591
%("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(record))}")
570592
end
593+
end
571594

572-
def modify_template(name)
573-
path = File.expand_path("../../fixtures/test/#{name}.erb", __FILE__)
574-
original = File.read(path)
575-
File.write(path, "#{original} Modified!")
576-
ActionView::LookupContext::DetailsKey.clear
577-
yield
578-
ensure
579-
File.write(path, original)
595+
class NamespacedEtagRenderTest < ActionController::TestCase
596+
tests Namespaced::ImplicitRenderTestController
597+
include TemplateModificationHelper
598+
599+
def test_etag_reflects_template_digest
600+
get :hello_world
601+
assert_response :ok
602+
assert_not_nil etag = @response.etag
603+
604+
request.if_none_match = etag
605+
get :hello_world
606+
assert_response :not_modified
607+
608+
modify_template("namespaced/implicit_render_test/hello_world") do
609+
request.if_none_match = etag
610+
get :hello_world
611+
assert_response :ok
612+
assert_not_equal etag, @response.etag
580613
end
614+
end
581615
end
582616

583617
class MetalRenderTest < ActionController::TestCase
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world!

0 commit comments

Comments
 (0)