1#!/usr/bin/perl -w
2
3use strict;
4use lib 't/lib';
5
6use Test::More tests => 3;
7use Test::HexString;
8
9use IO::Async::Loop;
10use IO::Async::Test;
11
12use Net::Async::FastCGI;
13
14use TestFCGI;
15
16my $request;
17
18my ( $S, $selfaddr ) = make_server_sock;
19
20my $loop = IO::Async::Loop->new();
21testing_loop( $loop );
22
23my $fcgi = Net::Async::FastCGI->new(
24   handle => $S,
25   on_request => sub { $request = $_[1] },
26);
27
28$loop->add( $fcgi );
29
30my $C = connect_client_sock( $selfaddr );
31
32$C->syswrite(
33   # Begin
34   fcgi_trans( type => 1, id => 1, data => "\0\1\0\0\0\0\0\0" ) .
35   # No parameters
36   fcgi_trans( type => 4, id => 1, data => "" ) .
37   # No STDIN
38   fcgi_trans( type => 5, id => 1, data => "" )
39);
40
41wait_for { defined $request };
42
43is_deeply( $request->params,
44           {},
45           '$request has empty params hash' );
46is( $request->read_stdin_line,
47    undef,
48    '$request has empty STDIN' );
49
50my $stdout = "Hello, world.";
51
52$request->stream_stdout_then_finish(
53   sub {
54      my ( $len ) = @_;
55      return length $stdout ? substr( $stdout, 0, 128, "" ) : undef;
56   },
57   0 );
58
59my $expect;
60
61$expect =
62   # STDOUT
63   fcgi_trans( type => 6, id => 1, data => "Hello, world." ) .
64   # End of STDOUT
65   fcgi_trans( type => 6, id => 1, data => "" ) .
66   # End request
67   fcgi_trans( type => 3, id => 1, data => "\0\0\0\0\0\0\0\0" );
68
69my $buffer;
70
71$buffer = "";
72
73wait_for_stream { length $buffer >= length $expect } $C => $buffer;
74
75is_hexstr( $buffer, $expect, 'FastCGI end request record' );
76