Still being something of a newbie (AKA the more I know, the more I realize how little I know), I got a bit stuck recently whilst trying to write some test scripts for uploading an image to a CGI::Application web page. So I had a look at the test scripts for CGI. It's all pretty much laid out in http://cpansearch.perl.org/src/LDS/CGI.pm-3.49/t/upload.t. This script first sets various environment variables, and then localizes the STDIN, feeds a MIME formatted file into the STDIN and then finally creates a CGI object, which then of course reads the environment variables and the STDIN.

I thought to myself there has to be a better way. I propose to shamelessly rip out the guts of that test script and turn it into a more reusable Test module. I was thinking of calling it "Test::CGI::POST". Although in the Test namespace it would not be a standard test module with ever more esoteric "ok_...." methods. Rather I was thinking of the following more OO interface:

  1. As a convenience when you do "use Test::CGI::POST" you can specify some environment variables. There could also be some options for standard sets of environment variables.
  2. The constructor would take a CODE ref that must return a CGI like object. This CODE ref could be changed later but the module is useless without it so it might as well either be mandatory in the constructor or at least default to returning a plain CGI object.
  3. There would be a "param" method with much the same options as CGI::param.
  4. The key method would be the "create_cgi" method. It would do the trick of tweaking the STDIN, formatting the stashed parameters from the "param" method into a MIME stream and feeding that into STDIN, before calling the create CGI callback, the result of which would be returned.
  5. There could also be some helper methods based upon GD for creating an image of a specified format, width and height.

Edit: I have started work on this at http://github.com/periapt/Test-CGI-Multipart. Nothing useable so far but as a proof of concept it is there.


In reply to RFC: Systemizing CGI upload testing by SilasTheMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.