From 0ed321b09408dba82fcf5bfd2be62b6556faee38 Mon Sep 17 00:00:00 2001 From: Yohei Kitamura Date: Thu, 8 Oct 2020 14:02:12 -0400 Subject: [PATCH] Reset @cache_control in Response when duplicating its instance --- lib/rack/cache/response.rb | 1 + test/context_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/rack/cache/response.rb b/lib/rack/cache/response.rb index 443be06..eff8da3 100644 --- a/lib/rack/cache/response.rb +++ b/lib/rack/cache/response.rb @@ -40,6 +40,7 @@ def initialize(status, headers, body) def initialize_copy(other) super @headers = other.headers.dup + @cache_control = nil end # Return the status, headers, and body in a three-tuple. diff --git a/test/context_test.rb b/test/context_test.rb index 7351b75..8252483 100644 --- a/test/context_test.rb +++ b/test/context_test.rb @@ -992,6 +992,29 @@ cache.trace.must_include :pass end + it 'does not cache when Cache-Control response header changed to private (reset @cache_control on dup)' do + count = 0 + respond_with do |req,res| + count+= 1 + res['Cache-Control'] = (count == 1) ? 'public' : 'private, no-store' + res['ETag'] = count.to_s + res.status = (count == 1) ? 200 : 304 + end + + get '/' + assert app.called? + assert response.ok? + cache.trace.must_include :miss + cache.trace.must_include :store + + get '/' + assert app.called? + assert response.ok? + cache.trace.must_include :stale + cache.trace.must_include :valid + cache.trace.wont_include :store + end + it 'logs to rack.logger if available' do logger = Class.new do attr_reader :logged_level