Skip to content

Commit b206e2f

Browse files
authored
Merge pull request rails#41735 from ags/private-no-store-cache-control
Allow 'private, no-store' Cache-Control header
2 parents 725f8f4 + 0680658 commit b206e2f

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

actionpack/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Add support for 'private, no-store' Cache-Control headers.
2+
3+
Previously, 'no-store' was exclusive; no other directives could be specified.
4+
5+
*Alex Smith*
6+
17
* Expand payload of `unpermitted_parameters.action_controller` instrumentation to allow subscribers to
28
know which controller action received unpermitted parameters.
39

actionpack/lib/action_dispatch/http/cache.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,31 +195,30 @@ def merge_and_normalize_cache_control!(cache_control)
195195

196196
control.merge! cache_control
197197

198+
options = []
199+
198200
if control[:no_store]
199-
self._cache_control = NO_STORE
201+
options << PRIVATE if control[:private]
202+
options << NO_STORE
200203
elsif control[:no_cache]
201-
options = []
202204
options << PUBLIC if control[:public]
203205
options << NO_CACHE
204206
options.concat(control[:extras]) if control[:extras]
205-
206-
self._cache_control = options.join(", ")
207207
else
208208
extras = control[:extras]
209209
max_age = control[:max_age]
210210
stale_while_revalidate = control[:stale_while_revalidate]
211211
stale_if_error = control[:stale_if_error]
212212

213-
options = []
214213
options << "max-age=#{max_age.to_i}" if max_age
215214
options << (control[:public] ? PUBLIC : PRIVATE)
216215
options << MUST_REVALIDATE if control[:must_revalidate]
217216
options << "stale-while-revalidate=#{stale_while_revalidate.to_i}" if stale_while_revalidate
218217
options << "stale-if-error=#{stale_if_error.to_i}" if stale_if_error
219218
options.concat(extras) if extras
220-
221-
self._cache_control = options.join(", ")
222219
end
220+
221+
self._cache_control = options.join(", ")
223222
end
224223
end
225224
end

actionpack/test/dispatch/live_response_test.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,24 @@ def test_cache_control_is_set_manually
6262
assert_equal "public", @response.headers["Cache-Control"]
6363
end
6464

65+
def test_cache_control_no_store_default_standalone
66+
@response.set_header("Cache-Control", "no-store")
67+
@response.stream.write "omg"
68+
assert_equal "no-store", @response.headers["Cache-Control"]
69+
end
70+
6571
def test_cache_control_no_store_is_respected
66-
@response.set_header("Cache-Control", "private, no-store")
72+
@response.set_header("Cache-Control", "public, no-store")
6773
@response.stream.write "omg"
6874
assert_equal "no-store", @response.headers["Cache-Control"]
6975
end
7076

77+
def test_cache_control_no_store_private
78+
@response.set_header("Cache-Control", "private, no-store")
79+
@response.stream.write "omg"
80+
assert_equal "private, no-store", @response.headers["Cache-Control"]
81+
end
82+
7183
def test_content_length_is_removed
7284
@response.headers["Content-Length"] = "1234"
7385
@response.stream.write "omg"

actionpack/test/dispatch/response_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,17 @@ def test_only_set_charset_still_defaults_to_text_html
306306
assert_equal("no-store", resp.headers["Cache-Control"])
307307
end
308308

309+
test "respect private, no-store cache-control" do
310+
resp = ActionDispatch::Response.new.tap { |response|
311+
response.cache_control[:private] = true
312+
response.cache_control[:no_store] = true
313+
response.body = "Hello"
314+
}
315+
resp.to_a
316+
317+
assert_equal("private, no-store", resp.headers["Cache-Control"])
318+
end
319+
309320
test "read content type with default charset utf-8" do
310321
resp = ActionDispatch::Response.new(200, "Content-Type" => "text/xml")
311322
assert_equal("utf-8", resp.charset)

0 commit comments

Comments
 (0)