Commit | Line | Data |
dd4e6fd2 |
1 | #!perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use FindBin; |
42da66a9 |
7 | use lib "$FindBin::Bin/../lib"; |
dd4e6fd2 |
8 | |
ed4ac838 |
9 | use Test::More tests => 101; |
dd4e6fd2 |
10 | use Catalyst::Test 'TestApp'; |
11 | |
12 | use Catalyst::Request; |
5c0ff128 |
13 | use Catalyst::Request::Upload; |
dd4e6fd2 |
14 | use HTTP::Headers; |
15 | use HTTP::Headers::Util 'split_header_words'; |
16 | use HTTP::Request::Common; |
2f381252 |
17 | use Path::Class::Dir; |
dd4e6fd2 |
18 | |
19 | { |
20 | my $creq; |
21 | |
fbcc39ad |
22 | my $request = POST( |
23 | 'http://localhost/dump/request/', |
24 | 'Content-Type' => 'form-data', |
dd4e6fd2 |
25 | 'Content' => [ |
a2e038a1 |
26 | 'live_engine_request_cookies.t' => |
27 | ["$FindBin::Bin/live_engine_request_cookies.t"], |
28 | 'live_engine_request_headers.t' => |
29 | ["$FindBin::Bin/live_engine_request_headers.t"], |
30 | 'live_engine_request_uploads.t' => |
31 | ["$FindBin::Bin/live_engine_request_uploads.t"], |
fbcc39ad |
32 | ] |
dd4e6fd2 |
33 | ); |
34 | |
35 | ok( my $response = request($request), 'Request' ); |
36 | ok( $response->is_success, 'Response Successful 2xx' ); |
fbcc39ad |
37 | is( $response->content_type, 'text/plain', 'Response Content-Type' ); |
38 | like( |
39 | $response->content, |
40 | qr/^bless\( .* 'Catalyst::Request' \)$/s, |
41 | 'Content is a serialized Catalyst::Request' |
42 | ); |
43 | |
dd4e6fd2 |
44 | { |
45 | no strict 'refs'; |
fbcc39ad |
46 | ok( |
47 | eval '$creq = ' . $response->content, |
48 | 'Unserialize Catalyst::Request' |
49 | ); |
dd4e6fd2 |
50 | } |
51 | |
52 | isa_ok( $creq, 'Catalyst::Request' ); |
53 | is( $creq->method, 'POST', 'Catalyst::Request method' ); |
fbcc39ad |
54 | is( $creq->content_type, 'multipart/form-data', |
55 | 'Catalyst::Request Content-Type' ); |
56 | is( $creq->content_length, $request->content_length, |
57 | 'Catalyst::Request Content-Length' ); |
dd4e6fd2 |
58 | |
59 | for my $part ( $request->parts ) { |
60 | |
61 | my $disposition = $part->header('Content-Disposition'); |
62 | my %parameters = @{ ( split_header_words($disposition) )[0] }; |
63 | |
8e061fef |
64 | my $upload = $creq->uploads->{ $parameters{filename} }; |
fbcc39ad |
65 | |
5c0ff128 |
66 | isa_ok( $upload, 'Catalyst::Request::Upload' ); |
dd4e6fd2 |
67 | |
d3fced79 |
68 | is( $upload->type, $part->content_type, 'Upload Content-Type' ); |
69 | is( $upload->size, length( $part->content ), 'Upload Content-Length' ); |
a2e038a1 |
70 | |
c4bed79a |
71 | # make sure upload is accessible via legacy params->{$file} |
8e061fef |
72 | is( $creq->parameters->{ $upload->filename }, |
a2e038a1 |
73 | $upload->filename, 'legacy param method ok' ); |
74 | |
75 | ok( !-e $upload->tempname, 'Upload temp file was deleted' ); |
dd4e6fd2 |
76 | } |
dd4e6fd2 |
77 | } |
1408d0a4 |
78 | |
79 | { |
80 | my $creq; |
81 | |
fbcc39ad |
82 | my $request = POST( |
83 | 'http://localhost/dump/request/', |
1408d0a4 |
84 | 'Content-Type' => 'multipart/form-data', |
85 | 'Content' => [ |
a2e038a1 |
86 | 'testfile' => ["$FindBin::Bin/live_engine_request_cookies.t"], |
87 | 'testfile' => ["$FindBin::Bin/live_engine_request_headers.t"], |
88 | 'testfile' => ["$FindBin::Bin/live_engine_request_uploads.t"], |
fbcc39ad |
89 | ] |
1408d0a4 |
90 | ); |
91 | |
92 | ok( my $response = request($request), 'Request' ); |
93 | ok( $response->is_success, 'Response Successful 2xx' ); |
fbcc39ad |
94 | is( $response->content_type, 'text/plain', 'Response Content-Type' ); |
95 | like( |
96 | $response->content, |
97 | qr/^bless\( .* 'Catalyst::Request' \)$/s, |
98 | 'Content is a serialized Catalyst::Request' |
99 | ); |
100 | |
1408d0a4 |
101 | { |
102 | no strict 'refs'; |
fbcc39ad |
103 | ok( |
104 | eval '$creq = ' . $response->content, |
105 | 'Unserialize Catalyst::Request' |
106 | ); |
1408d0a4 |
107 | } |
108 | |
109 | isa_ok( $creq, 'Catalyst::Request' ); |
110 | is( $creq->method, 'POST', 'Catalyst::Request method' ); |
fbcc39ad |
111 | is( $creq->content_type, 'multipart/form-data', |
112 | 'Catalyst::Request Content-Type' ); |
113 | is( $creq->content_length, $request->content_length, |
114 | 'Catalyst::Request Content-Length' ); |
115 | |
1408d0a4 |
116 | my @parts = $request->parts; |
117 | |
fbcc39ad |
118 | for ( my $i = 0 ; $i < @parts ; $i++ ) { |
119 | |
1408d0a4 |
120 | my $part = $parts[$i]; |
121 | my $disposition = $part->header('Content-Disposition'); |
122 | my %parameters = @{ ( split_header_words($disposition) )[0] }; |
fbcc39ad |
123 | |
8e061fef |
124 | my $upload = $creq->uploads->{ $parameters{name} }->[$i]; |
fbcc39ad |
125 | |
1408d0a4 |
126 | isa_ok( $upload, 'Catalyst::Request::Upload' ); |
127 | is( $upload->type, $part->content_type, 'Upload Content-Type' ); |
128 | is( $upload->filename, $parameters{filename}, 'Upload filename' ); |
129 | is( $upload->size, length( $part->content ), 'Upload Content-Length' ); |
a2e038a1 |
130 | |
131 | ok( !-e $upload->tempname, 'Upload temp file was deleted' ); |
1408d0a4 |
132 | } |
133 | } |
32d4bba8 |
134 | |
135 | { |
136 | my $creq; |
137 | |
fbcc39ad |
138 | my $request = POST( |
139 | 'http://localhost/engine/request/uploads/slurp', |
32d4bba8 |
140 | 'Content-Type' => 'multipart/form-data', |
a2e038a1 |
141 | 'Content' => |
142 | [ 'slurp' => ["$FindBin::Bin/live_engine_request_uploads.t"], ] |
32d4bba8 |
143 | ); |
144 | |
145 | ok( my $response = request($request), 'Request' ); |
146 | ok( $response->is_success, 'Response Successful 2xx' ); |
147 | is( $response->content_type, 'text/plain', 'Response Content-Type' ); |
fbcc39ad |
148 | is( $response->content, ( $request->parts )[0]->content, 'Content' ); |
ed4ac838 |
149 | |
150 | # XXX: no way to test that temporary file for this test was deleted |
32d4bba8 |
151 | } |
fdb3773e |
152 | |
153 | { |
154 | my $request = POST( |
155 | 'http://localhost/dump/request', |
156 | 'Content-Type' => 'multipart/form-data', |
157 | 'Content' => |
42da66a9 |
158 | [ 'file' => ["$FindBin::Bin/../catalyst_130pix.gif"], ] |
fdb3773e |
159 | ); |
160 | |
c4c4dd19 |
161 | # LWP will auto-correct Content-Length when using a remote server |
162 | SKIP: |
163 | { |
164 | if ( $ENV{CATALYST_SERVER} ) { |
165 | skip 'Using remote server', 2; |
166 | } |
fdb3773e |
167 | |
c4c4dd19 |
168 | # Sending wrong Content-Length here and see if subequent requests fail |
169 | $request->header('Content-Length' => $request->header('Content-Length') + 1); |
170 | |
171 | ok( my $response = request($request), 'Request' ); |
172 | ok( !$response->is_success, 'Response Error' ); |
173 | } |
fdb3773e |
174 | |
175 | $request = POST( |
176 | 'http://localhost/dump/request', |
177 | 'Content-Type' => 'multipart/form-data', |
178 | 'Content' => |
42da66a9 |
179 | [ 'file1' => ["$FindBin::Bin/../catalyst_130pix.gif"], |
180 | 'file2' => ["$FindBin::Bin/../catalyst_130pix.gif"], ] |
fdb3773e |
181 | ); |
182 | |
c4c4dd19 |
183 | ok( my $response = request($request), 'Request' ); |
fdb3773e |
184 | ok( $response->is_success, 'Response Successful 2xx' ); |
185 | is( $response->content_type, 'text/plain', 'Response Content-Type' ); |
186 | like( $response->content, qr/file1 => bless/, 'Upload with name file1'); |
187 | like( $response->content, qr/file2 => bless/, 'Upload with name file2'); |
ed4ac838 |
188 | |
189 | my $creq; |
190 | { |
191 | no strict 'refs'; |
192 | ok( |
193 | eval '$creq = ' . $response->content, |
194 | 'Unserialize Catalyst::Request' |
195 | ); |
196 | } |
197 | |
198 | for my $file ( $creq->upload ) { |
199 | my $upload = $creq->upload($file); |
200 | ok( !-e $upload->tempname, 'Upload temp file was deleted' ); |
201 | } |
fdb3773e |
202 | } |
a7e05d9d |
203 | |
204 | { |
205 | my $creq; |
206 | |
207 | my $request = POST( |
208 | 'http://localhost/dump/request/', |
209 | 'Content-Type' => 'form-data', |
210 | 'Content' => [ |
211 | 'testfile' => 'textfield value', |
42da66a9 |
212 | 'testfile' => ["$FindBin::Bin/../catalyst_130pix.gif"], |
a7e05d9d |
213 | ] |
214 | ); |
215 | |
216 | ok( my $response = request($request), 'Request' ); |
217 | ok( $response->is_success, 'Response Successful 2xx' ); |
218 | is( $response->content_type, 'text/plain', 'Response Content-Type' ); |
219 | like( |
220 | $response->content, |
221 | qr/^bless\( .* 'Catalyst::Request' \)$/s, |
222 | 'Content is a serialized Catalyst::Request' |
223 | ); |
224 | |
225 | { |
226 | no strict 'refs'; |
227 | ok( |
228 | eval '$creq = ' . $response->content, |
229 | 'Unserialize Catalyst::Request' |
230 | ); |
231 | } |
232 | |
233 | isa_ok( $creq, 'Catalyst::Request' ); |
234 | is( $creq->method, 'POST', 'Catalyst::Request method' ); |
235 | is( $creq->content_type, 'multipart/form-data', |
236 | 'Catalyst::Request Content-Type' ); |
237 | is( $creq->content_length, $request->content_length, |
238 | 'Catalyst::Request Content-Length' ); |
239 | |
8e061fef |
240 | my $param = $creq->parameters->{testfile}; |
a7e05d9d |
241 | |
242 | ok( @$param == 2, '2 values' ); |
243 | is( $param->[0], 'textfield value', 'correct value' ); |
244 | like( $param->[1], qr/\Qcatalyst_130pix.gif/, 'filename' ); |
245 | |
246 | for my $part ( $request->parts ) { |
247 | |
248 | my $disposition = $part->header('Content-Disposition'); |
249 | my %parameters = @{ ( split_header_words($disposition) )[0] }; |
250 | |
251 | next unless exists $parameters{filename}; |
252 | |
8e061fef |
253 | my $upload = $creq->uploads->{ $parameters{name} }; |
a7e05d9d |
254 | |
255 | isa_ok( $upload, 'Catalyst::Request::Upload' ); |
256 | |
257 | is( $upload->type, $part->content_type, 'Upload Content-Type' ); |
258 | is( $upload->size, length( $part->content ), 'Upload Content-Length' ); |
ed4ac838 |
259 | is( $upload->filename, 'catalyst_130pix.gif', 'Upload Filename' ); |
260 | ok( !-e $upload->tempname, 'Upload temp file was deleted' ); |
a7e05d9d |
261 | } |
262 | } |
2f381252 |
263 | |
c4b09639 |
264 | # Test PUT request with application/octet-stream file gets deleted |
265 | |
266 | { |
267 | my $body; |
268 | |
269 | my $request = PUT( |
270 | 'http://localhost/dump/body/', |
271 | 'Content-Type' => 'application/octet-stream', |
272 | 'Content' => 'foobarbaz', |
c4b09639 |
273 | ); |
274 | |
275 | ok( my $response = request($request), 'Request' ); |
276 | ok( $response->is_success, 'Response Successful 2xx' ); |
277 | is( $response->content_type, 'text/plain', 'Response Content-Type' ); |
278 | like( |
279 | $response->content, |
280 | qr/bless\( .* 'HTTP::Body::OctetStream' \)/s, |
281 | 'Content is a serialized HTTP::Body::OctetStream' |
282 | ); |
283 | |
284 | { |
285 | no strict 'refs'; |
286 | ok( |
287 | eval '$body = ' . substr( $response->content, 8 ), # FIXME - substr not needed in other test cases? |
288 | 'Unserialize HTTP::Body::OctetStream' |
289 | ) or warn $@; |
290 | } |
291 | |
292 | isa_ok( $body, 'HTTP::Body::OctetStream' ); |
293 | isa_ok($body->body, 'File::Temp'); |
294 | |
295 | ok( !-e $body->body->filename, 'Upload temp file was deleted' ); |
296 | } |
297 | |
2f381252 |
298 | # test uploadtmp config var |
299 | |
300 | { |
301 | my $creq; |
302 | |
303 | my $dir = "$FindBin::Bin/"; |
304 | local TestApp->config->{ uploadtmp } = $dir; |
305 | $dir = Path::Class::Dir->new( $dir ); |
306 | |
307 | my $request = POST( |
308 | 'http://localhost/dump/request/', |
309 | 'Content-Type' => 'multipart/form-data', |
310 | 'Content' => [ |
311 | 'testfile' => ["$FindBin::Bin/live_engine_request_uploads.t"], |
312 | ] |
313 | ); |
314 | |
315 | ok( my $response = request($request), 'Request' ); |
316 | ok( $response->is_success, 'Response Successful 2xx' ); |
317 | is( $response->content_type, 'text/plain', 'Response Content-Type' ); |
318 | like( |
319 | $response->content, |
320 | qr/^bless\( .* 'Catalyst::Request' \)$/s, |
321 | 'Content is a serialized Catalyst::Request' |
322 | ); |
323 | |
324 | { |
325 | no strict 'refs'; |
326 | ok( |
327 | eval '$creq = ' . $response->content, |
328 | 'Unserialize Catalyst::Request' |
329 | ); |
330 | } |
331 | |
332 | isa_ok( $creq, 'Catalyst::Request' ); |
333 | is( $creq->method, 'POST', 'Catalyst::Request method' ); |
334 | is( $creq->content_type, 'multipart/form-data', |
335 | 'Catalyst::Request Content-Type' ); |
336 | is( $creq->content_length, $request->content_length, |
337 | 'Catalyst::Request Content-Length' ); |
338 | |
339 | for my $part ( $request->parts ) { |
340 | |
341 | my $disposition = $part->header('Content-Disposition'); |
342 | my %parameters = @{ ( split_header_words($disposition) )[0] }; |
343 | |
344 | next unless exists $parameters{filename}; |
345 | |
346 | my $upload = $creq->{uploads}->{ $parameters{name} }; |
347 | |
348 | isa_ok( $upload, 'Catalyst::Request::Upload' ); |
349 | |
350 | is( $upload->type, $part->content_type, 'Upload Content-Type' ); |
351 | is( $upload->size, length( $part->content ), 'Upload Content-Length' ); |
352 | |
353 | like( $upload->tempname, qr{\Q$dir\E}, 'uploadtmp' ); |
ed4ac838 |
354 | |
355 | ok( !-e $upload->tempname, 'Upload temp file was deleted' ); |
2f381252 |
356 | } |
357 | } |
358 | |