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