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