t: move i/o testing routines to Test::PLP module
[perl/plp/.git] / t / 50-cgi.t
1 use strict;
2 use warnings;
3
4 use Cwd;
5 use File::Basename qw( dirname );
6 use File::Spec;
7 use Test::More;
8
9 plan tests => 25;
10
11 use_ok('Test::PLP');
12
13 $PLP::use_cache = 0 if $PLP::use_cache;
14 #TODO: caching on (change file names)
15
16 chdir File::Spec->catdir(dirname($0), '50-cgi')
17         or BAIL_OUT('cannot change to test directory ./50-cgi/');
18 my $ORGDIR = '.'; # Cwd::getcwd();
19
20 # 0*: permission checks using generated dummy files
21 SKIP:
22 for my $file (glob '0*.html') {
23         $file =~ s/[.]html$/.plp/;
24         my ($mode) = $file =~ /^..-(\d*)\b/;
25         eval {
26                 if ($mode eq 404) {
27                         return 1;  # do not create
28                 }
29
30                 # prepare input
31                 open my $out, '>', $file or die "cannot generate source file ($!)\n";
32                 print {$out} 'ok';
33
34                 if ($mode eq 403) {
35                         chmod 0244, $file or die "cannot change permissions ($!)\n";
36                 }
37
38                 return -e $file;
39         } or chomp $@, skip("$file: $@", 1);  # ignore generation failure
40
41         plp_ok($file);
42         eval { unlink $file };  # clean up
43 }
44
45 # 1*-2*: generic tests with standard environment
46 plp_ok($_) for glob '[12]*.html';
47
48 # 3*: error tests depending on warning message
49 SKIP: {
50         my @inctests = glob '3*.html';
51
52         my $INCFILE = File::Spec->rel2abs("$ORGDIR/missinginclude");
53         if (open my $dummy, "<", $INCFILE) {  # like PLP::source will
54                 fail("file missinginclude shouldn't exist");
55                 skip("missinginclude tests (3*)", @inctests - 1);
56         }
57         my $INCWARN = qq{Can't open "$INCFILE" ($!)};
58
59         plp_ok($_, INCWARN => $INCWARN) for @inctests;
60 }
61
62 # 4*-6*: apache environment (default)
63 plp_ok($_) for glob '[4-6]*.html';
64
65 #TODO: %fields
66 #TODO: %cookie
67
68 # 7*: multipart posts
69 TODO: {
70         local $TODO = 'future feature';
71         plp_ok($_, -env => {
72                 CONTENT_TYPE => 'multipart/form-data; boundary=knip',
73         }) for glob '7*.html';
74 }
75
76 # 8*: lighttpd environment
77 plp_ok($_, -env => {
78         # lighttpd/1.4.7 CGI environment
79         REQUEST_METHOD => 'GET',
80         REQUEST_URI => "/$_/test/123",
81         QUERY_STRING => 'test=1&test=2',
82         GATEWAY_INTERFACE => 'CGI/1.1',
83         
84         SCRIPT_NAME => "/$_", #XXX: .plp?
85         SCRIPT_FILENAME => "$ORGDIR/$_",
86         PATH_INFO => '/test/123',
87         PATH_TRANSLATED => undef,
88         DOCUMENT_ROOT => undef,
89 }) for glob '8*.plp';
90