]> Sergey Matveev's repositories - public-inbox.git/commitdiff
t/httpd-corner: test HTTP chunking with curl
authorEric Wong <e@80x24.org>
Tue, 23 Feb 2016 23:17:01 +0000 (23:17 +0000)
committerEric Wong <e@80x24.org>
Tue, 23 Feb 2016 23:17:01 +0000 (23:17 +0000)
Just in case my knowledge of chunking is wrong.

t/httpd-corner.t

index 5834c1bd68fd7e2aea6616609a80673090fab55e..366e56cb5d3f04b2f00b5e9ee3e6ae983a6d55f0 100644 (file)
@@ -16,7 +16,7 @@ use Digest::SHA qw(sha1_hex);
 use File::Temp qw/tempdir/;
 use Cwd qw/getcwd/;
 use IO::Socket;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
+use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
 my $tmpdir = tempdir(CLEANUP => 1);
 my $err = "$tmpdir/stderr.log";
@@ -82,6 +82,43 @@ my $check_self = sub {
        is($body, sha1_hex($str), 'read expected body');
 };
 
+SKIP: {
+       use POSIX qw(dup2);
+       use IO::File;
+       my $have_curl = 0;
+       foreach my $p (split(':', $ENV{PATH})) {
+               -x "$p/curl" or next;
+               $have_curl = 1;
+               last;
+       }
+       my $ntest = 2;
+       $have_curl or skip('curl(1) missing', $ntest);
+       my $url = 'http://' . $sock->sockhost . ':' . $sock->sockport . '/sha1';
+       my ($r, $w);
+       pipe($r, $w) or die "pipe: $!";
+       my $tout = IO::File->new_tmpfile or die "new_tmpfile: $!";
+       my $pid = fork;
+       defined $pid or die "fork: $!";
+       my @cmd = (qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url);
+       if ($pid == 0) {
+               dup2(fileno($r), 0) or die "redirect stdin failed: $!\n";
+               dup2(fileno($tout), 1) or die "redirect stdout failed: $!\n";
+               exec(@cmd) or die 'exec `' . join(' '). "' failed: $!\n";
+       }
+       $w->autoflush(1);
+       foreach my $c ('a'..'z') {
+               print $w $c or die "failed to write to curl: $!";
+               delay();
+       }
+       close $w or die "close write pipe: $!";
+       close $r or die "close read pipe: $!";
+       my $kid = waitpid $pid, 0;
+       is($?, 0, 'curl exited successfully');
+       $tout->sysseek(0, SEEK_SET);
+       $tout->sysread(my $buf, 100);
+       is($buf, sha1_hex($str), 'read expected body');
+}
+
 {
        my $conn = conn_for($sock, '1.1 pipeline together');
        $conn->write("PUT /sha1 HTTP/1.1\r\nUser-agent: hello\r\n\r\n" .