1

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?

serenesat
  • 4,611
  • 10
  • 37
  • 53

2 Answers2

1

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.

simbabque
  • 53,749
  • 8
  • 73
  • 136
1

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.

serenesat
  • 4,611
  • 10
  • 37
  • 53