2006-06-30 16:42:12 -04:00
|
|
|
# Copyright (c) 2005 Zed A. Shaw
|
|
|
|
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
|
|
#
|
|
|
|
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
|
|
|
# for more information.
|
2006-05-21 10:46:42 -04:00
|
|
|
|
2006-05-04 12:14:43 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'net/http'
|
|
|
|
require 'mongrel'
|
|
|
|
|
|
|
|
include Mongrel
|
|
|
|
|
2006-08-07 22:07:24 -04:00
|
|
|
class ConditionalResponseTest < Test::Unit::TestCase
|
2006-05-04 12:14:43 -04:00
|
|
|
def setup
|
2006-05-16 00:26:03 -04:00
|
|
|
@h = HttpServer.new('127.0.0.1', 3501)
|
2006-05-04 12:14:43 -04:00
|
|
|
@h.register('/', Mongrel::DirHandler.new('.'))
|
|
|
|
@h.run
|
2006-05-16 00:26:03 -04:00
|
|
|
|
2006-05-04 12:14:43 -04:00
|
|
|
@http = Net::HTTP.new(@h.host, @h.port)
|
|
|
|
|
|
|
|
# get the ETag and Last-Modified headers
|
|
|
|
@path = '/README'
|
|
|
|
res = @http.start { |http| http.get(@path) }
|
2006-05-13 17:25:53 -04:00
|
|
|
assert_not_nil @etag = res['ETag']
|
|
|
|
assert_not_nil @last_modified = res['Last-Modified']
|
2006-08-03 16:49:11 -04:00
|
|
|
assert_not_nil @content_length = res['Content-Length']
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
orig_stderr = STDERR.dup
|
|
|
|
|
|
|
|
# temporarily disable the puts method in STDERR to silence warnings from stop
|
|
|
|
class << STDERR
|
|
|
|
define_method(:puts) {}
|
|
|
|
end
|
|
|
|
|
|
|
|
@h.stop
|
|
|
|
ensure
|
|
|
|
# restore STDERR
|
|
|
|
STDERR.reopen(orig_stderr)
|
|
|
|
end
|
|
|
|
|
|
|
|
# status should be 304 Not Modified when If-None-Match is the matching ETag
|
|
|
|
def test_not_modified_via_if_none_match
|
|
|
|
assert_status_for_get_and_head Net::HTTPNotModified, 'If-None-Match' => @etag
|
|
|
|
end
|
|
|
|
|
2006-08-03 16:49:11 -04:00
|
|
|
# status should be 304 Not Modified when If-Modified-Since is the matching Last-Modified date
|
|
|
|
def test_not_modified_via_if_modified_since
|
|
|
|
assert_status_for_get_and_head Net::HTTPNotModified, 'If-Modified-Since' => @last_modified
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# status should be 304 Not Modified when If-None-Match is the matching ETag
|
2006-08-03 16:49:11 -04:00
|
|
|
# and If-Modified-Since is the matching Last-Modified date
|
|
|
|
def test_not_modified_via_if_none_match_and_if_modified_since
|
|
|
|
assert_status_for_get_and_head Net::HTTPNotModified, 'If-None-Match' => @etag, 'If-Modified-Since' => @last_modified
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# status should be 200 OK when If-None-Match is invalid
|
|
|
|
def test_invalid_if_none_match
|
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => 'invalid'
|
2006-08-03 16:49:11 -04:00
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => 'invalid', 'If-Modified-Since' => @last_modified
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
|
2006-08-03 16:49:11 -04:00
|
|
|
# status should be 200 OK when If-Modified-Since is invalid
|
|
|
|
def test_invalid_if_modified_since
|
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-Modified-Since' => 'invalid'
|
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => @etag, 'If-Modified-Since' => 'invalid'
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
|
2006-08-03 16:49:11 -04:00
|
|
|
# status should be 304 Not Modified when If-Modified-Since is greater than the Last-Modified header, but less than the system time
|
|
|
|
def test_if_modified_since_greater_than_last_modified
|
2006-05-04 12:14:43 -04:00
|
|
|
sleep 2
|
|
|
|
last_modified_plus_1 = (Time.httpdate(@last_modified) + 1).httpdate
|
2006-08-03 16:49:11 -04:00
|
|
|
assert_status_for_get_and_head Net::HTTPNotModified, 'If-Modified-Since' => last_modified_plus_1
|
|
|
|
assert_status_for_get_and_head Net::HTTPNotModified, 'If-None-Match' => @etag, 'If-Modified-Since' => last_modified_plus_1
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
|
2006-08-03 16:49:11 -04:00
|
|
|
# status should be 200 OK when If-Modified-Since is less than the Last-Modified header
|
|
|
|
def test_if_modified_since_less_than_last_modified
|
2006-05-04 12:14:43 -04:00
|
|
|
last_modified_minus_1 = (Time.httpdate(@last_modified) - 1).httpdate
|
2006-08-03 16:49:11 -04:00
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-Modified-Since' => last_modified_minus_1
|
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => @etag, 'If-Modified-Since' => last_modified_minus_1
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
|
2006-08-03 16:49:11 -04:00
|
|
|
# status should be 200 OK when If-Modified-Since is a date in the future
|
|
|
|
def test_future_if_modified_since
|
2006-05-04 12:14:43 -04:00
|
|
|
the_future = Time.at(2**31-1).httpdate
|
2006-08-03 16:49:11 -04:00
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-Modified-Since' => the_future
|
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => @etag, 'If-Modified-Since' => the_future
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# status should be 200 OK when If-None-Match is a wildcard
|
|
|
|
def test_wildcard_match
|
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => '*'
|
2006-08-03 16:49:11 -04:00
|
|
|
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => '*', 'If-Modified-Since' => @last_modified
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# assert the response status is correct for GET and HEAD
|
2006-08-03 16:49:11 -04:00
|
|
|
def assert_status_for_get_and_head(response_class, headers = {})
|
2006-05-04 12:14:43 -04:00
|
|
|
%w{ get head }.each do |method|
|
2006-05-13 17:25:53 -04:00
|
|
|
res = @http.send(method, @path, headers)
|
2006-08-03 16:49:11 -04:00
|
|
|
assert_kind_of response_class, res
|
2006-05-13 17:25:53 -04:00
|
|
|
assert_equal @etag, res['ETag']
|
2006-08-03 16:49:11 -04:00
|
|
|
case response_class.to_s
|
|
|
|
when 'Net::HTTPNotModified' then
|
|
|
|
assert_nil res['Last-Modified']
|
|
|
|
assert_nil res['Content-Length']
|
|
|
|
when 'Net::HTTPOK' then
|
|
|
|
assert_equal @last_modified, res['Last-Modified']
|
|
|
|
assert_equal @content_length, res['Content-Length']
|
|
|
|
else
|
|
|
|
fail "Incorrect response class: #{response_class}"
|
2006-05-13 17:25:53 -04:00
|
|
|
end
|
2006-05-04 12:14:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|