简体   繁体   中英

Set perl cgi query_string offline

I'd like to write some unit tests for my cgi script. I wrote my script as a modulino (script which could be a module depending on context) and I would like to test its functionality but also make sure that all parameters in query_string are set as they should.

I'd tried doing something like this:

$ENV{'QUERY_STRING'} = 'param1=some_param';
my $cgi = CGI->new;
print "param1= ".$cgi->param("param1")."\n";

But it seems to be completely ignoring that. Is there a way to set a query string without actually doing GET method?

You can use command line arguments with CGI.pm.

$ index.pl param1=some_param foo=bar

Those will show up in the script. But that is still inconvenient for unittesting your application. If there is a webserver there anyway, you can use Test::WWW::Mechanize .

I think I have found a solution:

$ENV{QUERY_STRING} = 'engine=sample';
$ENV{REQUEST_METHOD} = 'GET';
$ENV{GATEWAY_INTERFACE} = 'CGI/1.1';

Apparentely $ENV{QUERY_STRING} is not enough for this to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM