2N/A#!/usr/local/bin/perl -w
2N/A
2N/A#################################################################
2N/A# Emanuele Zeppieri, Mark Stosberg #
2N/A# Shamelessly stolen from Data::FormValidator and CGI::Upload #
2N/A#################################################################
2N/A
2N/Ause strict;
2N/A
2N/Ause Test::More 'no_plan';
2N/A
2N/Ause CGI;
2N/A
2N/A#-----------------------------------------------------------------------------
2N/A# %ENV setup.
2N/A#-----------------------------------------------------------------------------
2N/A
2N/Amy %myenv;
2N/A
2N/ABEGIN {
2N/A %myenv = (
2N/A 'SCRIPT_NAME' => '/test.cgi',
2N/A 'SERVER_NAME' => 'perl.org',
2N/A 'HTTP_CONNECTION' => 'TE, close',
2N/A 'REQUEST_METHOD' => 'POST',
2N/A 'SCRIPT_URI' => 'http://www.perl.org/test.cgi',
2N/A 'CONTENT_LENGTH' => 3285,
2N/A 'SCRIPT_FILENAME' => '/home/usr/test.cgi',
2N/A 'SERVER_SOFTWARE' => 'Apache/1.3.27 (Unix) ',
2N/A 'HTTP_TE' => 'deflate,gzip;q=0.3',
2N/A 'QUERY_STRING' => '',
2N/A 'REMOTE_PORT' => '1855',
2N/A 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)',
2N/A 'SERVER_PORT' => '80',
2N/A 'REMOTE_ADDR' => '127.0.0.1',
2N/A 'CONTENT_TYPE' => 'multipart/form-data; boundary=xYzZY',
2N/A 'SERVER_PROTOCOL' => 'HTTP/1.1',
2N/A 'PATH' => '/usr/local/bin:/usr/bin:/bin',
2N/A 'REQUEST_URI' => '/test.cgi',
2N/A 'GATEWAY_INTERFACE' => 'CGI/1.1',
2N/A 'SCRIPT_URL' => '/test.cgi',
2N/A 'SERVER_ADDR' => '127.0.0.1',
2N/A 'DOCUMENT_ROOT' => '/home/develop',
2N/A 'HTTP_HOST' => 'www.perl.org'
2N/A );
2N/A
2N/A for my $key (keys %myenv) {
2N/A $ENV{$key} = $myenv{$key};
2N/A }
2N/A}
2N/A
2N/AEND {
2N/A for my $key (keys %myenv) {
2N/A delete $ENV{$key};
2N/A }
2N/A}
2N/A
2N/A#-----------------------------------------------------------------------------
2N/A# Simulate the upload (really, multiple uploads contained in a single stream).
2N/A#-----------------------------------------------------------------------------
2N/A
2N/Amy $q;
2N/A
2N/A{
2N/A local *STDIN;
2N/A open STDIN, '<../lib/CGI/t/upload_post_text.txt'
2N/A or die 'missing test file t/upload_post_text.txt';
2N/A binmode STDIN;
2N/A $q = CGI->new;
2N/A}
2N/A
2N/A#-----------------------------------------------------------------------------
2N/A# Check that the file names retrieved by CGI are correct.
2N/A#-----------------------------------------------------------------------------
2N/A
2N/Ais( $q->param('does_not_exist_gif'), 'does_not_exist.gif', 'filename_2' );
2N/Ais( $q->param('100;100_gif') , '100;100.gif' , 'filename_3' );
2N/Ais( $q->param('300x300_gif') , '300x300.gif' , 'filename_4' );
2N/A
2N/A{
2N/A my $test = "multiple file names are handled right with same-named upload fields";
2N/A my @hello_names = $q->param('hello_world');
2N/A is ($hello_names[0],'goodbye_world.txt',$test. "...first file");
is ($hello_names[1],'hello_world.txt',$test. "...second file");
}
#-----------------------------------------------------------------------------
# Now check that the upload method works.
#-----------------------------------------------------------------------------
ok( defined $q->upload('does_not_exist_gif'), 'upload_basic_2' );
ok( defined $q->upload('100;100_gif') , 'upload_basic_3' );
ok( defined $q->upload('300x300_gif') , 'upload_basic_4' );
{
my $test = "file handles have expected length for multi-valued field. ";
my ($goodbye_fh,$hello_fh) = $q->upload('hello_world');
# Go to end of file;
seek($goodbye_fh,0,2);
# How long is the file?
is(tell($goodbye_fh), 15, "$test..first file");
# Go to end of file;
seek($hello_fh,0,2);
# How long is the file?
is(tell($hello_fh), 13, "$test..second file");
}
{
my $test = "300x300_gif has expected length";
my $fh1 = $q->upload('300x300_gif');
is(tell($fh1), 0, "First object: filehandle starts with position set at zero");
# Go to end of file;
seek($fh1,0,2);
# How long is the file?
is(tell($fh1), 1656, $test);
}
my $q2 = CGI->new;
{
my $test = "Upload filehandles still work after calling CGI->new a second time";
$q->param('new','zoo');
is($q2->param('new'),undef,
"Reality Check: params set in one object instance don't appear in another instance");
my $fh2 = $q2->upload('300x300_gif');
is(tell($fh2), 0, "...so the state of a file handle shouldn't be carried to a new object instance, either.");
# Go to end of file;
seek($fh2,0,2);
# How long is the file?
is(tell($fh2), 1656, $test);
}
{
my $test = "multi-valued uploads are reset properly";
my ($dont_care, $hello_fh2) = $q2->upload('hello_world');
is(tell($hello_fh2), 0, $test);
}
# vim: nospell