Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

testing CGI.pm file uploads?

by markjugg (Curate)
on Apr 11, 2003 at 16:27 UTC ( #249917=perlquestion: print w/replies, xml ) Need Help??

markjugg has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm writing a module to help validate file uploads using CGI.pm and Data::FormValidator. I'm writing test scripts now, and would like to simulate the file upload so I can test the validation.

I checked to see if there were any tests for this in the CGI.pm distribution and didn't find any. I'm looking for suggestions on how I test CGI.pm file upload functionality from a Perl script. If the solution can work around requiring other modules and net access for the test script, it's a plus.

Thanks!

-mark

Replies are listed 'Best First'.
Re: testing CGI.pm file uploads?
by chromatic (Archbishop) on Apr 11, 2003 at 17:00 UTC

    Tie a filehandle. Stick the text of an HTTP POST with an uploaded file in there. Pass the filehandle to CGI->new(). Process the upload as you normally would. Check to see that you've received the file as usual.

      Thanks chromatic, Your suggestion got me poking around to create such a file. Here's some starter code for someone else wanting to do this:
      use HTTP::Request::Common; use LWP::UserAgent; # This form data will be printed to STDOUT my $r = POST 'http://www.perl.org/survey.cgi', Content_Type => 'form-data', Content => [ name => 'Gisle Aas', email => 'gisle@aas.no', gender => 'M', born => '1964', init => ["$ENV{HOME}/.profile"], ]; print $r->as_string;

      -mark

      While this approach made sense to me, it didn't turn out to work as expected. It looks like the CGI.pm "new" function is expecting a basic name/value format from the file handle, and can't currently handle a raw HTTP POST request.

      Another approach that occured to me is to capture the environment variables of a real HTTP POST, and then set them again inside the test script.

      -mark

Re: testing CGI.pm file uploads?
by markjugg (Curate) on Apr 11, 2003 at 19:15 UTC
    Ok, I've solved this now. It took a few steps. The first one was generating a file that contained a request (inspired by chromatic's suggestion). The basics of that are given above. Additionally, I needed to trim off the headers (so the first line looks like this: --xYzZY), because that's the way that the Perl script expects to receive it.

    Next, I actually sent the request to a Perl script, and then dumped out %ENV to capture that. This gives me the correct content length, and other needed environment variables

    Finally, I created the test script, which is fooled into thinking that is is normal file upload, because all the environmental variables are set, and the data is coming in on STDIN. I tried tying the filehandle directly instead of duplicating it with STDIN, but that didn't work.

    So with my working input file, here's a snippet from the test script:

    %ENV = ( %ENV, 'SCRIPT_NAME' => '/test.cgi', 'SERVER_NAME' => 'perl.org', 'HTTP_CONNECTION' => 'TE, close', 'REQUEST_METHOD' => 'POST', 'SCRIPT_URI' => 'http://www.perl.org/test.cgi', 'CONTENT_LENGTH' => '2986', 'SCRIPT_FILENAME' => '/home/usr/test.cgi', 'SERVER_SOFTWARE' => 'Apache/1.3.27 (Unix) ', 'HTTP_TE' => 'deflate,gzip;q=0.3', 'QUERY_STRING' => '', 'REMOTE_PORT' => '1855', 'HTTP_USER_AGENT' => 'libwww-perl/5.69', 'SERVER_PORT' => '80', 'REMOTE_ADDR' => '127.0.0.1', 'CONTENT_TYPE' => 'multipart/form-data; boundary=xYzZY', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'PATH' => '/usr/local/bin:/usr/bin:/bin', 'REQUEST_URI' => '/test.cgi', 'GATEWAY_INTERFACE' => 'CGI/1.1', 'SCRIPT_URL' => '/test.cgi', 'SERVER_ADDR' => '127.0.0.1', 'DOCUMENT_ROOT' => '/home/develop', 'HTTP_HOST' => 'www.perl.org' ); use CGI; open(IN,'<t/post_text.txt'); *STDIN = *IN; $q = new CGI;

    -mark

Re: testing CGI.pm file uploads?
by maksl (Pilgrim) on Apr 11, 2003 at 18:16 UTC
    don't know if it's offtopic, but you could check:
    A) if your server does allow cgi uploads: $CGI::DISABLE_UPLOAD
    B) if there is a file size limit: $CGI::POST_MAX
    recently learned on them in thread "Uploading files: can't find contents?"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://249917]
Approved by hardburn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2023-06-03 18:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (16 votes). Check out past polls.

    Notices?