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