]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-corner.psgi
0e0e21a84cd585cf187793c8f85f85b78f574d10
[public-inbox.git] / t / httpd-corner.psgi
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # corner case tests for the generic PSGI server
4 # Usage: plackup [OPTIONS] /path/to/this/file
5 use strict;
6 use warnings;
7 use Plack::Request;
8 use Plack::Builder;
9 require Digest::SHA;
10 my $app = sub {
11         my ($env) = @_;
12         my $path = $env->{PATH_INFO};
13         my $in = $env->{'psgi.input'};
14         my $actual = -s $in;
15         my $code = 500;
16         my $h = [ 'Content-Type' => 'text/plain' ];
17         my $body = [];
18         if ($path eq '/sha1') {
19                 my $sha1 = Digest::SHA->new('SHA-1');
20                 my $buf;
21                 while (1) {
22                         my $r = $in->read($buf, 4096);
23                         die "read err: $!" unless defined $r;
24                         last if $r == 0;
25                         $sha1->add($buf);
26                 }
27                 $code = 200;
28                 push @$body, $sha1->hexdigest;
29         } elsif (my $fifo = $env->{HTTP_X_CHECK_FIFO}) {
30                 if ($path eq '/slow-header') {
31                         return sub {
32                                 open my $f, '<', $fifo or
33                                                 die "open $fifo: $!\n";
34                                 my @r = <$f>;
35                                 $_[0]->([200, $h, \@r ]);
36                         };
37                 } elsif ($path eq '/slow-body') {
38                         return sub {
39                                 my $fh = $_[0]->([200, $h]);
40                                 open my $f, '<', $fifo or
41                                                 die "open $fifo: $!\n";
42                                 while (defined(my $l = <$f>)) {
43                                         $fh->write($l);
44                                 }
45                                 $fh->close;
46                         };
47                 }
48         }
49
50         [ $code, $h, $body ]
51 };
52
53 builder {
54         enable 'ContentLength';
55         enable 'Head';
56         $app;
57 }