generate app for live tests manually rather than using Catalyst::Devel
[catagits/Catalyst-Runtime.git] / xt / author / http-server.t
CommitLineData
040850b3 1use strict;
2use warnings;
3
81f25ce6 4use Test::More tests => 1;
5ac4489b 5use Test::TCP;
4853fb50 6
040850b3 7use File::Path;
8use FindBin;
a526c982 9use Net::EmptyPort qw(wait_port empty_port);
29057674 10use Try::Tiny;
876db346 11use Plack::Builder;
8ae46e98 12use lib 't/lib';
13use MakeTestApp;
040850b3 14
547f8806 15# Run a single test by providing it as the first arg
16my $single_test = shift;
17
8ae46e98 18my $test_app_dir = make_test_app;
040850b3 19
20# spawn the standalone HTTP server
29057674 21my $port = empty_port;
d93d402c 22
23my $pid = fork;
24if ($pid) {
25 # parent.
26 print "Waiting for server to start...\n";
29057674 27 wait_port_timeout($port, 30);
d93d402c 28} elsif ($pid == 0) {
29 # child process
8ae46e98 30 unshift @INC, "$test_app_dir/lib", "$FindBin::Bin/../../lib";
d93d402c 31 require TestApp;
32
1316cc64 33 my $psgi_app = TestApp->apply_default_middlewares(TestApp->psgi_app);
876db346 34 Plack::Loader->auto(port => $port)->run(builder {
35 mount '/test_prefix' => $psgi_app;
36 mount '/' => sub {
37 return [501, ['Content-Type' => 'text/plain'], ['broken tests']];
38 };
39 });
d93d402c 40
41 exit 0;
42} else {
43 die "fork failed: $!";
896c9ed2 44}
32e231eb 45
040850b3 46# run the testsuite against the HTTP server
876db346 47$ENV{CATALYST_SERVER} = "http://localhost:$port/test_prefix";
547f8806 48
e8e8895a 49chdir '..';
50
2f381252 51my $return;
547f8806 52if ( $single_test ) {
e8e8895a 53 $return = system( "$^X -Ilib/ $single_test" );
547f8806 54}
55else {
529d5abc 56 $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t'));
547f8806 57}
040850b3 58
59# shut it down
e1b364f4 60kill 'INT', $pid;
040850b3 61
62# clean up
81f25ce6 63rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";
896c9ed2 64
2f381252 65is( $return, 0, 'live tests' );
55ddccae 66
4d7d4257 67# kill 'INT' doesn't exist in Windows, so to prevent child hanging,
68# this process will need to commit seppuku to clean up the children.
69if ($^O eq 'MSWin32') {
807303a1 70 # Furthermore, it needs to do it 'politely' so that TAP doesn't
4d7d4257 71 # smell anything 'dubious'.
72 require Win32::Process; # core in all versions of Win32 Perl
73 Win32::Process::KillProcess($$, $return);
74}
75
29057674 76sub wait_port_timeout {
77 my ($port, $timeout) = @_;
78
5ac4489b 79 wait_port($port, $timeout * 10) and return;
29057674 80
81 die "Server did not start within $timeout seconds";
896c9ed2 82}
868a7cca 83
84sub prove {
529d5abc 85 my (@tests) = @_;
868a7cca 86 if (!(my $pid = fork)) {
641b0131 87 require TAP::Harness;
e8e8895a 88
e8e8895a 89 my $harness = TAP::Harness->new({
529d5abc 90 lib => ['lib'],
e8e8895a 91 });
92
a1e953c8 93 my $aggregator = $harness->runtests(@tests);
641b0131 94
95 exit $aggregator->has_errors ? 1 : 0;
868a7cca 96 } else {
97 waitpid $pid, 0;
98 return $?;
99 }
100}