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