X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=t%2Fhttpd-corner.t;h=89f2866b55682215811eec1e87b7abc269b4a4d3;hb=4da62f284003d75abe7cb35594414eb2224f42bc;hp=cb813897c025d6eca82d2da81ede68fc8ce64903;hpb=95bdac7f09c69036efed537a4d03d5bdd2ae4eb6;p=public-inbox.git diff --git a/t/httpd-corner.t b/t/httpd-corner.t index cb813897..89f2866b 100644 --- a/t/httpd-corner.t +++ b/t/httpd-corner.t @@ -10,6 +10,7 @@ use PublicInbox::Spawn qw(which spawn); use PublicInbox::TestCommon; require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status)); use Digest::SHA qw(sha1_hex); +use IO::Handle (); use IO::Socket; use IO::Socket::UNIX; use Fcntl qw(:seek); @@ -22,6 +23,7 @@ my $err = "$tmpdir/stderr.log"; my $out = "$tmpdir/stdout.log"; my $psgi = "./t/httpd-corner.psgi"; my $sock = tcp_server() or die; +my @zmods = qw(PublicInbox::GzipFilter IO::Uncompress::Gunzip); # make sure stdin is not a pipe for lsof test to check for leaking pipes open(STDIN, '<', '/dev/null') or die 'no /dev/null: $!'; @@ -154,6 +156,38 @@ SKIP: { like($head, qr/\b413\b/, 'got 413 response'); } +{ + my $conn = conn_for($sock, '1.1 Transfer-Encoding bogus'); + $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding: bogus\r\n\r\n"); + $conn->read(my $buf, 4096); + like($buf, qr!\AHTTP/1\.[0-9] 400 !, 'got 400 response on bogus TE'); +} +{ + my $conn = conn_for($sock, '1.1 Content-Length bogus'); + $conn->write("PUT /sha1 HTTP/1.1\r\nContent-Length: 3.3\r\n\r\n"); + $conn->read(my $buf, 4096); + like($buf, qr!\AHTTP/1\.[0-9] 400 !, 'got 400 response on bad length'); +} + +{ + my $req = "PUT /sha1 HTTP/1.1\r\nContent-Length: 3\r\n" . + "Content-Length: 3\r\n\r\n"; + # this is stricter than it needs to be. Due to the way + # Plack::HTTPParser, PSGI specs, and how hash tables work in common + # languages; it's not possible to tell the difference between folded + # and intentionally bad commas (e.g. "Content-Length: 3, 3") + if (0) { + require Plack::HTTPParser; # XS or pure Perl + require Data::Dumper; + Plack::HTTPParser::parse_http_request($req, my $env = {}); + diag Data::Dumper::Dumper($env); # "Content-Length: 3, 3" + } + my $conn = conn_for($sock, '1.1 Content-Length dupe'); + $conn->write($req); + $conn->read(my $buf, 4096); + like($buf, qr!\AHTTP/1\.[0-9] 400 !, 'got 400 response on dupe length'); +} + { my $conn = conn_for($sock, 'chunk with pipeline'); my $n = 10; @@ -292,6 +326,22 @@ SKIP: { close $fh or die "curl errored out \$?=$?"; is($n, 30 * 1024 * 1024, 'got expected output from curl'); is($non_zero, 0, 'read all zeros'); + + require_mods(@zmods, 1); + open $fh, '-|', qw(curl -sS), "$base/psgi-return-gzip" or die; + binmode $fh; + my $buf = do { local $/; <$fh> }; + close $fh or die "curl errored out \$?=$?"; + IO::Uncompress::Gunzip::gunzip(\$buf => \(my $out)); + is($out, "hello world\n"); +} + +{ + my $conn = conn_for($sock, 'psgi_return ENOENT'); + print $conn "GET /psgi-return-enoent HTTP/1.1\r\n\r\n" or die; + my $buf = ''; + sysread($conn, $buf, 16384, length($buf)) until $buf =~ /\r\n\r\n/; + like($buf, qr!HTTP/1\.[01] 500\b!, 'got 500 error on ENOENT'); } { @@ -494,7 +544,7 @@ SKIP: { } { - my $conn = conn_for($sock, '1.1 Connnection: close'); + my $conn = conn_for($sock, '1.1 Connection: close'); $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n"); delay(); $conn->write("Content-Length: $len\r\n\r\n$str"); @@ -564,6 +614,36 @@ SKIP: { is_deeply([], [keys %child], 'no extra pipes with -W0'); }; +# ensure compatibility with other PSGI servers +SKIP: { + require_mods(@zmods, qw(Plack::Test HTTP::Request::Common), 3); + use_ok 'HTTP::Request::Common'; + use_ok 'Plack::Test'; + STDERR->flush; + open my $olderr, '>&', \*STDERR or die "dup stderr: $!"; + open my $tmperr, '+>', undef or die; + open STDERR, '>&', $tmperr or die; + STDERR->autoflush(1); + my $app = require $psgi; + test_psgi($app, sub { + my ($cb) = @_; + my $req = GET('http://example.com/psgi-return-gzip'); + my $res = $cb->($req); + my $buf = $res->content; + IO::Uncompress::Gunzip::gunzip(\$buf => \(my $out)); + is($out, "hello world\n", 'got expected output'); + + $req = GET('http://example.com/psgi-return-enoent'); + $res = $cb->($req); + is($res->code, 500, 'got error on ENOENT'); + seek($tmperr, 0, SEEK_SET) or die; + my $errbuf = do { local $/; <$tmperr> }; + like($errbuf, qr/this-better-not-exist/, + 'error logged about missing command'); + }); + open STDERR, '>&', $olderr or die "restore stderr: $!"; +} + done_testing(); sub capture {