port test/unit/test_ccc.rb to Perl 5
[unicorn.git] / t / client_body_buffer_size.t
blobc8e871d4fbb34f6f54e72d7e6f2e3c72a88e7f62
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 my $conf_fh = write_file '>', $u_conf, <<EOM;
8 client_body_buffer_size 0
9 EOM
10 $conf_fh->autoflush(1);
11 my $srv = tcp_server();
12 my $host_port = tcp_host_port($srv);
13 my @uarg = (qw(-E none t/client_body_buffer_size.ru -c), $u_conf);
14 my $ar = unicorn(@uarg, { 3 => $srv });
15 my ($c, $status, $hdr);
16 my $mem_class = 'StringIO';
17 my $fs_class = 'Unicorn::TmpIO';
19 $c = tcp_start($srv, "PUT /input_class HTTP/1.0\r\nContent-Length: 0");
20 ($status, $hdr) = slurp_hdr($c);
21 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
22 is(readline($c), $mem_class, 'zero-byte file is StringIO');
24 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: 1");
25 print $c '.';
26 ($status, $hdr) = slurp_hdr($c);
27 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
28 is(readline($c), $fs_class, '1 byte file is filesystem-backed');
31 my $fifo = "$tmpdir/fifo";
32 POSIX::mkfifo($fifo, 0600) or die "mkfifo: $!";
33 seek($conf_fh, 0, SEEK_SET);
34 truncate($conf_fh, 0);
35 print $conf_fh <<EOM;
36 after_fork { |_,_| File.open('$fifo', 'w') { |fp| fp.write "pid=#\$\$" } }
37 EOM
38 $ar->do_kill('HUP');
39 open my $fifo_fh, '<', $fifo;
40 like(my $wpid = readline($fifo_fh), qr/\Apid=\d+\z/a ,
41         'reloaded w/ default client_body_buffer_size');
44 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: 1");
45 ($status, $hdr) = slurp_hdr($c);
46 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
47 is(readline($c), $mem_class, 'class for a 1 byte file is memory-backed');
50 my $one_meg = 1024 ** 2;
51 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: $one_meg");
52 ($status, $hdr) = slurp_hdr($c);
53 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
54 is(readline($c), $fs_class, '1 megabyte file is FS-backed');
56 # reload with bigger client_body_buffer_size
57 say $conf_fh "client_body_buffer_size $one_meg";
58 $ar->do_kill('HUP');
59 open $fifo_fh, '<', $fifo;
60 like($wpid = readline($fifo_fh), qr/\Apid=\d+\z/a ,
61         'reloaded w/ bigger client_body_buffer_size');
64 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: $one_meg");
65 ($status, $hdr) = slurp_hdr($c);
66 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
67 is(readline($c), $mem_class, '1 megabyte file is now memory-backed');
69 my $too_big = $one_meg + 1;
70 $c = tcp_start($srv, "PUT /tmp_class HTTP/1.0\r\nContent-Length: $too_big");
71 ($status, $hdr) = slurp_hdr($c);
72 like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid');
73 is(readline($c), $fs_class, '1 megabyte + 1 byte file is FS-backed');
76 undef $ar;
77 check_stderr;
78 undef $tmpdir;
79 done_testing;