convert to Distar
[catagits/Catalyst-Runtime.git] / xt / author / http-server.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 1;
5 use Test::TCP;
6
7 use File::Path;
8 use FindBin;
9 use Net::EmptyPort qw(wait_port empty_port);
10 use Try::Tiny;
11 use Plack::Builder;
12
13 eval { require Catalyst::Devel; Catalyst::Devel->VERSION(1.0); 1; } || do {
14     fail("Could not load Catalyst::Devel: $@");
15     exit 1;
16 };
17
18 eval { require File::Copy::Recursive; 1 } || do {
19     fail("Could not load File::Copy::Recursive: $@");
20     exit 1;
21 };
22
23 # Run a single test by providing it as the first arg
24 my $single_test = shift;
25
26 my $tmpdir = "$FindBin::Bin/../../t/tmp";
27
28 # clean up
29 rmtree $tmpdir if -d $tmpdir;
30
31 # create a TestApp and copy the test libs into it
32 mkdir $tmpdir;
33 chdir $tmpdir;
34 system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
35 chdir "$FindBin::Bin/..";
36 File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;
37
38 # remove TestApp's tests
39 rmtree '../t/tmp/TestApp/t' or die;
40
41 # spawn the standalone HTTP server
42 my $port = empty_port;
43
44 my $pid = fork;
45 if ($pid) {
46     # parent.
47     print "Waiting for server to start...\n";
48     wait_port_timeout($port, 30);
49 } elsif ($pid == 0) {
50     # child process
51     unshift @INC, "$tmpdir/TestApp/lib", "$FindBin::Bin/../../lib";
52     require TestApp;
53
54     my $psgi_app = TestApp->apply_default_middlewares(TestApp->psgi_app);
55     Plack::Loader->auto(port => $port)->run(builder {
56         mount '/test_prefix' => $psgi_app;
57         mount '/' => sub {
58             return [501, ['Content-Type' => 'text/plain'], ['broken tests']];
59         };
60     });
61
62     exit 0;
63 } else {
64     die "fork failed: $!";
65 }
66
67 # run the testsuite against the HTTP server
68 $ENV{CATALYST_SERVER} = "http://localhost:$port/test_prefix";
69
70 chdir '..';
71
72 my $return;
73 if ( $single_test ) {
74     $return = system( "$^X -Ilib/ $single_test" );
75 }
76 else {
77     $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t'));
78 }
79
80 # shut it down
81 kill 'INT', $pid;
82
83 # clean up
84 rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";
85
86 is( $return, 0, 'live tests' );
87
88 # kill 'INT' doesn't exist in Windows, so to prevent child hanging,
89 # this process will need to commit seppuku to clean up the children.
90 if ($^O eq 'MSWin32') {
91     # Furthermore, it needs to do it 'politely' so that TAP doesn't
92     # smell anything 'dubious'.
93     require Win32::Process;  # core in all versions of Win32 Perl
94     Win32::Process::KillProcess($$, $return);
95 }
96
97 sub wait_port_timeout {
98     my ($port, $timeout) = @_;
99
100     wait_port($port, $timeout * 10) and return;
101
102     die "Server did not start within $timeout seconds";
103 }
104
105 sub prove {
106     my (@tests) = @_;
107     if (!(my $pid = fork)) {
108         require TAP::Harness;
109
110         my $aggr = -e '.aggregating';
111         my $harness = TAP::Harness->new({
112             ($aggr ? (test_args => \@tests) : ()),
113             lib => ['lib'],
114         });
115
116         my $aggregator = $aggr
117             ? $harness->runtests('t/aggregate.t')
118             : $harness->runtests(@tests);
119
120         exit $aggregator->has_errors ? 1 : 0;
121     } else {
122         waitpid $pid, 0;
123         return $?;
124     }
125 }