Don't use the server.pl script in the live tests
[catagits/Catalyst-Runtime.git] / t / author / http-server.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 1;
5
6 use File::Path;
7 use FindBin;
8 use IO::Socket;
9
10 use Catalyst::Devel 1.0;
11 use File::Copy::Recursive;
12
13 # Run a single test by providing it as the first arg
14 my $single_test = shift;
15
16 my $tmpdir = "$FindBin::Bin/../../t/tmp";
17
18 # clean up
19 rmtree $tmpdir if -d $tmpdir;
20
21 # create a TestApp and copy the test libs into it
22 mkdir $tmpdir;
23 chdir $tmpdir;
24 system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
25 chdir "$FindBin::Bin/..";
26 File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;
27
28 # remove TestApp's tests
29 rmtree '../t/tmp/TestApp/t' or die;
30
31 # spawn the standalone HTTP server
32 my $port = 30000 + int rand(1 + 10000);
33
34 my $pid = fork;
35 if ($pid) {
36     # parent.
37     print "Waiting for server to start...\n";
38     my $timeout = 30;
39     my $count = 0;
40     while ( check_port( 'localhost', $port ) != 1 ) {
41         sleep 1;
42         die "Server did not start within $timeout seconds:"
43             if $count++ > $timeout;
44     }
45 } elsif ($pid == 0) {
46     # child process
47     unshift @INC, "$tmpdir/TestApp/lib", "$FindBin::Bin/../../lib";
48     require TestApp;
49
50     my $psgi_app = TestApp->_wrapped_legacy_psgi_app(TestApp->psgi_app);
51     Plack::Loader->auto(port => $port)->run($psgi_app);
52
53     exit 0;
54 } else {
55     die "fork failed: $!";
56 }
57
58 # run the testsuite against the HTTP server
59 $ENV{CATALYST_SERVER} = "http://localhost:$port";
60
61 chdir '..';
62
63 my $return;
64 if ( $single_test ) {
65     $return = system( "$^X -Ilib/ $single_test" );
66 }
67 else {
68     $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t'));
69 }
70
71 # shut it down
72 kill 'INT', $pid;
73
74 # clean up
75 rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";
76
77 is( $return, 0, 'live tests' );
78
79 sub check_port {
80     my ( $host, $port ) = @_;
81
82     my $remote = IO::Socket::INET->new(
83         Proto    => "tcp",
84         PeerAddr => $host,
85         PeerPort => $port
86     );
87     if ($remote) {
88         close $remote;
89         return 1;
90     }
91     else {
92         return 0;
93     }
94 }
95
96 sub prove {
97     my (@tests) = @_;
98     if (!(my $pid = fork)) {
99         require TAP::Harness;
100
101         my $aggr = -e '.aggregating';
102         my $harness = TAP::Harness->new({
103             ($aggr ? (test_args => \@tests) : ()),
104             lib => ['lib'],
105         });
106
107         my $aggregator = $aggr
108             ? $harness->runtests('t/aggregate.t')
109             : $harness->runtests(@tests);
110
111         exit $aggregator->has_errors ? 1 : 0;
112     } else {
113         waitpid $pid, 0;
114         return $?;
115     }
116 }