t/50-cgi: %post data parse tests
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 20 Feb 2012 07:57:31 +0000 (08:57 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 19 Nov 2013 00:53:37 +0000 (01:53 +0100)
Test user input handling by optionally reading from matching *.txt files.

t/50-cgi.t
t/50-cgi/60-empty %post.html [new file with mode: 0644]
t/50-cgi/60-empty %post.plp [new file with mode: 0644]
t/50-cgi/61-%post set.html [new file with mode: 0644]
t/50-cgi/61-%post set.plp [new file with mode: 0644]
t/50-cgi/62-%post data.html [new file with mode: 0644]
t/50-cgi/62-%post data.plp [new file with mode: 0644]
t/50-cgi/62-%post data.txt [new file with mode: 0644]

index 6b0661a2bd891e7320d5c34ad1aa0b814dc95b76..f58566f447ff026b561479f85ab490d0b32340c3 100644 (file)
@@ -23,7 +23,7 @@ eval {
 eval { require PerlIO::scalar };
 plan skip_all => "PerlIO required (perl 5.8) to test PLP" if $@;
 
-plan tests => 21;
+plan tests => 24;
 
 require_ok('PLP::Backend::CGI') or BAIL_OUT();
 
@@ -36,7 +36,7 @@ my $ORGDIR = Cwd::getcwd();
 open ORGOUT, '>&', *STDOUT;
 
 sub plp_is {
-       my ($test, $src, $expect, $env) = @_;
+       my ($test, $src, $expect, $env, $in) = @_;
        local $Test::Builder::Level = $Test::Builder::Level + 1;
 
        %ENV = (
@@ -54,6 +54,13 @@ sub plp_is {
                $env ? %{$env} : (),
        ); # Apache/2.2.4 CGI environment
 
+       if (defined $in) {
+               $ENV{CONTENT_LENGTH} = length $in;
+               $ENV{CONTENT_TYPE} = 'application/x-www-form-urlencoded';
+               close STDIN;
+               open STDIN, '<', $in;
+       }
+
        close STDOUT;
        open STDOUT, '>', \my $output;  # STDOUT buffered to scalar
        select STDOUT;  # output before start() (which selects PLPOUT)
@@ -96,6 +103,7 @@ sub plp_ok {
        (my $name = $file) =~ s/[.][^.]+$//;
        $file = "$name.html";
        my $infile = delete $replace{-input} // "$name.plp";
+       my $addin = -e "$name.txt" && "$name.txt";
        $name =~ s/^(\d*)-// and $name .= " ($1)";
 
        my $out = eval {
@@ -121,7 +129,7 @@ sub plp_ok {
                <eval \s+ line="([^"]*)"> (.*?) </eval>
        }{ getwarning($2, $1, $infile) }msxge;
 
-       plp_is($name, $infile, $out, $env);
+       plp_is($name, $infile, $out, $env, $addin);
 }
 
 # 0*: permission checks using generated dummy files
@@ -169,7 +177,6 @@ SKIP: {
 # 4*-7*: apache environment (default)
 plp_ok($_) for glob '[4-7]*.html';
 
-#TODO: %post
 #TODO: %fields
 #TODO: %cookie
 
diff --git a/t/50-cgi/60-empty %post.html b/t/50-cgi/60-empty %post.html
new file mode 100644 (file)
index 0000000..713860d
--- /dev/null
@@ -0,0 +1,3 @@
+$HEAD
+PLPdummy
+0
diff --git a/t/50-cgi/60-empty %post.plp b/t/50-cgi/60-empty %post.plp
new file mode 100644 (file)
index 0000000..8dc76c4
--- /dev/null
@@ -0,0 +1,3 @@
+<:
+print join "\n", %post;  # expect dummy key on first access
+print scalar keys %post;  # empty now
diff --git a/t/50-cgi/61-%post set.html b/t/50-cgi/61-%post set.html
new file mode 100644 (file)
index 0000000..33727f2
--- /dev/null
@@ -0,0 +1,4 @@
+$HEAD
+ok
+
+1
diff --git a/t/50-cgi/61-%post set.plp b/t/50-cgi/61-%post set.plp
new file mode 100644 (file)
index 0000000..147d6c4
--- /dev/null
@@ -0,0 +1,4 @@
+<:
+%post = (Foo => 'ok');
+print join "\n", @post{'Foo', 'foo', '@Foo'};
+print scalar keys %post;
diff --git a/t/50-cgi/62-%post data.html b/t/50-cgi/62-%post data.html
new file mode 100644 (file)
index 0000000..3ca132a
--- /dev/null
@@ -0,0 +1,2 @@
+$HEAD
+3{'foo' => 'b a r','@foo' => ['','b a r']}
diff --git a/t/50-cgi/62-%post data.plp b/t/50-cgi/62-%post data.plp
new file mode 100644 (file)
index 0000000..1fc42b5
--- /dev/null
@@ -0,0 +1,5 @@
+<:
+print scalar keys %post;
+
+require Data::Dumper;
+print Data::Dumper->new([\%post])->Indent(0)->Terse(1)->Dump;
diff --git a/t/50-cgi/62-%post data.txt b/t/50-cgi/62-%post data.txt
new file mode 100644 (file)
index 0000000..82ccf07
--- /dev/null
@@ -0,0 +1 @@
+foo=&foo=b+a%20r
\ No newline at end of file