This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move CGI.pm from lib to ext
[perl5.git] / ext / CGI / t / upload.t
1 #!/usr/local/bin/perl -w
2
3 #################################################################
4 #  Emanuele Zeppieri, Mark Stosberg                             #
5 #  Shamelessly stolen from Data::FormValidator and CGI::Upload  #
6 #################################################################
7
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);
11
12 use strict;
13
14 use Test::More 'no_plan';
15
16 use CGI;
17
18 #-----------------------------------------------------------------------------
19 # %ENV setup.
20 #-----------------------------------------------------------------------------
21
22 my %myenv;
23
24 BEGIN {
25     %myenv = (
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',
35         'QUERY_STRING'      => '',
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'
49     );
50
51     for my $key (keys %myenv) {
52         $ENV{$key} = $myenv{$key};
53     }
54 }
55
56 END {
57     for my $key (keys %myenv) {
58         delete $ENV{$key};
59     }
60 }
61
62 #-----------------------------------------------------------------------------
63 # Simulate the upload (really, multiple uploads contained in a single stream).
64 #-----------------------------------------------------------------------------
65
66 my $q;
67
68 {
69     local *STDIN;
70     open STDIN, '<t/upload_post_text.txt'
71         or die 'missing test file t/upload_post_text.txt';
72     binmode STDIN;
73     $q = CGI->new;
74 }
75
76 #-----------------------------------------------------------------------------
77 # Check that the file names retrieved by CGI are correct.
78 #-----------------------------------------------------------------------------
79
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' );
83
84
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");
89 }
90
91 #-----------------------------------------------------------------------------
92 # Now check that the upload method works.
93 #-----------------------------------------------------------------------------
94
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' );
98
99 {
100     my $test = "file handles have expected length for multi-valued field. ";
101     my ($goodbye_fh,$hello_fh) = $q->upload('hello_world');
102
103         # Go to end of file;
104         seek($goodbye_fh,0,2);
105         # How long is the file?
106         is(tell($goodbye_fh), 15, "$test..first file");
107
108         # Go to end of file;
109         seek($hello_fh,0,2);
110         # How long is the file?
111         is(tell($hello_fh), 13, "$test..second file");
112
113 }
114
115
116
117 {
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");
121
122     # Go to end of file;
123     seek($fh1,0,2);
124     # How long is the file?
125     is(tell($fh1), 1656, $test);
126 }
127
128 my $q2 = CGI->new;
129
130 {
131     my $test = "Upload filehandles still work after calling CGI->new a second time";
132     $q->param('new','zoo');
133
134     is($q2->param('new'),undef, 
135         "Reality Check: params set in one object instance don't appear in another instance");
136
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.");
139         # Go to end of file;
140         seek($fh2,0,2);
141         # How long is the file?
142         is(tell($fh2), 1656, $test);
143 }
144
145 {
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);
149 }
150
151 # vim: nospell