Skip to content

Commit

Permalink
Merge pull request #25817 from javan/fix-namespaced-implicit-render-e…
Browse files Browse the repository at this point in the history
…tag-template-digest

Fix adding implicitly rendered namespaced template digests to ETags
  • Loading branch information
rafaelfranca committed Jul 13, 2016
2 parents 8592593 + 03efd17 commit 853a041
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
Expand Up @@ -45,7 +45,7 @@ def determine_template_etag(options)
# template digest from the ETag.
def pick_template_for_etag(options)
unless options[:template] == false
options[:template] || "#{controller_name}/#{action_name}"
options[:template] || "#{controller_path}/#{action_name}"
end
end

Expand Down
54 changes: 44 additions & 10 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -42,6 +42,14 @@ def empty_action_with_template
end
end

module Namespaced
class ImplicitRenderTestController < ActionController::Base
def hello_world
fresh_when(etag: 'abc')
end
end
end

class TestController < ActionController::Base
protect_from_forgery

Expand Down Expand Up @@ -258,6 +266,19 @@ def determine_layout
end
end

module TemplateModificationHelper
private
def modify_template(name)
path = File.expand_path("../../fixtures/#{name}.erb", __FILE__)
original = File.read(path)
File.write(path, "#{original} Modified!")
ActionView::LookupContext::DetailsKey.clear
yield
ensure
File.write(path, original)
end
end

class MetalTestController < ActionController::Metal
include AbstractController::Rendering
include ActionView::Rendering
Expand Down Expand Up @@ -487,6 +508,7 @@ def test_last_modified_works_with_less_than_too

class EtagRenderTest < ActionController::TestCase
tests TestControllerWithExtraEtags
include TemplateModificationHelper

def test_strong_etag
@request.if_none_match = strong_etag(['strong', 'ab', :cde, [:f]])
Expand Down Expand Up @@ -535,7 +557,7 @@ def test_etag_reflects_template_digest
get :with_template
assert_response :not_modified

modify_template(:hello_world) do
modify_template("test/hello_world") do
request.if_none_match = etag
get :with_template
assert_response :ok
Expand All @@ -552,7 +574,7 @@ def test_etag_reflects_implicit_template_digest
get :with_implicit_template
assert_response :not_modified

modify_template(:with_implicit_template) do
modify_template("test/with_implicit_template") do
request.if_none_match = etag
get :with_implicit_template
assert_response :ok
Expand All @@ -568,16 +590,28 @@ def weak_etag(record)
def strong_etag(record)
%("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(record))}")
end
end

def modify_template(name)
path = File.expand_path("../../fixtures/test/#{name}.erb", __FILE__)
original = File.read(path)
File.write(path, "#{original} Modified!")
ActionView::LookupContext::DetailsKey.clear
yield
ensure
File.write(path, original)
class NamespacedEtagRenderTest < ActionController::TestCase
tests Namespaced::ImplicitRenderTestController
include TemplateModificationHelper

def test_etag_reflects_template_digest
get :hello_world
assert_response :ok
assert_not_nil etag = @response.etag

request.if_none_match = etag
get :hello_world
assert_response :not_modified

modify_template("namespaced/implicit_render_test/hello_world") do
request.if_none_match = etag
get :hello_world
assert_response :ok
assert_not_equal etag, @response.etag
end
end
end

class MetalRenderTest < ActionController::TestCase
Expand Down
@@ -0,0 +1 @@
Hello world!

0 comments on commit 853a041

Please sign in to comment.