Skip to content

Commit

Permalink
Allow DateTime instances to be passed to last_modified. Makes sure th…
Browse files Browse the repository at this point in the history
…e value is always a string, so Rack::Lint will not complain. Fixes GH sinatra#16
  • Loading branch information
rkh committed Sep 1, 2010
1 parent cf3c218 commit 9cf834a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
3 changes: 2 additions & 1 deletion lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ def expires(amount, *values)
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
Expand Down
63 changes: 34 additions & 29 deletions test/helpers_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/helper'
require 'date'

class HelpersTest < Test::Unit::TestCase
def test_default
Expand Down Expand Up @@ -423,42 +424,46 @@ def send_file_app(opts={})
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 'ignores nil' do
mock_app do
get '/' do last_modified nil; 200; end
end

it 'sets the Last-Modified header to a valid RFC 2616 date value' do
get '/'
assert_equal @now.httpdate, response['Last-Modified']
assert ! response['Last-Modified']
end

it 'returns a body when conditional get misses' do
get '/'
assert_equal 200, status
assert_equal 'Boo!', body
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 'halts when a conditional GET matches' do
get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
assert_equal 304, status
assert_equal '', body
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 'ignores nil' do
mock_app {
get '/' do last_modified nil; 200; end
}
it 'returns a body when conditional get misses' do
get '/'
assert_equal 200, status
assert_equal 'Boo!', body
end

get '/'
assert ! response['Last-Modified']
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

Expand Down

0 comments on commit 9cf834a

Please sign in to comment.