Fixed http server test scripts to run catalyst.pl without Cat installed
[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 $@;
55ddccae 14plan tests => 1;
040850b3 15
16# clean up
17rmtree "$FindBin::Bin/../../t/var" if -d "$FindBin::Bin/../../t/var";
18
19# create a TestApp and copy the test libs into it
20mkdir "$FindBin::Bin/../../t/var";
21chdir "$FindBin::Bin/../../t/var";
32e231eb 22system "perl -I$FindBin::Bin/../../lib $FindBin::Bin/../../script/catalyst.pl TestApp";
040850b3 23chdir "$FindBin::Bin/../..";
24File::Copy::Recursive::dircopy( 't/live/lib', 't/var/TestApp/lib' );
25
26# spawn the standalone HTTP server
896c9ed2 27my $port = 30000 + int rand(1 + 10000);
28my $pid = open my $server,
e1b364f4 29 "perl -I$FindBin::Bin/../../lib $FindBin::Bin/../../t/var/TestApp/script/testapp_server.pl -port $port 2>&1 |"
040850b3 30 or die "Unable to spawn standalone HTTP server: $!";
896c9ed2 31
040850b3 32# wait for it to start
896c9ed2 33print "Waiting for server to start...\n";
34while ( check_port( 'localhost', $port ) != 1 ) {
35 sleep 1;
36}
32e231eb 37
38sleep 60;
040850b3 39
40# run the testsuite against the HTTP server
896c9ed2 41$ENV{CATALYST_SERVER} = "http://localhost:$port";
55ddccae 42system( 'prove -r -Ilib/ t/live/' );
040850b3 43
44# shut it down
e1b364f4 45kill 'INT', $pid;
896c9ed2 46close $server;
040850b3 47
48# clean up
49rmtree "$FindBin::Bin/../../t/var" if -d "$FindBin::Bin/../../t/var";
896c9ed2 50
55ddccae 51ok( 'done' );
52
896c9ed2 53sub 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}