From: Jonathan Rockway Date: Fri, 17 Nov 2006 16:46:56 +0000 (+0000) Subject: applying upload fix patch from Carl Franks (see message to catalyst-dev@) X-Git-Tag: 5.7099_04~268 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=a7e05d9d37504788045c0ea35b64f3cfe1b056f8 applying upload fix patch from Carl Franks (see message to catalyst-dev@) --- diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 3a73c04..d0c97b3 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -494,8 +494,20 @@ sub prepare_uploads { # support access to the filename as a normal param my @filenames = map { $_->{filename} } @uploads; - $c->request->parameters->{$name} = - @filenames > 1 ? \@filenames : $filenames[0]; + # append, if there's already params with this name + if (exists $c->request->parameters->{$name}) { + if (ref $c->request->parameters->{$name} eq 'ARRAY') { + push @{ $c->request->parameters->{$name} }, @filenames; + } + else { + $c->request->parameters->{$name} = + [ $c->request->parameters->{$name}, @filenames ]; + } + } + else { + $c->request->parameters->{$name} = + @filenames > 1 ? \@filenames : $filenames[0]; + } } } diff --git a/t/live_engine_request_uploads.t b/t/live_engine_request_uploads.t index d0eeb7d..d2e95ab 100644 --- a/t/live_engine_request_uploads.t +++ b/t/live_engine_request_uploads.t @@ -6,7 +6,7 @@ use warnings; use FindBin; use lib "$FindBin::Bin/lib"; -use Test::More tests => 59; +use Test::More tests => 75; use Catalyst::Test 'TestApp'; use Catalyst::Request; @@ -183,3 +183,62 @@ use HTTP::Request::Common; like( $response->content, qr/file1 => bless/, 'Upload with name file1'); like( $response->content, qr/file2 => bless/, 'Upload with name file2'); } + +{ + my $creq; + + my $request = POST( + 'http://localhost/dump/request/', + 'Content-Type' => 'form-data', + 'Content' => [ + 'testfile' => 'textfield value', + 'testfile' => ["$FindBin::Bin/catalyst_130pix.gif"], + ] + ); + + ok( my $response = request($request), 'Request' ); + ok( $response->is_success, 'Response Successful 2xx' ); + is( $response->content_type, 'text/plain', 'Response Content-Type' ); + like( + $response->content, + qr/^bless\( .* 'Catalyst::Request' \)$/s, + 'Content is a serialized Catalyst::Request' + ); + + { + no strict 'refs'; + ok( + eval '$creq = ' . $response->content, + 'Unserialize Catalyst::Request' + ); + } + + isa_ok( $creq, 'Catalyst::Request' ); + is( $creq->method, 'POST', 'Catalyst::Request method' ); + is( $creq->content_type, 'multipart/form-data', + 'Catalyst::Request Content-Type' ); + is( $creq->content_length, $request->content_length, + 'Catalyst::Request Content-Length' ); + + my $param = $creq->{parameters}->{testfile}; + + ok( @$param == 2, '2 values' ); + is( $param->[0], 'textfield value', 'correct value' ); + like( $param->[1], qr/\Qcatalyst_130pix.gif/, 'filename' ); + + for my $part ( $request->parts ) { + + my $disposition = $part->header('Content-Disposition'); + my %parameters = @{ ( split_header_words($disposition) )[0] }; + + next unless exists $parameters{filename}; + + my $upload = $creq->{uploads}->{ $parameters{name} }; + + isa_ok( $upload, 'Catalyst::Request::Upload' ); + + is( $upload->type, $part->content_type, 'Upload Content-Type' ); + is( $upload->size, length( $part->content ), 'Upload Content-Length' ); + is( $upload->filename, 'catalyst_130pix.gif' ); + } +}