t/lib.perl: fix Perl integration tests w/o installation
[unicorn.git] / t / client_body_buffer_size.t
blobd47990122c75863ae774ec5ea4e9591873eaa5ff
1 #!perl -w
2 # Copyright (C) unicorn hackers <unicorn-public@yhbt.net>
3 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
5 use v5.14; BEGIN { require './t/lib.perl' };
6 use autodie;
7 open my $conf_fh, '>', $u_conf;
8 $conf_fh->autoflush(1);
9 print $conf_fh <<EOM;
10 client_body_buffer_size 0
11 EOM
12 my $srv = tcp_server();
13 my $host_port = tcp_host_port($srv);
14 my @uarg = (qw(-E none t/client_body_buffer_size.ru -c), $u_conf);
15 my $ar = unicorn(@uarg, { 3 => $srv });
16 my ($c, $status, $hdr);
17 my $mem_class = 'StringIO';
18 my $fs_class = 'Unicorn::TmpIO';
20 $c = tcp_start($srv, "PUT /input_class HTTP/1.0\r\nContent-Length: 0");
21 ($status, $hdr) = slurp_hdr($c);
22 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
23 is(readline($c), $mem_class, 'zero-byte file is StringIO');
25 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: 1");
26 print $c '.';
27 ($status, $hdr) = slurp_hdr($c);
28 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
29 is(readline($c), $fs_class, '1 byte file is filesystem-backed');
32 my $fifo = "$tmpdir/fifo";
33 POSIX::mkfifo($fifo, 0600) or die "mkfifo: $!";
34 seek($conf_fh, 0, SEEK_SET);
35 truncate($conf_fh, 0);
36 print $conf_fh <<EOM;
37 after_fork { |_,_| File.open('$fifo', 'w') { |fp| fp.write "pid=#\$\$" } }
38 EOM
39 $ar->do_kill('HUP');
40 open my $fifo_fh, '<', $fifo;
41 like(my $wpid = readline($fifo_fh), qr/\Apid=\d+\z/a ,
42         'reloaded w/ default client_body_buffer_size');
45 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: 1");
46 ($status, $hdr) = slurp_hdr($c);
47 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
48 is(readline($c), $mem_class, 'class for a 1 byte file is memory-backed');
51 my $one_meg = 1024 ** 2;
52 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: $one_meg");
53 ($status, $hdr) = slurp_hdr($c);
54 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
55 is(readline($c), $fs_class, '1 megabyte file is FS-backed');
57 # reload with bigger client_body_buffer_size
58 say $conf_fh "client_body_buffer_size $one_meg";
59 $ar->do_kill('HUP');
60 open $fifo_fh, '<', $fifo;
61 like($wpid = readline($fifo_fh), qr/\Apid=\d+\z/a ,
62         'reloaded w/ bigger client_body_buffer_size');
65 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: $one_meg");
66 ($status, $hdr) = slurp_hdr($c);
67 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
68 is(readline($c), $mem_class, '1 megabyte file is now memory-backed');
70 my $too_big = $one_meg + 1;
71 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: $too_big");
72 ($status, $hdr) = slurp_hdr($c);
73 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
74 is(readline($c), $fs_class, '1 megabyte + 1 byte file is FS-backed');
77 undef $ar;
78 check_stderr;
79 undef $tmpdir;
80 done_testing;