fixed lighttpd tests
[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 FindBin;
8 use IO::Socket;
9 use Test::More;
10
11 eval "use Catalyst::Devel 1.0";
12 plan skip_all => 'Catalyst::Devel required' if $@;
13
14 eval "use File::Copy::Recursive";
15 plan skip_all => 'File::Copy::Recursive required' if $@;
16
17 eval "use Test::Harness";
18 plan skip_all => 'Test::Harness required' if $@;
19
20 my $lighttpd_bin = $ENV{LIGHTTPD_BIN};
21 plan skip_all => 'Please set LIGHTTPD_BIN to run this test'
22     unless $lighttpd_bin && -x $lighttpd_bin;
23
24 plan tests => 1;
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 = <<"END";
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 END
76
77 open(my $lightconf, '>', "$docroot/lighttpd.conf") 
78   or die "Can't open $docroot/lighttpd.conf: $!";
79 print {$lightconf} $conf or die "Write error: $!";
80 close $lightconf;
81
82 my $pid = open my $lighttpd, "$lighttpd_bin -D -f $docroot/lighttpd.conf 2>&1 |" 
83     or die "Unable to spawn lighttpd: $!";
84     
85 # wait for it to start
86 while ( check_port( 'localhost', $port ) != 1 ) {
87     diag "Waiting for server to start...";
88     sleep 1;
89 }
90
91 # run the testsuite against the server
92 $ENV{CATALYST_SERVER} = "http://localhost:$port/deep/path";
93
94 my @tests = glob('t/live_*');
95 eval {
96     runtests(@tests);
97 };
98 ok(!$@, 'lighttpd tests ran OK');
99
100 # shut it down
101 kill 'INT', $pid;
102 close $lighttpd;
103
104 # clean up
105 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
106
107 sub check_port {
108     my ( $host, $port ) = @_;
109
110     my $remote = IO::Socket::INET->new(
111         Proto    => "tcp",
112         PeerAddr => $host,
113         PeerPort => $port
114     );
115     if ($remote) {
116         close $remote;
117         return 1;
118     }
119     else {
120         return 0;
121     }
122 }