Skip to content

Commit

Permalink
made form generation a little more predictable
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 9, 2013
1 parent 5db7e0f commit 2f33548
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -38,7 +38,7 @@ sub form {
# New transaction
my $tx = $self->tx(POST => $url, @_);

# Check for uploads and force multipart
# Check for uploads and force multipart if necessary
my $multipart;
for my $value (map { ref $_ eq 'ARRAY' ? @$_ : $_ } values %$form) {
++$multipart and last if ref $value eq 'HASH';
Expand All @@ -57,9 +57,9 @@ sub form {
# Urlencoded
else {
$headers->content_type('application/x-www-form-urlencoded');
my $params = Mojo::Parameters->new(%$form);
$params->charset($encoding) if defined $encoding;
$req->body($params->to_string);
my $p = Mojo::Parameters->new(map { $_ => $form->{$_} } sort keys %$form);
$p->charset($encoding) if defined $encoding;
$req->body($p->to_string);
}

return $tx;
Expand Down
20 changes: 12 additions & 8 deletions t/mojo/transactor.t
Expand Up @@ -88,12 +88,12 @@ is $tx->req->headers->content_type, 'application/x-www-form-urlencoded',
is $tx->req->body, 'test=123', 'right content';

# Simple form with multiple values
$tx = $t->form('http://kraih.com/foo' => {test => [1, 2, 3]});
$tx = $t->form('http://kraih.com/foo' => {a => [1, 2, 3], b => 4});
is $tx->req->url->to_abs, 'http://kraih.com/foo', 'right URL';
is $tx->req->method, 'POST', 'right method';
is $tx->req->headers->content_type, 'application/x-www-form-urlencoded',
'right "Content-Type" value';
is $tx->req->body, 'test=1&test=2&test=3', 'right content';
is $tx->req->body, 'a=1&a=2&a=3&b=4', 'right content';

# UTF-8 form
$tx = $t->form('http://kraih.com/foo' => 'UTF-8' => {test => 123});
Expand Down Expand Up @@ -129,24 +129,28 @@ is $tx->req->content->parts->[1], undef, 'no more parts';

# Multipart form with multiple values
$tx = $t->form(
'http://kraih.com/foo' => {test => [1, 2, 3]},
'http://kraih.com/foo' => {a => [1, 2, 3], b => 4},
{'Content-Type' => 'multipart/form-data'}
);
is $tx->req->url->to_abs, 'http://kraih.com/foo', 'right URL';
is $tx->req->method, 'POST', 'right method';
is $tx->req->headers->content_type, 'multipart/form-data',
'right "Content-Type" value';
like $tx->req->content->parts->[0]->headers->content_disposition, qr/"test"/,
like $tx->req->content->parts->[0]->headers->content_disposition, qr/"a"/,
'right "Content-Disposition" value';
is $tx->req->content->parts->[0]->asset->slurp, 1, 'right part';
like $tx->req->content->parts->[1]->headers->content_disposition, qr/"test"/,
like $tx->req->content->parts->[1]->headers->content_disposition, qr/"a"/,
'right "Content-Disposition" value';
is $tx->req->content->parts->[1]->asset->slurp, 2, 'right part';
like $tx->req->content->parts->[2]->headers->content_disposition, qr/"test"/,
like $tx->req->content->parts->[2]->headers->content_disposition, qr/"a"/,
'right "Content-Disposition" value';
is $tx->req->content->parts->[2]->asset->slurp, 3, 'right part';
is $tx->req->content->parts->[3], undef, 'no more parts';
is_deeply [$tx->req->param('test')], [1, 2, 3], 'right values';
like $tx->req->content->parts->[3]->headers->content_disposition, qr/"b"/,
'right "Content-Disposition" value';
is $tx->req->content->parts->[3]->asset->slurp, 4, 'right part';
is $tx->req->content->parts->[4], undef, 'no more parts';
is_deeply [$tx->req->param('a')], [1, 2, 3], 'right values';
is_deeply [$tx->req->param('b')], [4], 'right values';

# Multipart form with real file and custom header
$tx = $t->form('http://kraih.com/foo',
Expand Down

0 comments on commit 2f33548

Please sign in to comment.