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