Merge 'trunk' into 'better_scripts'
[catagits/Catalyst-Runtime.git] / t / author / http-server.t
CommitLineData
040850b3 1use strict;
2use warnings;
3
81f25ce6 4use Test::More tests => 1;
4853fb50 5
040850b3 6use File::Path;
7use FindBin;
2f381252 8use IPC::Open3;
896c9ed2 9use IO::Socket;
f0ee1a76 10
81f25ce6 11use Catalyst::Devel 1.0;
12use File::Copy::Recursive;
040850b3 13
547f8806 14# Run a single test by providing it as the first arg
15my $single_test = shift;
16
81f25ce6 17my $tmpdir = "$FindBin::Bin/../../t/tmp";
2f381252 18
040850b3 19# clean up
2f381252 20rmtree $tmpdir if -d $tmpdir;
040850b3 21
22# create a TestApp and copy the test libs into it
2f381252 23mkdir $tmpdir;
24chdir $tmpdir;
81f25ce6 25system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
67c3b305 26chdir "$FindBin::Bin/..";
81f25ce6 27File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;
10bdcbe8 28
29# remove TestApp's tests
81f25ce6 30rmtree '../t/tmp/TestApp/t' or die;
040850b3 31
32# spawn the standalone HTTP server
896c9ed2 33my $port = 30000 + int rand(1 + 10000);
81f25ce6 34my @cmd = ($^X, "-I$FindBin::Bin/../../lib",
35 "$FindBin::Bin/../../t/tmp/TestApp/script/testapp_server.pl", '-port', $port );
36my $pid = open3( undef, my $server, undef, @cmd)
040850b3 37 or die "Unable to spawn standalone HTTP server: $!";
896c9ed2 38
040850b3 39# wait for it to start
896c9ed2 40print "Waiting for server to start...\n";
81f25ce6 41my $timeout = 30;
42my $count = 0;
896c9ed2 43while ( check_port( 'localhost', $port ) != 1 ) {
44 sleep 1;
81f25ce6 45 die("Server did not start within $timeout seconds: " . join(' ', @cmd))
46 if $count++ > $timeout;
896c9ed2 47}
32e231eb 48
040850b3 49# run the testsuite against the HTTP server
896c9ed2 50$ENV{CATALYST_SERVER} = "http://localhost:$port";
547f8806 51
2f381252 52my $return;
547f8806 53if ( $single_test ) {
81f25ce6 54 $return = system( "$^X -I../lib/ $single_test" );
547f8806 55}
56else {
81f25ce6 57 $return = prove( '-r', '-I../lib/', glob('../t/aggregate/live_*.t') );
547f8806 58}
040850b3 59
60# shut it down
e1b364f4 61kill 'INT', $pid;
896c9ed2 62close $server;
040850b3 63
64# clean up
81f25ce6 65rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";
896c9ed2 66
2f381252 67is( $return, 0, 'live tests' );
55ddccae 68
896c9ed2 69sub check_port {
70 my ( $host, $port ) = @_;
71
72 my $remote = IO::Socket::INET->new(
73 Proto => "tcp",
74 PeerAddr => $host,
75 PeerPort => $port
76 );
77 if ($remote) {
78 close $remote;
79 return 1;
80 }
81 else {
82 return 0;
83 }
84}
868a7cca 85
86sub prove {
87 if (!(my $pid = fork)) {
88 require App::Prove;
89 my $prove = App::Prove->new;
90 $prove->process_args(@_);
91 exit( $prove->run ? 0 : 1 );
92 } else {
93 waitpid $pid, 0;
94 return $?;
95 }
96}