Allow max-age value to be specified separately from expires

This commit is contained in:
Jordan Owens 2022-07-17 19:58:47 -04:00
parent c149c2ad6d
commit 1a3c7369b8
2 changed files with 9 additions and 1 deletions

View File

@ -534,7 +534,7 @@ module Sinatra
max_age = time - Time.now max_age = time - Time.now
end end
values.last.merge!(:max_age => max_age) values.last.merge!(:max_age => max_age) { |key, v1, v2| v1 || v2 }
cache_control(*values) cache_control(*values)
response['Expires'] = time.httpdate response['Expires'] = time.httpdate

View File

@ -979,6 +979,8 @@ class HelpersTest < Minitest::Test
get('/baz') { expires Time.at(0) } get('/baz') { expires Time.at(0) }
get('/bah') { expires Time.at(0), :max_age => 20 }
get('/blah') do get('/blah') do
obj = Object.new obj = Object.new
def obj.method_missing(*a, &b) 60.send(*a, &b) end def obj.method_missing(*a, &b) 60.send(*a, &b) end
@ -1011,6 +1013,12 @@ class HelpersTest < Minitest::Test
assert_equal 'Thu, 01 Jan 1970 00:00:00 GMT', response['Expires'] assert_equal 'Thu, 01 Jan 1970 00:00:00 GMT', response['Expires']
end end
it 'allows max_age to be specified separately' do
get '/bah'
assert_equal 'Thu, 01 Jan 1970 00:00:00 GMT', response['Expires']
assert_equal ['max-age=20'], response['Cache-Control'].split(', ')
end
it 'accepts values pretending to be a Numeric (like ActiveSupport::Duration)' do it 'accepts values pretending to be a Numeric (like ActiveSupport::Duration)' do
get '/blah' get '/blah'
assert_equal ['public', 'no-cache', 'max-age=60'], response['Cache-Control'].split(', ') assert_equal ['public', 'no-cache', 'max-age=60'], response['Cache-Control'].split(', ')