Merge branch 'master' into gsoc_breadboard
[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->apply_default_middlewares(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 # kill 'INT' doesn't exist in Windows, so to prevent child hanging,
81 # this process will need to commit seppuku to clean up the children.
82 if ($^O eq 'MSWin32') {
83     # Furthermore, it needs to do it 'politely' so that TAP doesn't 
84     # smell anything 'dubious'.
85     require Win32::Process;  # core in all versions of Win32 Perl
86     Win32::Process::KillProcess($$, $return);
87 }
88
89 sub wait_port_timeout {
90     my ($port, $timeout) = @_;
91
92     # wait_port waits for 10 seconds
93     for (1 .. int($timeout / 10)) { # meh, good enough.
94         try { wait_port $port; 1 } and return;
95     }
96
97     die "Server did not start within $timeout seconds";
98 }
99
100 sub prove {
101     my (@tests) = @_;
102     if (!(my $pid = fork)) {
103         require TAP::Harness;
104
105         my $aggr = -e '.aggregating';
106         my $harness = TAP::Harness->new({
107             ($aggr ? (test_args => \@tests) : ()),
108             lib => ['lib'],
109         });
110
111         my $aggregator = $aggr
112             ? $harness->runtests('t/aggregate.t')
113             : $harness->runtests(@tests);
114
115         exit $aggregator->has_errors ? 1 : 0;
116     } else {
117         waitpid $pid, 0;
118         return $?;
119     }
120 }