diff options
author | zedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9> | 2006-05-04 16:14:43 +0000 |
---|---|---|
committer | zedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9> | 2006-05-04 16:14:43 +0000 |
commit | a46d3cfedbe14c672f52645167319bdfe959fb7b (patch) | |
tree | f45062eb7f6118f1c0f922b010433b8974940560 | |
parent | 0ca765c199519f30e99bda26552905f9e4baf0b9 (diff) | |
download | unicorn-a46d3cfedbe14c672f52645167319bdfe959fb7b.tar.gz |
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@164 19e92222-5c0b-0410-8929-a290d50e31e9
-rw-r--r-- | test/test_conditional.rb | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/test/test_conditional.rb b/test/test_conditional.rb new file mode 100644 index 0000000..cccf976 --- /dev/null +++ b/test/test_conditional.rb @@ -0,0 +1,100 @@ +require 'test/unit' +require 'net/http' +require 'mongrel' + +include Mongrel + +class HttpParserTest < Test::Unit::TestCase + def setup + @h = HttpServer.new('0.0.0.0', 3001) + @h.register('/', Mongrel::DirHandler.new('.')) + @h.run + + @http = Net::HTTP.new(@h.host, @h.port) + + # get the ETag and Last-Modified headers + @path = '/README' + res = @http.start { |http| http.get(@path) } + @etag = res['ETag'] + @last_modified = res['Last-Modified'] + 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 + + # status should be 304 Not Modified when If-Unmodified-Since is the matching Last-Modified date + def test_not_modified_via_if_unmodified_since + assert_status_for_get_and_head Net::HTTPNotModified, 'If-Unmodified-Since' => @last_modified + end + + # status should be 304 Not Modified when If-None-Match is the matching ETag + # and If-Unmodified-Since is the matching Last-Modified date + def test_not_modified_via_if_none_match_and_if_unmodified_since + assert_status_for_get_and_head Net::HTTPNotModified, 'If-None-Match' => @etag, 'If-Unmodified-Since' => @last_modified + 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' + assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => 'invalid', 'If-Unmodified-Since' => @last_modified + end + + # status should be 200 OK when If-Unmodified-Since is invalid + def test_invalid_if_unmodified_since + assert_status_for_get_and_head Net::HTTPOK, 'If-Unmodified-Since' => 'invalid' + assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => @etag, 'If-Unmodified-Since' => 'invalid' + end + + # status should be 304 Not Modified when If-Unmodified-Since is greater than the Last-Modified header, but less than the system time + def test_if_unmodified_since_greater_than_last_modified + sleep 2 + last_modified_plus_1 = (Time.httpdate(@last_modified) + 1).httpdate + assert_status_for_get_and_head Net::HTTPNotModified, 'If-Unmodified-Since' => last_modified_plus_1 + assert_status_for_get_and_head Net::HTTPNotModified, 'If-None-Match' => @etag, 'If-Unmodified-Since' => last_modified_plus_1 + end + + # status should be 200 OK when If-Unmodified-Since is less than the Last-Modified header + def test_if_unmodified_since_less_than_last_modified + last_modified_minus_1 = (Time.httpdate(@last_modified) - 1).httpdate + assert_status_for_get_and_head Net::HTTPOK, 'If-Unmodified-Since' => last_modified_minus_1 + assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => @etag, 'If-Unmodified-Since' => last_modified_minus_1 + end + + # status should be 200 OK when If-Unmodified-Since is a date in the future + def test_future_if_unmodified_since + the_future = Time.at(2**31-1).httpdate + assert_status_for_get_and_head Net::HTTPOK, 'If-Unmodified-Since' => the_future + assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => @etag, 'If-Unmodified-Since' => the_future + 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' => '*' + assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => '*', 'If-Unmodified-Since' => @last_modified + end + + private + + # assert the response status is correct for GET and HEAD + def assert_status_for_get_and_head(status_class, headers = {}) + %w{ get head }.each do |method| + assert_kind_of status_class, @http.send(method, @path, headers) + end + end +end |