Removed 01 from the filename
[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 eval "use File::Copy::Recursive";
11
12 plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP};
13 plan skip_all => 'File::Copy::Recursive required' if $@;
14
15 # clean up
16 rmtree "$FindBin::Bin/../../t/var" if -d "$FindBin::Bin/../../t/var";
17
18 # create a TestApp and copy the test libs into it
19 mkdir "$FindBin::Bin/../../t/var";
20 chdir "$FindBin::Bin/../../t/var";
21 system "$FindBin::Bin/../../script/catalyst.pl TestApp";
22 chdir "$FindBin::Bin/../..";
23 File::Copy::Recursive::dircopy( 't/live/lib', 't/var/TestApp/lib' );
24
25 # spawn the standalone HTTP server
26 my $port = 30000 + int rand(1 + 10000);
27 my $pid = open my $server, 
28     "$FindBin::Bin/../../t/var/TestApp/script/testapp_server.pl -port $port 2>&1 |"
29     or die "Unable to spawn standalone HTTP server: $!";
30
31 # wait for it to start
32 print "Waiting for server to start...\n";
33 while ( check_port( 'localhost', $port ) != 1 ) {
34     sleep 1;
35 }
36     
37 # run the testsuite against the HTTP server
38 $ENV{CATALYST_SERVER} = "http://localhost:$port";
39 system( 'prove -r -Ilib/ t/live/' );
40
41 # shut it down
42 kill 2, $pid;
43 close $server;
44
45 # clean up
46 rmtree "$FindBin::Bin/../../t/var" if -d "$FindBin::Bin/../../t/var";
47
48 sub check_port {
49     my ( $host, $port ) = @_;
50
51     my $remote = IO::Socket::INET->new(
52         Proto    => "tcp",
53         PeerAddr => $host,
54         PeerPort => $port
55     );
56     if ($remote) {
57         close $remote;
58         return 1;
59     }
60     else {
61         return 0;
62     }
63 }