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