- CONFIGURATION AND ENVIRONMENT
- BUGS AND LIMITATIONS
- LICENCE AND COPYRIGHT
- DISCLAIMER OF WARRANTY
Test::CGI::Multipart - Test posting of multi-part form data
This document describes Test::CGI::Multipart version 0.0.3
use Test::CGI::Multipart; my $tcm = Test::CGI::Multipart; # specify the form parameters $tcm->set_param(name='email',value=>'firstname.lastname@example.org'); $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.
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.
Several of the methods below take named parameters. For convenience we define those parameters here:
This option defines the CGI module. It should be a scalar consisting only of alphanumeric characters and
::. It defaults to 'CGI'.
This is the name of form parameter. It must be a scalar.
This is the value of the form parameter. It should either be a scalar or an array reference of scalars.
Where a form parameter represents a file, this is the name of that file.
The MIME type of the content. This defaults to 'text/plain'.
The HTTP_USER_AGENT environment variable. This defaults to 'Test::CGI::Multipart'.
An instance of this class might best be thought of as a "CGI object factory". The constructor takes no parameters.
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
This can be used to set a single form parameter. It takes two named arguments
value. Note that this method overrides any previous settings including file uploads.
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:
type representing the parameter name, the file name, the content and the MIME type respectively.
This returns a list of stashed parameter names.
In the absence of any defined callbacks, this method takes three mandatory named parameters:
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.
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.
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
value and possibly
type. A callback should take and return a single hash reference.
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:
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests to
email@example.com, or through the web interface at http://rt.cpan.org.
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.
LICENCE AND COPYRIGHT
Copyright (c) 2010, Nicholas Bamber
<firstname.lastname@example.org>. 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.