Skip to content

Commit

Permalink
better Mojo::UserAgent::Transactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 9, 2013
1 parent ffe21fd commit ae43ebb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
47 changes: 19 additions & 28 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -31,14 +31,11 @@ sub endpoint {
}

sub form {
my ($self, $url) = (shift, shift);

# Form
my $encoding = shift;
my ($self, $url, $encoding) = (shift, shift, shift);
my $form = ref $encoding ? $encoding : shift;
$encoding = undef if ref $encoding;

# Parameters
# Prepare parameters
my $params = Mojo::Parameters->new;
$params->charset($encoding) if defined $encoding;
my $multipart;
Expand All @@ -48,14 +45,14 @@ sub form {
# Array
if (ref $value eq 'ARRAY') {
for my $value (@$value) {
$params->append($name, $value) and next unless ref $value eq 'HASH';
++$multipart and $params->append($name, _asset($value));
$multipart++ if ref $value eq 'HASH';
$params->append($name, $value);
}
}

# Hash
elsif (ref $value eq 'HASH') {
++$multipart and $params->append($name, _asset($value));
++$multipart and $params->append($name, $value);
}

# Single value
Expand Down Expand Up @@ -189,24 +186,6 @@ sub websocket {
return $tx;
}

sub _asset {
my $value = shift;

# File
if (my $file = $value->{file}) {
$value->{file} = Mojo::Asset::File->new(path => $file) if !ref $file;
$value->{filename} ||= basename $value->{file}->path
if $value->{file}->isa('Mojo::Asset::File');
}

# Memory
elsif (defined(my $content = delete $value->{content})) {
$value->{file} = Mojo::Asset::Memory->new->add_chunk($content);
}

return $value;
}

sub _multipart {
my ($self, $encoding, $form) = @_;

Expand All @@ -217,13 +196,25 @@ sub _multipart {
for my $value (ref $values eq 'ARRAY' ? @$values : ($values)) {
push @parts, my $part = Mojo::Content::Single->new;

# File
# Upload
my $filename;
my $headers = $part->headers;
if (ref $value eq 'HASH') {

# File
if (my $file = delete $value->{file}) {
$file = Mojo::Asset::File->new(path => $file) unless ref $file;
$part->asset($file);
$value->{filename} ||= basename $file->path
if $file->isa('Mojo::Asset::File');
}

# Memory
else { $part->asset->add_chunk(delete $value->{content}) }

# Filename and headers
$filename = delete $value->{filename} || $name;
$filename = encode $encoding, $filename if $encoding;
$part->asset(delete $value->{file});
$headers->from_hash($value);
}

Expand Down
5 changes: 5 additions & 0 deletions t/mojo/transactor.t
Expand Up @@ -162,6 +162,7 @@ like $tx->req->content->parts->[0]->headers->content_disposition,
like $tx->req->content->parts->[0]->asset->slurp, qr/mytext/, 'right part';
ok !$tx->req->content->parts->[0]->headers->header('file'), 'no "file" header';
is $tx->req->content->parts->[0]->headers->dnt, 1, 'right "DNT" header';
ok !$tx->req->content->parts->[0]->headers->header('file'), 'no leaked header';
is $tx->req->content->parts->[1], undef, 'no more parts';

# Multipart form with in-memory content
Expand All @@ -172,6 +173,8 @@ is $tx->req->headers->content_type, 'multipart/form-data',
'right "Content-Type" value';
like $tx->req->content->parts->[0]->headers->content_disposition, qr/mytext/,
'right "Content-Disposition" value';
ok !$tx->req->content->parts->[0]->headers->header('content'),
'no leaked header';
is $tx->req->content->parts->[0]->asset->slurp, 'lalala', 'right part';
is $tx->req->content->parts->[1], undef, 'no more parts';

Expand All @@ -185,6 +188,8 @@ is $tx->req->headers->content_type, 'multipart/form-data',
like $tx->req->content->parts->[0]->headers->content_disposition,
qr/foo\.zip/, 'right "Content-Disposition" value';
is $tx->req->content->parts->[0]->asset->slurp, 'whatever', 'right part';
ok !$tx->req->content->parts->[0]->headers->header('filename'),
'no leaked header';
is $tx->req->content->parts->[1], undef, 'no more parts';
is $tx->req->upload('myzip')->filename, 'foo.zip', 'right filename';
is $tx->req->upload('myzip')->size, 8, 'right size';
Expand Down

0 comments on commit ae43ebb

Please sign in to comment.