mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Allow DateTime instances to be passed to last_modified. Makes sure the value is always a string, so Rack::Lint will not complain. Fixes GH #16
This commit is contained in:
parent
cf3c218a6c
commit
9cf834af67
2 changed files with 38 additions and 32 deletions
|
@ -249,8 +249,9 @@ module Sinatra
|
|||
def last_modified(time)
|
||||
return unless time
|
||||
time = time.to_time if time.respond_to?(:to_time)
|
||||
time = Time.parse time.strftime('%FT%T%:z') if time.respond_to?(:strftime)
|
||||
time = time.httpdate if time.respond_to?(:httpdate)
|
||||
response['Last-Modified'] = time
|
||||
response['Last-Modified'] = time.to_s
|
||||
halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE']
|
||||
time
|
||||
end
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
require 'date'
|
||||
|
||||
class HelpersTest < Test::Unit::TestCase
|
||||
def test_default
|
||||
|
@ -423,43 +424,47 @@ class HelpersTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
describe 'last_modified' do
|
||||
setup do
|
||||
now = Time.now
|
||||
mock_app {
|
||||
get '/' do
|
||||
body { 'Hello World' }
|
||||
last_modified now
|
||||
'Boo!'
|
||||
end
|
||||
}
|
||||
@now = now
|
||||
end
|
||||
|
||||
it 'sets the Last-Modified header to a valid RFC 2616 date value' do
|
||||
get '/'
|
||||
assert_equal @now.httpdate, response['Last-Modified']
|
||||
end
|
||||
|
||||
it 'returns a body when conditional get misses' do
|
||||
get '/'
|
||||
assert_equal 200, status
|
||||
assert_equal 'Boo!', body
|
||||
end
|
||||
|
||||
it 'halts when a conditional GET matches' do
|
||||
get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
|
||||
assert_equal 304, status
|
||||
assert_equal '', body
|
||||
end
|
||||
|
||||
it 'ignores nil' do
|
||||
mock_app {
|
||||
mock_app do
|
||||
get '/' do last_modified nil; 200; end
|
||||
}
|
||||
end
|
||||
|
||||
get '/'
|
||||
assert ! response['Last-Modified']
|
||||
end
|
||||
|
||||
[Time, DateTime].each do |klass|
|
||||
describe "with #{klass.name}" do
|
||||
setup do
|
||||
now = klass.now
|
||||
mock_app do
|
||||
get '/' do
|
||||
body { 'Hello World' }
|
||||
last_modified now
|
||||
'Boo!'
|
||||
end
|
||||
end
|
||||
@now = Time.parse now.to_s
|
||||
end
|
||||
|
||||
it 'sets the Last-Modified header to a valid RFC 2616 date value' do
|
||||
get '/'
|
||||
assert_equal @now.httpdate, response['Last-Modified']
|
||||
end
|
||||
|
||||
it 'returns a body when conditional get misses' do
|
||||
get '/'
|
||||
assert_equal 200, status
|
||||
assert_equal 'Boo!', body
|
||||
end
|
||||
|
||||
it 'halts when a conditional GET matches' do
|
||||
get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
|
||||
assert_equal 304, status
|
||||
assert_equal '', body
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'etag' do
|
||||
|
|
Loading…
Reference in a new issue