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
|
@ -249,8 +249,9 @@ module Sinatra
|
||||||
def last_modified(time)
|
def last_modified(time)
|
||||||
return unless time
|
return unless time
|
||||||
time = time.to_time if time.respond_to?(:to_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)
|
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']
|
halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE']
|
||||||
time
|
time
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require File.dirname(__FILE__) + '/helper'
|
require File.dirname(__FILE__) + '/helper'
|
||||||
|
require 'date'
|
||||||
|
|
||||||
class HelpersTest < Test::Unit::TestCase
|
class HelpersTest < Test::Unit::TestCase
|
||||||
def test_default
|
def test_default
|
||||||
|
@ -423,43 +424,47 @@ class HelpersTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'last_modified' do
|
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
|
it 'ignores nil' do
|
||||||
mock_app {
|
mock_app do
|
||||||
get '/' do last_modified nil; 200; end
|
get '/' do last_modified nil; 200; end
|
||||||
}
|
end
|
||||||
|
|
||||||
get '/'
|
get '/'
|
||||||
assert ! response['Last-Modified']
|
assert ! response['Last-Modified']
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe 'etag' do
|
describe 'etag' do
|
||||||
|
|
Loading…
Reference in New Issue