r12983@zaphod: kd | 2008-04-28 18:10:27 +1000
[catagits/Catalyst-Runtime.git] / t / optional_http-server.t
CommitLineData
040850b3 1use strict;
2use warnings;
3
040850b3 4use File::Path;
5use FindBin;
2f381252 6use IPC::Open3;
896c9ed2 7use IO::Socket;
f0ee1a76 8use Test::More;
f0ee1a76 9
f0ee1a76 10plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP};
5c9c810d 11eval "use Catalyst::Devel 1.0";
12plan skip_all => 'Catalyst::Devel required' if $@;
0d348d55 13eval "use File::Copy::Recursive";
14plan skip_all => 'File::Copy::Recursive required' if $@;
55ddccae 15plan tests => 1;
040850b3 16
547f8806 17# Run a single test by providing it as the first arg
18my $single_test = shift;
19
2f381252 20my $tmpdir = "$FindBin::Bin/../t/tmp";
21
040850b3 22# clean up
2f381252 23rmtree $tmpdir if -d $tmpdir;
040850b3 24
25# create a TestApp and copy the test libs into it
2f381252 26mkdir $tmpdir;
27chdir $tmpdir;
28system( 'perl', "-I$FindBin::Bin/../lib", "$FindBin::Bin/../script/catalyst.pl", 'TestApp' );
67c3b305 29chdir "$FindBin::Bin/..";
a2e038a1 30File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
10bdcbe8 31
32# remove TestApp's tests
33rmtree 't/tmp/TestApp/t';
040850b3 34
35# spawn the standalone HTTP server
896c9ed2 36my $port = 30000 + int rand(1 + 10000);
2f381252 37my $pid = open3( undef, my $server, undef,
38 'perl', "-I$FindBin::Bin/../lib",
39 "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '-port', $port )
040850b3 40 or die "Unable to spawn standalone HTTP server: $!";
896c9ed2 41
040850b3 42# wait for it to start
896c9ed2 43print "Waiting for server to start...\n";
44while ( check_port( 'localhost', $port ) != 1 ) {
45 sleep 1;
46}
32e231eb 47
040850b3 48# run the testsuite against the HTTP server
896c9ed2 49$ENV{CATALYST_SERVER} = "http://localhost:$port";
547f8806 50
2f381252 51my $return;
547f8806 52if ( $single_test ) {
2f381252 53 $return = system( "perl -Ilib/ $single_test" );
547f8806 54}
55else {
2f381252 56 $return = system( 'prove -r -Ilib/ t/live_*.t' );
547f8806 57}
040850b3 58
59# shut it down
e1b364f4 60kill 'INT', $pid;
896c9ed2 61close $server;
040850b3 62
63# clean up
a2e038a1 64rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
896c9ed2 65
2f381252 66is( $return, 0, 'live tests' );
55ddccae 67
896c9ed2 68sub check_port {
69 my ( $host, $port ) = @_;
70
71 my $remote = IO::Socket::INET->new(
72 Proto => "tcp",
73 PeerAddr => $host,
74 PeerPort => $port
75 );
76 if ($remote) {
77 close $remote;
78 return 1;
79 }
80 else {
81 return 0;
82 }
83}