Remove use of overload
[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;
f0ee1a76 10
f0ee1a76 11plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP};
5c9c810d 12eval "use Catalyst::Devel 1.0";
13plan skip_all => 'Catalyst::Devel required' if $@;
0d348d55 14eval "use File::Copy::Recursive";
15plan skip_all => 'File::Copy::Recursive required' if $@;
55ddccae 16plan tests => 1;
040850b3 17
547f8806 18# Run a single test by providing it as the first arg
19my $single_test = shift;
20
040850b3 21# clean up
a2e038a1 22rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
040850b3 23
24# create a TestApp and copy the test libs into it
a2e038a1 25mkdir "$FindBin::Bin/../t/tmp";
26chdir "$FindBin::Bin/../t/tmp";
27system "perl -I$FindBin::Bin/../lib $FindBin::Bin/../script/catalyst.pl TestApp";
67c3b305 28chdir "$FindBin::Bin/..";
a2e038a1 29File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
10bdcbe8 30
31# remove TestApp's tests
32rmtree 't/tmp/TestApp/t';
040850b3 33
34# spawn the standalone HTTP server
896c9ed2 35my $port = 30000 + int rand(1 + 10000);
36my $pid = open my $server,
a2e038a1 37 "perl -I$FindBin::Bin/../lib $FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl -port $port 2>&1 |"
040850b3 38 or die "Unable to spawn standalone HTTP server: $!";
896c9ed2 39
040850b3 40# wait for it to start
896c9ed2 41print "Waiting for server to start...\n";
42while ( check_port( 'localhost', $port ) != 1 ) {
43 sleep 1;
44}
32e231eb 45
040850b3 46# run the testsuite against the HTTP server
896c9ed2 47$ENV{CATALYST_SERVER} = "http://localhost:$port";
547f8806 48
49if ( $single_test ) {
50 system( "perl -Ilib/ $single_test" );
51}
52else {
53 system( 'prove -r -Ilib/ t/live_*' );
54}
040850b3 55
56# shut it down
e1b364f4 57kill 'INT', $pid;
896c9ed2 58close $server;
040850b3 59
60# clean up
a2e038a1 61rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
896c9ed2 62
55ddccae 63ok( 'done' );
64
896c9ed2 65sub check_port {
66 my ( $host, $port ) = @_;
67
68 my $remote = IO::Socket::INET->new(
69 Proto => "tcp",
70 PeerAddr => $host,
71 PeerPort => $port
72 );
73 if ($remote) {
74 close $remote;
75 return 1;
76 }
77 else {
78 return 0;
79 }
80}