Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved form content generator for GET and HEAD requests
  • Loading branch information
jberger committed Apr 22, 2013
1 parent ecb0904 commit d4dc79a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -189,7 +189,9 @@ sub _form {
$headers->content_type('application/x-www-form-urlencoded');
my $p = Mojo::Parameters->new(map { $_ => $form->{$_} } sort keys %$form);
$p->charset($options{charset}) if defined $options{charset};
$req->body($p->to_string);
my $method = uc $req->method;
if ($method eq 'GET' || $method eq 'HEAD') { $req->url->query->merge($p) }
else { $req->body($p->to_string) }
return $tx;
}

Expand Down Expand Up @@ -405,7 +407,8 @@ requests, with support for content generators.
While the "multipart/form-data" content type will be automatically used
instead of "application/x-www-form-urlencoded" when necessary, you can also
enforce it by setting the header manually.
enforce it by setting the header manually. GET and HEAD transactions merge
form paramters into the url query parameters.
=head2 upgrade
Expand Down
10 changes: 9 additions & 1 deletion t/mojo/transactor.t
Expand Up @@ -100,14 +100,22 @@ is $tx->req->headers->content_type, 'application/something',
'right "Content-Type" value';
is_deeply $tx->req->json, [1, 2], 'right content';

# Simple form
# Simple form (POST)
$tx = $t->tx(POST => 'http://kraih.com/foo' => form => {test => 123});
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=123', 'right content';

# Simple form (GET)
$tx = $t->tx(GET => 'http://kraih.com/foo' => form => {test => 123});
is $tx->req->url->to_abs, 'http://kraih.com/foo?test=123', 'right URL';
is $tx->req->method, 'GET', 'right method';
is $tx->req->headers->content_type, 'application/x-www-form-urlencoded',
'right "Content-Type" value';
is $tx->req->body, '', 'right content';

# Simple form with multiple values
$tx
= $t->tx(POST => 'http://kraih.com/foo' => form => {a => [1, 2, 3], b => 4});
Expand Down

0 comments on commit d4dc79a

Please sign in to comment.