Fix for non-root lighttpd
[catagits/Catalyst-Runtime.git] / t / optional_lighttpd-fastcgi-non-root.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use File::Path;
7 use File::Slurp qw(write_file);
8 use FindBin;
9 use IO::Socket;
10 use Test::More;
11
12 eval "use Catalyst::Devel 1.0";
13 plan skip_all => 'Catalyst::Devel required' if $@;
14
15 eval "use File::Copy::Recursive";
16 plan skip_all => 'File::Copy::Recursive required' if $@;
17
18 my $lighttpd_bin = $ENV{LIGHTTPD_BIN} || 'lighttpd';
19 plan skip_all => 'Cannot find lighttpd, please set LIGHTTPD_BIN'
20     unless -x $lighttpd_bin;
21
22 plan tests => 1;
23
24 # clean up
25 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
26
27 # create a TestApp and copy the test libs into it
28 mkdir "$FindBin::Bin/../t/tmp";
29 chdir "$FindBin::Bin/../t/tmp";
30 system "perl -I$FindBin::Bin/../lib $FindBin::Bin/../script/catalyst.pl TestApp";
31 chdir "$FindBin::Bin/..";
32 File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
33
34 # remove TestApp's tests
35 rmtree 't/tmp/TestApp/t';
36
37 # Create a temporary lighttpd config
38 my $docroot = "$FindBin::Bin/../t/tmp";
39 my $port    = 8529;
40
41 # Clean up docroot path
42 $docroot =~ s{/t/..}{};
43
44 my $conf = qq{
45 # basic lighttpd config file for testing fcgi+catalyst
46 server.modules = (
47     "mod_access",
48     "mod_fastcgi",
49     "mod_accesslog"
50 )
51
52 server.document-root = "$docroot"
53
54 server.errorlog    = "$docroot/error.log"
55 accesslog.filename = "$docroot/access.log"
56
57 server.bind = "127.0.0.1"
58 server.port = $port
59
60 # catalyst app specific fcgi setup
61 fastcgi.server = (
62     "/deep/path" => (
63         "FastCgiTest" => (
64             "socket"       => "$docroot/test.socket",
65             "check-local"  => "disable",
66             "bin-path"     => "$docroot/TestApp/script/testapp_fastcgi.pl",
67             "min-procs"    => 1,
68             "max-procs"    => 1,
69             "idle-timeout" => 20
70         )
71     )
72 )
73 };
74
75 write_file "$docroot/lighttpd.conf", $conf;
76
77 my $pid = open my $lighttpd, "$lighttpd_bin -D -f $docroot/lighttpd.conf 2>&1 |" 
78     or die "Unable to spawn lighttpd: $!";
79     
80 # wait for it to start
81 print "Waiting for server to start...\n";
82 while ( check_port( 'localhost', $port ) != 1 ) {
83     sleep 1;
84 }
85
86 # run the testsuite against the server
87 $ENV{CATALYST_SERVER} = "http://localhost:$port/deep/path";
88 system( 'prove -r -Ilib/ t/live_*' );
89
90 # shut it down
91 kill 'INT', $pid;
92 close $lighttpd;
93
94 # clean up
95 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
96
97 ok( 'done' );
98
99 sub check_port {
100     my ( $host, $port ) = @_;
101
102     my $remote = IO::Socket::INET->new(
103         Proto    => "tcp",
104         PeerAddr => $host,
105         PeerPort => $port
106     );
107     if ($remote) {
108         close $remote;
109         return 1;
110     }
111     else {
112         return 0;
113     }
114 }