Permalink
Browse files

changes the date comparison in last_modified

last_modified was halting only when the 'If-Modified-Since' header date
was equal to the time specified. Now, it halts when is equal or later
than the time specified.

Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
  • Loading branch information...
gnandretta authored and rkh committed Oct 7, 2010
1 parent e7862ac commit 4c9add8265736c76c26f667c8e81fb37b20aea89
Showing with 69 additions and 19 deletions.
  1. +7 −4 lib/sinatra/base.rb
  2. +62 −15 test/helpers_test.rb
View
@@ -246,16 +246,19 @@ def expires(amount, *values)
# and halt if conditional GET matches. The +time+ argument is a Time,
# DateTime, or other object that responds to +to_time+.
#
- # When the current request includes an 'If-Modified-Since' header that
- # matches the time specified, execution is immediately halted with a
- # '304 Not Modified' response.
+ # When the current request includes an 'If-Modified-Since' header that is
+ # equal or later than the time specified, execution is immediately halted
+ # with a '304 Not Modified' response.
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.to_s
- halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE']
+ begin
+ halt 304 if time <= Time.httpdate(request.env['HTTP_IF_MODIFIED_SINCE']).httpdate
+ rescue ArgumentError
+ end
time
end
View
@@ -472,32 +472,79 @@ def send_file_app(opts={})
[Time, DateTime].each do |klass|
describe "with #{klass.name}" do
setup do
- now = klass.now
+ last_modified_time = klass.now
mock_app do
get '/' do
- body { 'Hello World' }
- last_modified now
+ last_modified last_modified_time
'Boo!'
end
end
- @now = Time.parse now.to_s
+ @last_modified_time = Time.parse last_modified_time.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']
+ context "when there's no If-Modified-Since header" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/'
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
+
+ it 'conditional GET misses and returns a body' do
+ get '/'
+ assert_equal 200, status
+ assert_equal 'Boo!', body
+ end
end
- it 'returns a body when conditional get misses' do
- get '/'
- assert_equal 200, status
- assert_equal 'Boo!', body
+ context "when there's an invalid If-Modified-Since header" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'a really weird date' }
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
+
+ it 'conditional GET misses and returns a body' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'a really weird date' }
+ assert_equal 200, status
+ assert_equal 'Boo!', body
+ end
end
- it 'halts when a conditional GET matches' do
- get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
- assert_equal 304, status
- assert_equal '', body
+ context "when the resource has been modified since the If-Modified-Since header date" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => (@last_modified_time - 1).httpdate }
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
+
+ it 'conditional GET misses and returns a body' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => (@last_modified_time - 1).httpdate }
+ assert_equal 200, status
+ assert_equal 'Boo!', body
+ end
+ end
+
+ context "when the resource has been modified on the exact If-Modified-Since header date" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @last_modified_time.httpdate }
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
+
+ it 'conditional GET matches and halts' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @last_modified_time.httpdate }
+ assert_equal 304, status
+ assert_equal '', body
+ end
+ end
+
+ context "when the resource hasn't been modified since the If-Modified-Since header date" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => (@last_modified_time + 1).httpdate }
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
+
+ it 'conditional GET matches and halts' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => (@last_modified_time + 1).httpdate }
+ assert_equal 304, status
+ assert_equal '', body
+ end
end
end
end

0 comments on commit 4c9add8

Please sign in to comment.