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