Finish up the file upload handling by PseudoRequest. If you

call $pseudoRequest->uploadFiles with a list of filenames, it will populate
the upload method with upload objects, so that Storage->getFileFromFormPost
will have an object with all the right methods.
t/Storage.t tests this incidently.  It occurs to me that we need tests
for our PseudoRequest module, and it needs POD, and it should probably
be rewritten using Class::InsideOut for good form.
This commit is contained in:
Colin Kuskie 2007-11-10 00:47:17 +00:00
parent 4de53b2de9
commit 1cf78f5321
2 changed files with 72 additions and 2 deletions

View file

@ -11,7 +11,6 @@
use FindBin;
use strict;
use lib "$FindBin::Bin/lib";
our $todo;
use WebGUI::Test;
use WebGUI::Session;
@ -55,7 +54,7 @@ my $extensionTests = [
},
];
plan tests => 76 + scalar @{ $extensionTests }; # increment this value for each test you create
plan tests => 77 + scalar @{ $extensionTests }; # increment this value for each test you create
my $session = WebGUI::Test->session;
@ -395,6 +394,12 @@ $session->http->setStatus(200);
$pseudoRequest->upload('files', []);
is($fileStore->addFileFromFormPost('files'), undef, 'addFileFromFormPost returns empty string when asking for a form variable with no files attached');
$pseudoRequest->uploadFiles(
'oneFile',
[ File::Spec->catfile( WebGUI::Test->getTestCollateralPath, qw/WebGUI.pm/) ],
);
is($fileStore->addFileFromFormPost('oneFile'), 'WebGUI.pm', 'Return the name of the uploaded file');
END {
foreach my $stor (
$storage1, $storage2, $storage3, $copiedStorage,

View file

@ -1,5 +1,7 @@
package WebGUI::PseudoRequest;
use strict;
package WebGUI::PseudoRequest::Headers;
sub new {
@ -22,6 +24,55 @@ sub fetch {
return $self->{headers};
}
package WebGUI::PseudoRequest::Upload;
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = {
fh => undef,
size => 0,
filename => '',
};
my $file = shift;
if ($file and -e $file) {
$self->{filename} = $file;
$self->{size} = (stat $file)[7];
open my $fh, '<' . $file or
die "Unable to open $file for reading and creating a filehandle: $!\n";
$self->{fh} = $fh;
}
bless $self, $class;
return $self;
}
sub fh {
my $self = shift;
my $value = shift;
if (defined $value) {
$self->{fh} = $value;
}
return $self->{fh};
}
sub filename {
my $self = shift;
my $value = shift;
if (defined $value) {
$self->{filename} = $value;
}
return $self->{filename};
}
sub size {
my $self = shift;
my $value = shift;
if (defined $value) {
$self->{size} = $value;
}
return $self->{size};
}
package WebGUI::PseudoRequest;
sub new {
@ -156,6 +207,20 @@ sub upload {
return @{ $self->{uploads}->{$formName} };
}
sub uploadFiles {
my $self = shift;
my $formName = shift;
my $filesToUpload = shift;
return unless $formName;
return unless scalar $filesToUpload;
my @uploadObjects = ();
foreach my $file (@{ $filesToUpload }) {
my $upload = WebGUI::PseudoRequest::Upload->new($file);
push @uploadObjects, $upload;
}
$self->upload($formName, \@uploadObjects);
}
sub uri {
my $self = shift;
my $value = shift;