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