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