Testing CGI.pm-using code

Testing CGI.pm-using code

am 11.01.2008 23:06:26 von kj

Here's the problem. I'm trying to test some code that uses CGI.pm,
running on Linux. I want to do this "locally", without involving
an HTTP server (Apache, etc.).

Each of the intended tests will result in the execution of the
following code from CGI.pm:

read(\*STDIN, $$buff, $len, $offset);

I want the input that is read by this line to come from a scalar
variable (a different one for each test). But I can't get CGI to
read different inputs each time.

The following snippet illustrates the problem:

use CGI ();

sub test {
my $in = shift;
$ENV{ REQUEST_METHOD } = 'POST';
$ENV{ CONTENT_TYPE } = 'text/plain';
$ENV{ CONTENT_LENGTH } = length $in;

close STDIN;
open STDIN, '<', \$in or die $!;

print CGI->new->param( 'POSTDATA' ), "\n";
}

test( 'foo' );
test( 'bar' );

__END__
foo
foo


As you can see, the first call to test() produces the required
output, but the second one doesn't. In fact all calls to test()
will produce the same output as the first one does.

Can anyone suggest a way in which I could, within the execution of
a single script, give CGI different inputs via STDIN?

TIA!

Kynn Jones
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.

Re: Testing CGI.pm-using code

am 11.01.2008 23:51:42 von paduille.4061.mumia.w+nospam

On 01/11/2008 04:06 PM, kj wrote:
> Here's the problem. I'm trying to test some code that uses CGI.pm,
> running on Linux. I want to do this "locally", without involving
> an HTTP server (Apache, etc.).
> [...]

Hello Kynn. Try this for your test function:

sub test {
my $cgi = CGI->new(\$_[0]);
print $cgi->param('POSTDATA'), "\n";
}

I hope that your code was not meant to test reading from STDIN because
you don't need STDIN to set up a CGI.pm object.

__HTH__

Re: Testing CGI.pm-using code

am 11.01.2008 23:53:41 von xhoster

kj wrote:
> Here's the problem. I'm trying to test some code that uses CGI.pm,
> running on Linux. I want to do this "locally", without involving
> an HTTP server (Apache, etc.).

What is it you are trying to test?

>
> Each of the intended tests will result in the execution of the
> following code from CGI.pm:
>
> read(\*STDIN, $$buff, $len, $offset);
>
> I want the input that is read by this line to come from a scalar
> variable (a different one for each test). But I can't get CGI to
> read different inputs each time.
>
> The following snippet illustrates the problem:
>
> use CGI ();
>
> sub test {
> my $in = shift;
> $ENV{ REQUEST_METHOD } = 'POST';
> $ENV{ CONTENT_TYPE } = 'text/plain';
> $ENV{ CONTENT_LENGTH } = length $in;
>
> close STDIN;
> open STDIN, '<', \$in or die $!;
>
> print CGI->new->param( 'POSTDATA' ), "\n";

CGI->new can take an argument.

open my $fh, '<', \$in or die $!;
print CGI->new($fh)->param( 'POSTDATA' ), "\n";

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.