2016-08-06 13:16:09 -04:00
|
|
|
require "isolation/abstract_unit"
|
|
|
|
require "rack/test"
|
|
|
|
require "minitest/mock"
|
2015-05-30 17:11:49 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "action_view"
|
|
|
|
require "active_support/testing/method_call_assertions"
|
2015-05-30 17:11:49 -04:00
|
|
|
|
|
|
|
class PerRequestDigestCacheTest < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
include ActiveSupport::Testing::MethodCallAssertions
|
|
|
|
include Rack::Test::Methods
|
|
|
|
|
|
|
|
setup do
|
|
|
|
build_app
|
2016-08-06 13:16:09 -04:00
|
|
|
add_to_config "config.consider_all_requests_local = true"
|
2015-05-30 17:11:49 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "app/models/customer.rb", <<-RUBY
|
2015-05-30 17:11:49 -04:00
|
|
|
class Customer < Struct.new(:name, :id)
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
include ActiveModel::Conversion
|
2017-05-18 12:12:32 -04:00
|
|
|
|
|
|
|
def cache_key
|
|
|
|
[ name, id ].join("/")
|
|
|
|
end
|
2015-05-30 17:11:49 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "config/routes.rb", <<-RUBY
|
2015-05-30 17:11:49 -04:00
|
|
|
Rails.application.routes.draw do
|
|
|
|
resources :customers, only: :index
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "app/controllers/customers_controller.rb", <<-RUBY
|
2015-05-30 17:11:49 -04:00
|
|
|
class CustomersController < ApplicationController
|
2016-02-15 16:47:44 -05:00
|
|
|
self.perform_caching = true
|
|
|
|
|
2015-05-30 17:11:49 -04:00
|
|
|
def index
|
|
|
|
render [ Customer.new('david', 1), Customer.new('dingus', 2) ]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "app/views/customers/_customer.html.erb", <<-RUBY
|
2015-05-30 17:11:49 -04:00
|
|
|
<% cache customer do %>
|
|
|
|
<%= customer.name %>
|
|
|
|
<% end %>
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown :teardown_app
|
|
|
|
|
|
|
|
test "digests are reused when rendering the same template twice" do
|
2016-08-06 13:16:09 -04:00
|
|
|
get "/customers"
|
2015-05-30 17:11:49 -04:00
|
|
|
assert_equal 200, last_response.status
|
|
|
|
|
2016-02-18 11:23:26 -05:00
|
|
|
values = ActionView::LookupContext::DetailsKey.digest_caches.first.values
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal [ "8ba099b7749542fe765ff34a6824d548" ], values
|
2015-05-30 17:11:49 -04:00
|
|
|
assert_equal %w(david dingus), last_response.body.split.map(&:strip)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "template digests are cleared before a request" do
|
2016-02-18 11:23:26 -05:00
|
|
|
assert_called(ActionView::LookupContext::DetailsKey, :clear) do
|
2016-08-06 13:16:09 -04:00
|
|
|
get "/customers"
|
2015-05-30 17:11:49 -04:00
|
|
|
assert_equal 200, last_response.status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|