NAME Test::CGI::Multipart - Test posting of multi-part form data VERSION This document describes Test::CGI::Multipart version 0.0.2 SYNOPSIS use Test::CGI::Multipart; my $tcm = Test::CGI::Multipart; # specify the form parameters $tcm->set_param(name='email',value=>'jim@hacker.com'); $tcm->set_param(name=>'pets',value=> ['Rex', 'Oscar', 'Bidgie', 'Fish']); $tcm->set_param(name=>'first_name',value=>'Jim'); $tcm->set_param(name=>'last_name',value=>'Hacker'); $tcm->upload_file( name=>'file1', file=>'made_up_filename.txt', value=>$content ); $tcm->upload_file( name=>'file1', file=>'made_up_filename.blah', value=>$content_blah, type=>'application/blah' ); # Behind the scenes this will fake the browser and web server behaviour # with regard to environment variables, MIME format and standard input. my $cgi = $tcm->create_cgi; # Okay now we have a CGI object which we can pass into the code # that needs testing and run the form handling various tests. DESCRIPTION It is quite difficult to write test code to capture the behaviour of CGI or similar objects handling forms that include a file upload. Such code needs to harvest the parameters, build file content in MIME format, set the environment variables accordingly and pump it into the the standard input of the required CGI object. This module provides simple methods so that having prepared suitable content, the test script can simulate the submission of web forms including file uploads. However we also recognise that a test script is not always the best place to prepare content. Rather a test script would rather specify requirements for a file a upload: type, size, mismatches between the file name and its contents and so on. This module cannot hope to provide such open ended functionality but it can provide extension mechanisms. This module works with CGI (the default), CGI::Minimal and CGI::Simple. In principle it ought to work with all equivalent modules however each module has a slightly different interface when it comes to file uploads and so requires slightly different test code. INTERFACE Several of the methods below take named parameters. For convenience we define those parameters here: "cgi" This option defines the CGI module. It should be a scalar consisting only of alphanumeric characters and "::". It defaults to 'CGI'. "name" This is the name of form parameter. It must be a scalar. "value" This is the value of the form parameter. It should either be a scalar or an array reference of scalars. "file" Where a form parameter represents a file, this is the name of that file. "type" The MIME type of the content. This defaults to 'text/plain'. "ua" The HTTP_USER_AGENT environment variable. This defaults to 'Test::CGI::Multipart'. new An instance of this class might best be thought of as a "CGI object factory". The constructor takes no parameters. create_cgi This returns a CGI object created according to the specification encapsulated in the object. The exact mechanics are as follows: The parameters are packaged up in MIME format. The environment variables are set. A pipe is created. The far end of the pipe is attached to our standard input and the MIME content is pushed through the pipe. The appropriate CGI class is required. Uploads are enabled if the CGI class is CGI::Simple. Global variables are reset for CGI and CGI::Minimal. The CGI object is created and returned. As far as I can see this simulates what happens when a CGI script processes a multi-part POST form. One can specify a different CGI class using the "cgi" named parameter. One can set the HTTP_USER_AGENT environment variable with the "ua" parameter. set_param This can be used to set a single form parameter. It takes two named arguments "name" and "value". Note that this method overrides any previous settings including file uploads. get_param This retrieves a single form parameter. It takes a single named parameter: "name". The data returned will be a list either of scalar values or (in the case of a file upload) of HASHREFs. The HASHREFs would have the following fields: "file", "value" and "type" representing the parameter name, the file name, the content and the MIME type respectively. get_names This returns a list of stashed parameter names. upload_file In the absence of any defined callbacks, this method takes three mandatory named parameters: "name", "file" and "value" and one optional parameter "type". If there are any callbacks then the parameters are passed through each of the callbacks and must meet the standard parameter requirements by the time all the callbacks have been called. Unlike the "set_param" method this will not override previous settings for this parameter but will add. However setting a normal parameter and then an upload on the same name will throw an error. register_callback Callbacks are used by the "upload_file" method, to allow a file to be specified by properties rather than strict content. This method takes a single named parameter called "callback", which adds that callback to an internal array of callbacks. The idea being that the "upload_file" method can take any arguments you like so long as after all the callbacks have been applied, the parameters consist of "name", "file", "value" and possibly "type". A callback should take and return a single hash reference. DIAGNOSTICS "unexpected data structure" During the construction of the MIME data, the internal data structure turned out to have unexpected features. Since we control that data structure that should not happen. "mismatch: is %s a file upload or not" The parameter was being used for both for file upload and normal parameters. CONFIGURATION AND ENVIRONMENT Test::CGI::Multipart requires no configuration files or environment variables. However it should be noted that the module will overwrite the following environment variables: REQUEST_METHOD CONTENT_LENGTH CONTENT_TYPE HTTP_USER_AGENT INCOMPATIBILITIES I would like to get this working with CGI::Lite::Request and Apache::Request if that makes sense. So far I have not managed that. BUGS AND LIMITATIONS No bugs have been reported. Please report any bugs or feature requests to "bug-test-cgi-multipart@rt.cpan.org", or through the web interface at . This module depends upon MIME::Tools. Unfortunately that module does not handle newlines quite correctly. That seems to work fine for email but does not work with CGI. I have looked at MIME::Fast and MIME::Lite but MIME::Tools combined with a hack seems the best that can be done at the moment. Sooner or later someone is going to hit the limitations of that hack. AUTHOR Nicholas Bamber "" LICENCE AND COPYRIGHT Copyright (c) 2010, Nicholas Bamber "". All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. DISCLAIMER OF WARRANTY BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.