1 #!/usr/local/bin/perl -w
3 #################################################################
4 # Emanuele Zeppieri, Mark Stosberg #
5 # Shamelessly stolen from Data::FormValidator and CGI::Upload #
6 #################################################################
8 # Due to a bug in older versions of MakeMaker & Test::Harness, we must
9 # ensure the blib's are in @INC, else we might use the core CGI.pm
10 use lib qw(. ./blib/lib ./blib/arch);
14 use Test::More 'no_plan';
18 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
26 'SCRIPT_NAME' => '/test.cgi',
27 'SERVER_NAME' => 'perl.org',
28 'HTTP_CONNECTION' => 'TE, close',
29 'REQUEST_METHOD' => 'POST',
30 'SCRIPT_URI' => 'http://www.perl.org/test.cgi',
31 'CONTENT_LENGTH' => 3285,
32 'SCRIPT_FILENAME' => '/home/usr/test.cgi',
33 'SERVER_SOFTWARE' => 'Apache/1.3.27 (Unix) ',
34 'HTTP_TE' => 'deflate,gzip;q=0.3',
36 'REMOTE_PORT' => '1855',
37 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)',
38 'SERVER_PORT' => '80',
39 'REMOTE_ADDR' => '127.0.0.1',
40 'CONTENT_TYPE' => 'multipart/form-data; boundary=xYzZY',
41 'SERVER_PROTOCOL' => 'HTTP/1.1',
42 'PATH' => '/usr/local/bin:/usr/bin:/bin',
43 'REQUEST_URI' => '/test.cgi',
44 'GATEWAY_INTERFACE' => 'CGI/1.1',
45 'SCRIPT_URL' => '/test.cgi',
46 'SERVER_ADDR' => '127.0.0.1',
47 'DOCUMENT_ROOT' => '/home/develop',
48 'HTTP_HOST' => 'www.perl.org'
51 for my $key (keys %myenv) {
52 $ENV{$key} = $myenv{$key};
57 for my $key (keys %myenv) {
62 #-----------------------------------------------------------------------------
63 # Simulate the upload (really, multiple uploads contained in a single stream).
64 #-----------------------------------------------------------------------------
70 open STDIN, '<t/upload_post_text.txt'
71 or die 'missing test file t/upload_post_text.txt';
76 #-----------------------------------------------------------------------------
77 # Check that the file names retrieved by CGI are correct.
78 #-----------------------------------------------------------------------------
80 is( $q->param('does_not_exist_gif'), 'does_not_exist.gif', 'filename_2' );
81 is( $q->param('100;100_gif') , '100;100.gif' , 'filename_3' );
82 is( $q->param('300x300_gif') , '300x300.gif' , 'filename_4' );
85 my $test = "multiple file names are handled right with same-named upload fields";
86 my @hello_names = $q->param('hello_world');
87 is ($hello_names[0],'goodbye_world.txt',$test. "...first file");
88 is ($hello_names[1],'hello_world.txt',$test. "...second file");
91 #-----------------------------------------------------------------------------
92 # Now check that the upload method works.
93 #-----------------------------------------------------------------------------
95 ok( defined $q->upload('does_not_exist_gif'), 'upload_basic_2' );
96 ok( defined $q->upload('100;100_gif') , 'upload_basic_3' );
97 ok( defined $q->upload('300x300_gif') , 'upload_basic_4' );
100 my $test = "file handles have expected length for multi-valued field. ";
101 my ($goodbye_fh,$hello_fh) = $q->upload('hello_world');
104 seek($goodbye_fh,0,2);
105 # How long is the file?
106 is(tell($goodbye_fh), 15, "$test..first file");
110 # How long is the file?
111 is(tell($hello_fh), 13, "$test..second file");
118 my $test = "300x300_gif has expected length";
119 my $fh1 = $q->upload('300x300_gif');
120 is(tell($fh1), 0, "First object: filehandle starts with position set at zero");
124 # How long is the file?
125 is(tell($fh1), 1656, $test);
131 my $test = "Upload filehandles still work after calling CGI->new a second time";
132 $q->param('new','zoo');
134 is($q2->param('new'),undef,
135 "Reality Check: params set in one object instance don't appear in another instance");
137 my $fh2 = $q2->upload('300x300_gif');
138 is(tell($fh2), 0, "...so the state of a file handle shouldn't be carried to a new object instance, either.");
141 # How long is the file?
142 is(tell($fh2), 1656, $test);
146 my $test = "multi-valued uploads are reset properly";
147 my ($dont_care, $hello_fh2) = $q2->upload('hello_world');
148 is(tell($hello_fh2), 0, $test);