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