Commit | Line | Data |
0078014a |
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 | |
11 | my $test_file; |
12 | if($ENV{PERL_CORE}) { |
13 | chdir 't'; |
14 | @INC = '../lib'; |
15 | use File::Spec (); |
16 | $test_file = File::Spec->catfile(qw(.. lib CGI t), "upload_post_text.txt"); |
17 | } else { |
18 | use lib qw(. ./blib/lib ./blib/arch); |
19 | $test_file = "t/upload_post_text.txt"; |
20 | } |
21 | |
22 | use strict; |
23 | |
24 | use Test::More 'no_plan'; |
25 | |
26 | use CGI; |
27 | |
28 | #----------------------------------------------------------------------------- |
29 | # %ENV setup. |
30 | #----------------------------------------------------------------------------- |
31 | |
f8a128a9 |
32 | my %myenv; |
33 | |
34 | BEGIN { |
35 | %myenv = ( |
36 | 'SCRIPT_NAME' => '/test.cgi', |
37 | 'SERVER_NAME' => 'perl.org', |
38 | 'HTTP_CONNECTION' => 'TE, close', |
39 | 'REQUEST_METHOD' => 'POST', |
40 | 'SCRIPT_URI' => 'http://www.perl.org/test.cgi', |
41 | 'CONTENT_LENGTH' => 3285, |
42 | 'SCRIPT_FILENAME' => '/home/usr/test.cgi', |
43 | 'SERVER_SOFTWARE' => 'Apache/1.3.27 (Unix) ', |
44 | 'HTTP_TE' => 'deflate,gzip;q=0.3', |
45 | 'QUERY_STRING' => '', |
46 | 'REMOTE_PORT' => '1855', |
47 | 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)', |
48 | 'SERVER_PORT' => '80', |
49 | 'REMOTE_ADDR' => '127.0.0.1', |
50 | 'CONTENT_TYPE' => 'multipart/form-data; boundary=xYzZY', |
51 | 'SERVER_PROTOCOL' => 'HTTP/1.1', |
52 | 'PATH' => '/usr/local/bin:/usr/bin:/bin', |
53 | 'REQUEST_URI' => '/test.cgi', |
54 | 'GATEWAY_INTERFACE' => 'CGI/1.1', |
55 | 'SCRIPT_URL' => '/test.cgi', |
56 | 'SERVER_ADDR' => '127.0.0.1', |
57 | 'DOCUMENT_ROOT' => '/home/develop', |
58 | 'HTTP_HOST' => 'www.perl.org' |
59 | ); |
60 | |
61 | for my $key (keys %myenv) { |
62 | $ENV{$key} = $myenv{$key}; |
63 | } |
64 | } |
65 | |
66 | END { |
67 | for my $key (keys %myenv) { |
68 | delete $ENV{$key}; |
69 | } |
70 | } |
0078014a |
71 | |
72 | #----------------------------------------------------------------------------- |
73 | # Simulate the upload (really, multiple uploads contained in a single stream). |
74 | #----------------------------------------------------------------------------- |
75 | |
76 | my $q; |
77 | |
78 | { |
79 | local *STDIN; |
80 | open STDIN, "< $test_file" |
81 | or die 'missing test file t/upload_post_text.txt'; |
82 | binmode STDIN; |
83 | $q = CGI->new; |
84 | } |
85 | |
86 | #----------------------------------------------------------------------------- |
87 | # Check that the file names retrieved by CGI are correct. |
88 | #----------------------------------------------------------------------------- |
89 | |
90 | is( $q->param('does_not_exist_gif'), 'does_not_exist.gif', 'filename_2' ); |
91 | is( $q->param('100;100_gif') , '100;100.gif' , 'filename_3' ); |
92 | is( $q->param('300x300_gif') , '300x300.gif' , 'filename_4' ); |
93 | |
94 | { |
95 | my $test = "multiple file names are handled right with same-named upload fields"; |
96 | my @hello_names = $q->param('hello_world'); |
ebb7c588 |
97 | is ($hello_names[0],'goodbye_world.txt',$test. "...first file"); |
98 | is ($hello_names[1],'hello_world.txt',$test. "...second file"); |
0078014a |
99 | } |
100 | |
101 | #----------------------------------------------------------------------------- |
102 | # Now check that the upload method works. |
103 | #----------------------------------------------------------------------------- |
104 | |
105 | ok( defined $q->upload('does_not_exist_gif'), 'upload_basic_2' ); |
106 | ok( defined $q->upload('100;100_gif') , 'upload_basic_3' ); |
107 | ok( defined $q->upload('300x300_gif') , 'upload_basic_4' ); |
108 | |
109 | { |
110 | my $test = "file handles have expected length for multi-valued field. "; |
111 | my ($goodbye_fh,$hello_fh) = $q->upload('hello_world'); |
112 | |
113 | # Go to end of file; |
114 | seek($goodbye_fh,0,2); |
115 | # How long is the file? |
116 | is(tell($goodbye_fh), 15, "$test..first file"); |
117 | |
118 | # Go to end of file; |
119 | seek($hello_fh,0,2); |
120 | # How long is the file? |
121 | is(tell($hello_fh), 13, "$test..second file"); |
122 | |
123 | } |
124 | |
125 | |
126 | |
127 | { |
128 | my $test = "300x300_gif has expected length"; |
129 | my $fh1 = $q->upload('300x300_gif'); |
130 | is(tell($fh1), 0, "First object: filehandle starts with position set at zero"); |
131 | |
132 | # Go to end of file; |
133 | seek($fh1,0,2); |
134 | # How long is the file? |
135 | is(tell($fh1), 1656, $test); |
136 | } |
137 | |
138 | my $q2 = CGI->new; |
139 | |
140 | { |
141 | my $test = "Upload filehandles still work after calling CGI->new a second time"; |
142 | $q->param('new','zoo'); |
143 | |
144 | is($q2->param('new'),undef, |
145 | "Reality Check: params set in one object instance don't appear in another instance"); |
146 | |
147 | my $fh2 = $q2->upload('300x300_gif'); |
148 | is(tell($fh2), 0, "...so the state of a file handle shouldn't be carried to a new object instance, either."); |
149 | # Go to end of file; |
150 | seek($fh2,0,2); |
151 | # How long is the file? |
152 | is(tell($fh2), 1656, $test); |
153 | } |
154 | |
155 | { |
156 | my $test = "multi-valued uploads are reset properly"; |
157 | my ($dont_care, $hello_fh2) = $q2->upload('hello_world'); |
158 | is(tell($hello_fh2), 0, $test); |
159 | } |
160 | |
161 | # vim: nospell |