You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by sf...@apache.org on 2011/08/29 09:30:38 UTC

svn commit: r1162666 - /httpd/test/framework/trunk/t/apache/byterange7.t

Author: sf
Date: Mon Aug 29 07:30:37 2011
New Revision: 1162666

URL: http://svn.apache.org/viewvc?rev=1162666&view=rev
Log:
test content-length header in byterange-requests
test invalid range headers

Added:
    httpd/test/framework/trunk/t/apache/byterange7.t
      - copied, changed from r1162653, httpd/test/framework/trunk/t/apache/byterange5.t

Copied: httpd/test/framework/trunk/t/apache/byterange7.t (from r1162653, httpd/test/framework/trunk/t/apache/byterange5.t)
URL: http://svn.apache.org/viewvc/httpd/test/framework/trunk/t/apache/byterange7.t?p2=httpd/test/framework/trunk/t/apache/byterange7.t&p1=httpd/test/framework/trunk/t/apache/byterange5.t&r1=1162653&r2=1162666&rev=1162666&view=diff
==============================================================================
--- httpd/test/framework/trunk/t/apache/byterange5.t (original)
+++ httpd/test/framework/trunk/t/apache/byterange7.t Mon Aug 29 07:30:37 2011
@@ -5,100 +5,80 @@ use Apache::Test;
 use Apache::TestRequest;
 use Apache::TestUtil qw(t_write_file);
 
-# test multi-byterange-requests while allowing re-ordering
+# test content-length header in byterange-requests
+# test invalid range headers
 
 my $url = "/apache/chunked/byteranges.txt";
 my $file = Apache::Test::vars('serverroot') . "/htdocs$url";
 
 my $content = "";
-$content .= sprintf("%04d", $_) for (1 .. 2000);
+$content .= sprintf("%04d", $_) for (1 .. 10000);
 t_write_file($file, $content);
-my $clen = length($content);
+my $real_clen = length($content);
 
 
-my @test_cases = (
-    "0-1,1000-1001",
-    "1000-1100,100-200",
-    "1000-1100,100-200,2000-2200",
-    "1000-1100,100-200,2000-",
-    "3000-,100-200,2000-2200",
-);
-plan tests => scalar(@test_cases), need need_lwp;
-
-foreach my $test (@test_cases) {
-    my $result = GET $url, "Range" => "bytes=$test";
-    my $boundary;
-    my $ctype = $result->header("Content-Type");
-    if ($ctype =~ m{multipart/byteranges; boundary=(.*)}) {
-        $boundary = $1;
+my @test_cases = ( 1, 2, 10, 50, 100);
+my @test_cases2 = ("", ",", "7-1", "foo");
+plan tests => scalar(@test_cases) + 2 * scalar(@test_cases2), need need_lwp;
+
+foreach my $num (@test_cases) {
+    my @ranges;
+    foreach my $i (0 .. ($num-1)) {
+        push @ranges, sprintf("%d-%d", $i * 100, $i * 100 + 1);
+    }
+    my $range = join(",", @ranges);
+    my $result = GET $url, "Range" => "bytes=$range";
+    print "got ", $result->code, "\n";
+    if ($result->code != 206) {
+        print "did not get 206\n";
+        ok(0);
+        next;
     }
-    else {
-        print "Wrong Content-Type: $ctype\n"; 
+    my $clen = $result->header("Content-Length");
+    my $body = $result->content;
+    my $blen = length($body);
+    if ($blen == $real_clen) {
+        print "Did get full content, should have gotten only parts\n";
         ok(0);
         next;
     }
-
-    my @want = split(",", $test);
-    foreach my $w (@want) {
-        $w =~ /(\d*)-(\d*)/ or die;
-        if (defined $1 eq "") {
-            $w = [ $clen - $2, $clen - 1 ];
-        }
-        elsif ($2 eq "") {
-            $w = [ $1, $clen - 1 ];
-        }
-        else {
-            $w = [ $1, $2 ];
+    print "body length $blen\n";
+    if (defined $clen) {
+        print "Content-Length: $clen\n";
+        if ($blen != $clen) {
+            print "Content-Length does not match body\n";
+            ok(0);
+            next;
         }
     }
+    ok(1);
+}
 
-    my @got;
-    my $rcontent = $result->content;
-    my $error;
-    while ($rcontent =~ s{^[\n\s]*--$boundary\s*?\n(.+?)\r\n\r\n}{}s ) {
-        my $headers = $1;
-        my ($from, $to);
-        if ($headers =~ m{^Content-range: bytes (\d+)-(\d+)/\d*$}mi ) {
-            $from = $1;
-            $to = $2;
-        }
-        else {
-            print "Can't parse Content-range in '$headers'\n";
-            $error = 1;
-        }
-        push @got, [$from, $to];
-        my $chunk = substr($rcontent, 0, $to - $from + 1, "");
-        my $expect = substr($content, $from, $to - $from + 1);
-        if ($chunk ne $expect) {
-            print "Wrong content in range. Got: \n",
-                  $headers, $content,
-                  "Expected:\n$expect\n";
-            $error = 1;
-        }
+# test invalid range headers, with and without "bytes="
+my @test_cases3 = map { "bytes=" . $_ } @test_cases2;
+foreach my $range (@test_cases2, @test_cases3) {
+    my $result = GET $url, "Range" => "$range";
+    my $code = $result->code;
+    print "Got $code\n";
+    if ($code == 216) {
+        # guess that's ok
+        ok(1);
     }
-    if ($error) {
+    elsif ($code == 206) {
+        print "got partial content response with invalid range header\n";
         ok(0);
-        next;
     }
-    if ($rcontent !~ /^[\s\n]*--${boundary}--[\s\n]*$/) {
-        print "error parsing final boundary: '$rcontent'\n";
-        ok(0);
-        next;
-    }
-    foreach my $w (@want) {
-        my $found;
-        foreach my $g (@got) {
-            $found = 1 if ($g->[0] <= $w->[0] && $g->[1] >= $w->[1]);
-        }
-        if (!$found) {
-            print "Data for '$w->[0]-$w->[1]' not found in response\n";
-            $error = 1;
+    elsif ($code == 200) {
+        my $body = $result->content;
+        if ($body != $content) {
+            print "Body did not match expected content\n";
+            ok(0);
         }
+        ok(1);
     }
-    if ($error) {
+    else {
+        print "Huh?\n";
         ok(0);
-        next;
     }
-
-    ok (1);
 }
+