Skip to content

Commit

Permalink
fix a few form generation bugs in Mojo::UserAgent::Transactor (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 12, 2015
1 parent 1172b9b commit 84a856c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

6.37 2015-12-10
6.37 2015-12-12
- Fixed a few reference encoding bugs in Mojo::JSON.
- Fixed a few form generation bugs in Mojo::UserAgent::Transactor.

6.36 2015-12-08
- Improved Mojo::JSON performance slightly. (haarg)
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -170,12 +170,12 @@ sub _form {
}

# Query parameters or urlencoded
my $p = Mojo::Parameters->new(map { $_ => $form->{$_} } sort keys %$form);
$p->charset($options{charset}) if defined $options{charset};
my $method = uc $req->method;
if ($method eq 'GET' || $method eq 'HEAD') { $req->url->query($p) }
my @form = map { $_ => $form->{$_} } sort keys %$form;
if ($method eq 'GET' || $method eq 'HEAD') { $req->url->query->merge(@form) }
else {
$req->body($p->to_string);
$req->body(
Mojo::Parameters->new(@form)->charset($options{charset})->to_string);
_type($headers, 'application/x-www-form-urlencoded');
}
return $tx;
Expand Down
24 changes: 18 additions & 6 deletions t/mojo/transactor.t
Expand Up @@ -130,33 +130,45 @@ is $tx->req->body, 'a=1&a=2&a=3&b=4', 'right content';

# Existing query string (lowercase HEAD)
$tx = $t->tx(head => 'http://example.com?foo=bar' => form => {baz => [1, 2]});
is $tx->req->url->to_abs, 'http://example.com?baz=1&baz=2', 'right URL';
is $tx->req->url->to_abs, 'http://example.com?foo=bar&baz=1&baz=2', 'right URL';
is $tx->req->method, 'head', 'right method';
is $tx->req->headers->content_type, undef, 'no "Content-Type" value';
ok $tx->is_empty, 'transaction is empty';
is $tx->req->body, '', 'no content';

# UTF-8 form
# UTF-8 query
$tx
= $t->tx(POST => 'http://example.com/foo' => form => {test => 12345678912} =>
= $t->tx(
GET => 'http://example.com/foo' => form => {a => '', b => ''} =>
charset => 'UTF-8');
is $tx->req->url->to_abs, 'http://example.com/foo?a=%E2%98%83&b=%E2%99%A5',
'right URL';
is $tx->req->method, 'GET', 'right method';
is $tx->req->headers->content_type, undef, 'no "Content-Type" value';
is $tx->req->body, '', 'no content';

# UTF-8 form
$tx
= $t->tx(
POST => 'http://example.com/foo' => form => {'' => ''} => charset =>
'UTF-8');
is $tx->req->url->to_abs, 'http://example.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=12345678912', 'right content';
is $tx->req->body, '%E2%99%A5=%E2%98%83', 'right content';

# UTF-8 form with header and custom content type
$tx
= $t->tx(POST => 'http://example.com/foo' =>
{Accept => '*/*', 'Content-Type' => 'application/mojo-form'} => form =>
{test => 123, nothing => undef} => charset => 'UTF-8');
{'' => '', nothing => undef} => charset => 'UTF-8');
is $tx->req->url->to_abs, 'http://example.com/foo', 'right URL';
is $tx->req->method, 'POST', 'right method';
is $tx->req->headers->content_type, 'application/mojo-form',
'right "Content-Type" value';
is $tx->req->headers->accept, '*/*', 'right "Accept" value';
is $tx->req->body, 'test=123', 'right content';
is $tx->req->body, '%E2%99%A5=%E2%98%83', 'right content';

# Multipart form
$tx
Expand Down

0 comments on commit 84a856c

Please sign in to comment.