Use Ref::Util where appropriate
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_script_fastcgi.t
CommitLineData
78dc94f2 1use strict;
2use warnings;
3
4use FindBin qw/$Bin/;
5use lib "$Bin/../lib";
6
7use Test::More;
2a56ace9 8use Test::Fatal;
78dc94f2 9
dd4530ec 10use Ref::Util qw(is_plain_hashref);
11
78dc94f2 12use Catalyst::Script::FastCGI;
13
665a72a2 14local our $fake_handler = \42;
e40d69a7 15
16{
17 package TestFastCGIScript;
18 use Moose;
19 use namespace::autoclean;
20
21 extends 'Catalyst::Script::FastCGI';
22
23 # Avoid loading the real plack engine, as that will load FCGI and fail if
24 # it's not there. We don't really need a full engine anyway as the overriden
25 # MyApp->run will just capture its arguments and return without delegating
26 # to the engine to run things.
27 override load_engine => sub { $fake_handler };
28
29 __PACKAGE__->meta->make_immutable;
30}
31
32sub testOption {
33 my ($argstring, $resultarray) = @_;
34
35 local @ARGV = @$argstring;
36 local @TestAppToTestScripts::RUN_ARGS;
2a56ace9 37 is exception {
e40d69a7 38 TestFastCGIScript->new_with_options(application_name => 'TestAppToTestScripts')->run;
2a56ace9 39 }, undef, "new_with_options";
e40d69a7 40 # First element of RUN_ARGS will be the script name, which we don't care about
41 shift @TestAppToTestScripts::RUN_ARGS;
aee7cdcc 42
e40d69a7 43 my $server = pop @TestAppToTestScripts::RUN_ARGS;
44 is $server, $fake_handler, 'Loaded Plack handler gets passed to the app';
aee7cdcc 45
dd4530ec 46 if (scalar(@TestAppToTestScripts::RUN_ARGS) && is_plain_hashref($TestAppToTestScripts::RUN_ARGS[-1])) {
aee7cdcc 47 is ref(delete($TestAppToTestScripts::RUN_ARGS[-1]->{argv})), 'ARRAY';
48 is ref(delete($TestAppToTestScripts::RUN_ARGS[-1]->{extra_argv})), 'ARRAY';
49 }
50
e40d69a7 51 is_deeply \@TestAppToTestScripts::RUN_ARGS, $resultarray, "is_deeply comparison";
52}
53
54# Returns the hash expected when no flags are passed
55sub opthash {
56 return {
57 (map { ($_ => undef) } qw(pidfile keep_stderr detach nproc manager)),
58 proc_title => 'perl-fcgi-pm [TestAppToTestScripts]',
59 @_,
60 };
61}
62
78dc94f2 63
64# Test default (no opts/args behaviour)
65testOption( [ qw// ], [undef, opthash()] );
66
67# listen socket
68testOption( [ qw|-l /tmp/foo| ], ['/tmp/foo', opthash()] );
69testOption( [ qw/-l 127.0.0.1:3000/ ], ['127.0.0.1:3000', opthash()] );
70
71#daemonize -d --daemon
e1d59dc4 72testOption( [ qw/-d/ ], [undef, opthash(detach => 1)] );
73testOption( [ qw/--daemon/ ], [undef, opthash(detach => 1)] );
78dc94f2 74
ad08ab75 75# pidfile -pidfile -p --pid --pidfile
78dc94f2 76testOption( [ qw/--pidfile cat.pid/ ], [undef, opthash(pidfile => 'cat.pid')] );
77testOption( [ qw/--pid cat.pid/ ], [undef, opthash(pidfile => 'cat.pid')] );
ad08ab75 78testOption( [ qw/-p cat.pid/ ], [undef, opthash(pidfile => 'cat.pid')] );
78dc94f2 79
80# manager
81testOption( [ qw/--manager foo::bar/ ], [undef, opthash(manager => 'foo::bar')] );
82testOption( [ qw/-M foo::bar/ ], [undef, opthash(manager => 'foo::bar')] );
83
84# keeperr
85testOption( [ qw/--keeperr/ ], [undef, opthash(keep_stderr => 1)] );
86testOption( [ qw/-e/ ], [undef, opthash(keep_stderr => 1)] );
87
88# nproc
89testOption( [ qw/--nproc 6/ ], [undef, opthash(nproc => 6)] );
90testOption( [ qw/--n 6/ ], [undef, opthash(nproc => 6)] );
91
8865ee12 92# proc_title
93testOption( [ qw/--proc_title foo/ ], [undef, opthash(proc_title => 'foo')] );
9c74923d 94
78dc94f2 95done_testing;