1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#!/usr/local/bin/perl -w
#################################################################
# Emanuele Zeppieri, Mark Stosberg #
# Shamelessly stolen from Data::FormValidator and CGI::Upload #
#################################################################
use strict;
use Test::More 'no_plan';
use CGI;
#-----------------------------------------------------------------------------
# %ENV setup.
#-----------------------------------------------------------------------------
my %myenv;
BEGIN {
%myenv = (
'SCRIPT_NAME' => '/test.cgi',
'SERVER_NAME' => 'perl.org',
'HTTP_CONNECTION' => 'TE, close',
'REQUEST_METHOD' => 'POST',
'SCRIPT_URI' => 'http://www.perl.org/test.cgi',
'CONTENT_LENGTH' => 3285,
'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' => 'Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)',
'SERVER_PORT' => '80',
'REMOTE_ADDR' => '127.0.0.1',
'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' => '127.0.0.1',
'DOCUMENT_ROOT' => '/home/develop',
'HTTP_HOST' => 'www.perl.org'
);
for my $key (keys %myenv) {
$ENV{$key} = $myenv{$key};
}
}
END {
for my $key (keys %myenv) {
delete $ENV{$key};
}
}
#-----------------------------------------------------------------------------
# Simulate the upload (really, multiple uploads contained in a single stream).
#-----------------------------------------------------------------------------
my $q;
{
local *STDIN;
open STDIN, '<t/upload_post_text.txt'
or die 'missing test file t/upload_post_text.txt';
binmode STDIN;
$q = CGI->new;
}
#-----------------------------------------------------------------------------
# Check that the file names retrieved by CGI are correct.
#-----------------------------------------------------------------------------
is( $q->param('does_not_exist_gif'), 'does_not_exist.gif', 'filename_2' );
is( $q->param('100;100_gif') , '100;100.gif' , 'filename_3' );
is( $q->param('300x300_gif') , '300x300.gif' , 'filename_4' );
{
my $test = "multiple file names are handled right with same-named upload fields";
my @hello_names = $q->param('hello_world');
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
|