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