Use Ref::Util where appropriate
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_script_fastcgi.t
1 use strict;
2 use warnings;
3
4 use FindBin qw/$Bin/;
5 use lib "$Bin/../lib";
6
7 use Test::More;
8 use Test::Fatal;
9
10 use Ref::Util qw(is_plain_hashref);
11
12 use Catalyst::Script::FastCGI;
13
14 local our $fake_handler = \42;
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
32 sub testOption {
33     my ($argstring, $resultarray) = @_;
34
35     local @ARGV = @$argstring;
36     local @TestAppToTestScripts::RUN_ARGS;
37     is exception {
38         TestFastCGIScript->new_with_options(application_name => 'TestAppToTestScripts')->run;
39     }, undef, "new_with_options";
40     # First element of RUN_ARGS will be the script name, which we don't care about
41     shift @TestAppToTestScripts::RUN_ARGS;
42
43     my $server = pop @TestAppToTestScripts::RUN_ARGS;
44     is $server, $fake_handler, 'Loaded Plack handler gets passed to the app';
45
46     if (scalar(@TestAppToTestScripts::RUN_ARGS) && is_plain_hashref($TestAppToTestScripts::RUN_ARGS[-1])) {
47         is ref(delete($TestAppToTestScripts::RUN_ARGS[-1]->{argv})), 'ARRAY';
48         is ref(delete($TestAppToTestScripts::RUN_ARGS[-1]->{extra_argv})), 'ARRAY';
49     }
50
51     is_deeply \@TestAppToTestScripts::RUN_ARGS, $resultarray, "is_deeply comparison";
52 }
53
54 # Returns the hash expected when no flags are passed
55 sub opthash {
56     return {
57         (map { ($_ => undef) } qw(pidfile keep_stderr detach nproc manager)),
58         proc_title => 'perl-fcgi-pm [TestAppToTestScripts]',
59         @_,
60     };
61 }
62
63
64 # Test default (no opts/args behaviour)
65 testOption( [ qw// ], [undef, opthash()] );
66
67 # listen socket
68 testOption( [ qw|-l /tmp/foo| ], ['/tmp/foo', opthash()] );
69 testOption( [ qw/-l 127.0.0.1:3000/ ], ['127.0.0.1:3000', opthash()] );
70
71 #daemonize           -d --daemon
72 testOption( [ qw/-d/ ], [undef, opthash(detach => 1)] );
73 testOption( [ qw/--daemon/ ], [undef, opthash(detach => 1)] );
74
75 # pidfile        -pidfile -p                 --pid --pidfile
76 testOption( [ qw/--pidfile cat.pid/ ], [undef, opthash(pidfile => 'cat.pid')] );
77 testOption( [ qw/--pid cat.pid/ ], [undef, opthash(pidfile => 'cat.pid')] );
78 testOption( [ qw/-p cat.pid/ ], [undef, opthash(pidfile => 'cat.pid')] );
79
80 # manager
81 testOption( [ qw/--manager foo::bar/ ], [undef, opthash(manager => 'foo::bar')] );
82 testOption( [ qw/-M foo::bar/ ], [undef, opthash(manager => 'foo::bar')] );
83
84 # keeperr
85 testOption( [ qw/--keeperr/ ], [undef, opthash(keep_stderr => 1)] );
86 testOption( [ qw/-e/ ], [undef, opthash(keep_stderr => 1)] );
87
88 # nproc
89 testOption( [ qw/--nproc 6/ ], [undef, opthash(nproc => 6)] );
90 testOption( [ qw/--n 6/ ], [undef, opthash(nproc => 6)] );
91
92 # proc_title
93 testOption( [ qw/--proc_title foo/ ], [undef, opthash(proc_title => 'foo')] );
94
95 done_testing;