stas 2003/08/13 20:52:03
Modified: ModPerl-Registry/t 304.t ModPerl-Registry/t/cgi-bin 304.pl Log: - make the 304 handler more robust - add more test cases (bogus dates) Revision Changes Path 1.2 +28 -17 modperl-2.0/ModPerl-Registry/t/304.t Index: 304.t =================================================================== RCS file: /home/cvs/modperl-2.0/ModPerl-Registry/t/304.t,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- 304.t 6 Aug 2003 07:30:31 -0000 1.1 +++ 304.t 14 Aug 2003 03:52:03 -0000 1.2 @@ -5,7 +5,7 @@ use Apache::TestUtil; use Apache::TestRequest qw(GET); -plan tests => 4; +plan tests => 10; my $url = "/registry/304.pl"; @@ -29,23 +29,34 @@ #t_debug $res->as_string; } - { - # modified - my $if_modified_since = 'Sun, 29 Oct 2000 15:43:28 GMT'; - my $res = GET($url, 'If-Modified-Since' => $if_modified_since); - - ok t_cmp( - 200, - $res->code, - "test !HTTP_NOT_MODIFIED (200 status)", + # full response cases: + # 1) the resource has been modified since the If-Modified-Since date + # 2) bogus If-Modified-Since date => is considered as a + # non-If-Modified-Since require + # + my %dates = ( + 'Sun, 29 Oct 2000 15:43:28 GMT' => "the resource was modified since #1", + 'Sun, 28 Oct 2000 15:43:29 GMT' => "the resource was modified since #2", + 'Thu, 32 Jun 1999 24:59:59 MIT' => "bogus If-Modified-Since #1", + 'Thu Juk 99 00:00:00 9999 FUK' => "bogus If-Modified-Since #2", ); + my $received = '<html><head></head><body>Test</body></html>'; + while ( my($if_modified_since, $debug) = each %dates) { + my $res = GET($url, 'If-Modified-Since' => $if_modified_since); + t_debug "If-Modified-Since $if_modified_since"; + ok t_cmp( + 200, + $res->code, + "$debug (code)" + ); + + ok t_cmp( + $received, + $res->content, + "$debug (body)" + ); - ok t_cmp( - '<html><head></head><body>Test</body></html>', - $res->content, - "test !HTTP_NOT_MODIFIED (normal body)", - ); - - #t_debug $res->as_string; + #t_debug $res->as_string; + } } 1.2 +18 -8 modperl-2.0/ModPerl-Registry/t/cgi-bin/304.pl Index: 304.pl =================================================================== RCS file: /home/cvs/modperl-2.0/ModPerl-Registry/t/cgi-bin/304.pl,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- 304.pl 6 Aug 2003 07:30:31 -0000 1.1 +++ 304.pl 14 Aug 2003 03:52:03 -0000 1.2 @@ -1,30 +1,40 @@ +use strict; +use warnings FATAL => 'all'; + +# manually handle 'If-Modified-Since' requests + use APR::Date (); use Apache::Util (); use Apache::RequestRec (); -use strict; -use warnings FATAL => 'all'; - use constant FMT => '%a, %d %b %Y %H:%M:%S %Z'; use constant GMT => 1; +use Apache::Const -compile => qw(HTTP_NOT_MODIFIED); my $last_modified = "Sun, 29 Oct 2000 15:43:29 GMT"; my $r = shift; -my $date = Apache::Util::format_time($r->request_time, FMT, GMT, $r->pool); - my $if_modified_since = $r->headers_in->{'If-Modified-Since'}; my $status = 200; my $body = '<html><head></head><body>Test</body></html>'; -if ($if_modified_since && APR::Date::parse_http($last_modified) - < APR::Date::parse_http($if_modified_since)) { +#APR::Date::parse_http may fail +my $if_modified_since_secs = + ($if_modified_since && APR::Date::parse_http($if_modified_since)) || 0; +my $last_modified_secs = APR::Date::parse_http($last_modified); + +#warn "If-Modified-Since $if_modified_since\n"; +#warn "last_modified_secs $last_modified_secs\n"; +#warn "if_modified_since_secs $if_modified_since_secs\n\n"; - $status = 304; +if ($last_modified_secs < $if_modified_since_secs) { + $status = Apache::HTTP_NOT_MODIFIED; $body = ''; } + +my $date = Apache::Util::format_time($r->request_time, FMT, GMT, $r->pool); print <<HEADERS; Status: $status