3bc33a0e4acea91e375aa73c8eded2a3195a7c1b
[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 Test::TCP;
9 use Try::Tiny;
10 use Plack::Builder;
11
12 use Catalyst::Devel 1.0;
13 use File::Copy::Recursive;
14
15 # Run a single test by providing it as the first arg
16 my $single_test = shift;
17
18 my $tmpdir = "$FindBin::Bin/../../t/tmp";
19
20 # clean up
21 rmtree $tmpdir if -d $tmpdir;
22
23 # create a TestApp and copy the test libs into it
24 mkdir $tmpdir;
25 chdir $tmpdir;
26 system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
27 chdir "$FindBin::Bin/..";
28 File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;
29
30 # remove TestApp's tests
31 rmtree '../t/tmp/TestApp/t' or die;
32
33 # spawn the standalone HTTP server
34 my $port = empty_port;
35
36 my $pid = fork;
37 if ($pid) {
38     # parent.
39     print "Waiting for server to start...\n";
40     wait_port_timeout($port, 30);
41 } elsif ($pid == 0) {
42     # child process
43     unshift @INC, "$tmpdir/TestApp/lib", "$FindBin::Bin/../../lib";
44     require TestApp;
45
46     my $psgi_app = TestApp->_wrapped_legacy_psgi_app(TestApp->psgi_app);
47     Plack::Loader->auto(port => $port)->run(builder {
48         mount '/test_prefix' => $psgi_app;
49         mount '/' => sub {
50             return [501, ['Content-Type' => 'text/plain'], ['broken tests']];
51         };
52     });
53
54     exit 0;
55 } else {
56     die "fork failed: $!";
57 }
58
59 # run the testsuite against the HTTP server
60 $ENV{CATALYST_SERVER} = "http://localhost:$port/test_prefix";
61
62 chdir '..';
63
64 my $return;
65 if ( $single_test ) {
66     $return = system( "$^X -Ilib/ $single_test" );
67 }
68 else {
69     $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t'));
70 }
71
72 # shut it down
73 kill 'INT', $pid;
74
75 # clean up
76 rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";
77
78 is( $return, 0, 'live tests' );
79
80 sub wait_port_timeout {
81     my ($port, $timeout) = @_;
82
83     # wait_port waits for 10 seconds
84     for (1 .. int($timeout / 10)) { # meh, good enough.
85         try { wait_port $port; 1 } and return;
86     }
87
88     die "Server did not start within $timeout seconds";
89 }
90
91 sub prove {
92     my (@tests) = @_;
93     if (!(my $pid = fork)) {
94         require TAP::Harness;
95
96         my $aggr = -e '.aggregating';
97         my $harness = TAP::Harness->new({
98             ($aggr ? (test_args => \@tests) : ()),
99             lib => ['lib'],
100         });
101
102         my $aggregator = $aggr
103             ? $harness->runtests('t/aggregate.t')
104             : $harness->runtests(@tests);
105
106         exit $aggregator->has_errors ? 1 : 0;
107     } else {
108         waitpid $pid, 0;
109         return $?;
110     }
111 }