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

testing 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:


I'm writing a module to help validate file uploads using 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 distribution and didn't find any. I'm looking for suggestions on how I test 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.



Replies are listed 'Best First'.
Re: testing 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 '', Content_Type => 'form-data', Content => [ name => 'Gisle Aas', email => '', gender => 'M', born => '1964', init => ["$ENV{HOME}/.profile"], ]; print $r->as_string;


      While this approach made sense to me, it didn't turn out to work as expected. It looks like the "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.


Re: testing 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' => '', 'HTTP_CONNECTION' => 'TE, close', 'REQUEST_METHOD' => 'POST', 'SCRIPT_URI' => '', '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' => '', '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' => '', 'DOCUMENT_ROOT' => '/home/develop', 'HTTP_HOST' => '' ); use CGI; open(IN,'<t/post_text.txt'); *STDIN = *IN; $q = new CGI;


Re: testing 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?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://249917]
Approved by hardburn
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
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (16 votes). Check out past polls.