Removed 01 from the filename
[catagits/Catalyst-Runtime.git] / t / optional / http-server.t
CommitLineData
040850b3 1#!perl
2
3use strict;
4use warnings;
5
040850b3 6use File::Path;
7use FindBin;
896c9ed2 8use IO::Socket;
f0ee1a76 9use Test::More;
10eval "use File::Copy::Recursive";
11
f0ee1a76 12plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP};
ac93c094 13plan skip_all => 'File::Copy::Recursive required' if $@;
040850b3 14
15# clean up
16rmtree "$FindBin::Bin/../../t/var" if -d "$FindBin::Bin/../../t/var";
17
18# create a TestApp and copy the test libs into it
19mkdir "$FindBin::Bin/../../t/var";
20chdir "$FindBin::Bin/../../t/var";
21system "$FindBin::Bin/../../script/catalyst.pl TestApp";
22chdir "$FindBin::Bin/../..";
23File::Copy::Recursive::dircopy( 't/live/lib', 't/var/TestApp/lib' );
24
25# spawn the standalone HTTP server
896c9ed2 26my $port = 30000 + int rand(1 + 10000);
27my $pid = open my $server,
28 "$FindBin::Bin/../../t/var/TestApp/script/testapp_server.pl -port $port 2>&1 |"
040850b3 29 or die "Unable to spawn standalone HTTP server: $!";
896c9ed2 30
040850b3 31# wait for it to start
896c9ed2 32print "Waiting for server to start...\n";
33while ( check_port( 'localhost', $port ) != 1 ) {
34 sleep 1;
35}
040850b3 36
37# run the testsuite against the HTTP server
896c9ed2 38$ENV{CATALYST_SERVER} = "http://localhost:$port";
040850b3 39system( 'prove -r -Ilib/ t/live/' );
40
41# shut it down
42kill 2, $pid;
896c9ed2 43close $server;
040850b3 44
45# clean up
46rmtree "$FindBin::Bin/../../t/var" if -d "$FindBin::Bin/../../t/var";
896c9ed2 47
48sub 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}