Permalink
Browse files

Allow DateTime instances to be passed to last_modified. Makes sure th…

…e value is always a string, so Rack::Lint will not complain. Fixes GH #16
  • Loading branch information...
1 parent cf3c218 commit 9cf834af67965ea97261e8ed6f6d6ac79314bd34 @rkh committed Sep 1, 2010
Showing with 36 additions and 30 deletions.
  1. +2 −1 lib/sinatra/base.rb
  2. +34 −29 test/helpers_test.rb
View
@@ -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
View
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/helper'
+require 'date'
class HelpersTest < Test::Unit::TestCase
def test_default
@@ -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

0 comments on commit 9cf834a

Please sign in to comment.