39f0fd8bac43657df3b563d3171a1eb7584db539
[catagits/Catalyst-Controller-WrapCGI.git] / t / cgibin.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin '$Bin';
7 use lib "$Bin/lib";
8
9 use Test::More;
10 use HTTP::Request::Common;
11
12 my %orig_sig;
13 BEGIN {
14     %orig_sig = %SIG;
15 # perl < 5.8.9 won't set a %SIG entry to undef, it sets it to ''
16     %orig_sig = map { defined $_ ? $_ : '' } %orig_sig
17         if $] < 5.008009;
18 }
19
20 use Catalyst::Test 'TestCGIBin';
21
22 # this should be ignored
23 $ENV{MOD_PERL} = "mod_perl/2.0";
24
25 is_deeply \%SIG, \%orig_sig, '%SIG is preserved on compile';
26
27 my $response = request POST '/my-bin/path/test.pl', [
28     foo => 'bar',
29     bar => 'baz'
30 ];
31
32 is($response->content, 'foo:bar bar:baz', 'POST to Perl CGI File');
33
34 $response = request '/my-bin/path/test.pl?foo=bar&bar=baz';
35
36 is($response->content, 'foo:bar bar:baz',
37     'Perl CGI File invoked with query params');
38
39 $response = request POST '/my-bin/exit.pl', [
40     name => 'world',
41 ];
42
43 is($response->content, 'hello world', 'POST to Perl CGI with exit()');
44
45 $response = request POST '/my-bin/exit.pl', [
46     name => 'world',
47     exit => 17,
48 ];
49
50 is($response->code, 500, 'POST to Perl CGI with nonzero exit()');
51
52 $response = request '/my-bin/ignored.cgi';
53
54 is($response->code, 500, "file not matching 'cgi_file_pattern' is ignored");
55
56 $response = request POST '/cgihandler/mtfnpy', [
57     foo => 'bar',
58     bar => 'baz'
59 ];
60
61 is($response->content, 'foo:bar bar:baz',
62     'POST to Perl CGI File through a forward via cgi_action');
63
64 $response = request '/my-bin/path/testdata.pl';
65 is($response->content, "testing\n",
66     'scripts with __DATA__ sections work');
67
68 $response = request '/my-bin/pathinfo.pl/path/info';
69 is($response->content, '/path/info',
70     'PATH_INFO works');
71
72 ok request '/my-bin/sigs.pl';
73
74 is_deeply \%SIG, \%orig_sig, '%SIG is preserved';
75
76 SKIP: {
77     skip "Can't run shell scripts on non-*nix", 1
78         if $^O eq 'MSWin32' || $^O eq 'VMS';
79
80 # for some reason the +x is not preserved in the dist
81     system "chmod +x $Bin/lib/TestCGIBin/root/cgi-bin/test.sh";
82     system "chmod +x $Bin/lib/TestCGIBin/root/cgi-bin/exit_nonzero.sh";
83
84     is(get('/my-bin/test.sh'), "Hello!\n", 'Non-Perl CGI File');
85
86     $response = request GET '/my-bin/exit_nonzero.sh';
87     is $response->code, 500, 'Non-Perl CGI with non-zero exit dies';
88 }
89
90 { $response = get('/my-bin/time.pl');
91   sleep 1;
92   my $response_2 =  get('/my-bin/time.pl');
93   isnt( $response, $response_2, 'cgis are getting invoked each time' );
94 }
95
96 done_testing;