Skip to content

Commit

Permalink
use UTF-8 as the default charset
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 17, 2015
1 parent df53aa7 commit 129e317
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,6 +1,8 @@

6.38 2015-12-15
6.38 2015-12-17
- Updated prettify.js to version 8-Dec-2015.
- Improved form generation in Mojo::UserAgent::Transactor to use UTF-8 as the
default charset.

6.37 2015-12-14
- Added protocol and with_protocols methods to Mojo::Transaction::WebSocket.
Expand Down
1 change: 1 addition & 0 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -151,6 +151,7 @@ sub websocket {

sub _form {
my ($self, $tx, $form, %options) = @_;
$options{charset} //= 'UTF-8';

# Check for uploads and force multipart if necessary
my $req = $tx->req;
Expand Down
59 changes: 45 additions & 14 deletions t/mojo/transactor.t
Expand Up @@ -138,53 +138,84 @@ is $tx->req->body, '', 'no content';

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

# UTF-8 form with header and custom content type
$tx
= $t->tx(POST => 'http://example.com/foo' =>
{Accept => '*/*', 'Content-Type' => 'application/mojo-form'} => form =>
{'' => '', nothing => undef} => charset => 'UTF-8');
{'' => '', nothing => undef});
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, '%E2%99%A5=%E2%98%83', 'right content';

# Multipart form
# Form (shift_jis)
$tx
= $t->tx(
POST => 'http://example.com/foo' => form => {'やった' => 'やった'} =>
charset => 'shift_jis');
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, '%82%E2%82%C1%82%BD=%82%E2%82%C1%82%BD', 'right content';
is $tx->req->default_charset('shift_jis')->param('やった'), 'やった',
'right value';

# UTF-8 multipart form
$tx
= $t->tx(POST => 'http://example.com/foo' =>
{'Content-Type' => 'multipart/form-data'} => form =>
{'' => '', nothing => undef});
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, 'multipart/form-data',
'right "Content-Type" value';
like $tx->req->content->parts->[0]->headers->content_disposition,
qr/"@{[encode 'UTF-8', '♥']}"/, 'right "Content-Disposition" value';
is $tx->req->content->parts->[0]->asset->slurp, encode('UTF-8', ''),
'right part';
ok !$tx->req->content->parts->[0]->asset->is_file, 'stored in memory';
ok !$tx->req->content->parts->[0]->asset->auto_upgrade, 'no upgrade';
is $tx->req->content->parts->[1], undef, 'no more parts';
is $tx->req->param(''), '', 'right value';

# Multipart form (shift_jis)
$tx
= $t->tx(POST => 'http://example.com/foo' =>
{'Content-Type' => 'multipart/form-data'} => form =>
{test => 123, nothing => undef});
{'やった' => 'やった'} => charset => 'shift_jis');
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, '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/"@{[encode 'shift_jis', 'やった']}"/,
'right "Content-Disposition" value';
is $tx->req->content->parts->[0]->asset->slurp, 123, 'right part';
is $tx->req->content->parts->[0]->asset->slurp,
encode('shift_jis', 'やった'), 'right part';
ok !$tx->req->content->parts->[0]->asset->is_file, 'stored in memory';
ok !$tx->req->content->parts->[0]->asset->auto_upgrade, 'no upgrade';
is $tx->req->content->parts->[1], undef, 'no more parts';
is $tx->req->default_charset('shift_jis')->param('やった'), 'やった',
'right value';

# Multipart form with multiple values
$tx
Expand All @@ -209,7 +240,7 @@ like $tx->req->content->parts->[3]->headers->content_disposition, qr/"b"/,
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->every_param('a'), [1, 2, 3], 'right values';
is_deeply [$tx->req->param('b')], [4], 'right values';
is $tx->req->param('b'), 4, 'right value';

# Multipart form with real file and custom header
$tx = $t->tx(POST => 'http://example.com/foo' => form =>
Expand Down Expand Up @@ -288,7 +319,7 @@ $tx = $t->tx(
file => Mojo::Asset::Memory->new->add_chunk('snowman'),
filename => '"☃".jpg'
}
} => charset => 'UTF-8'
}
);
is $tx->req->url->to_abs, 'http://example.com/foo', 'right URL';
is $tx->req->method, 'POST', 'right method';
Expand Down
6 changes: 3 additions & 3 deletions t/mojolicious/lite_app.t
Expand Up @@ -852,15 +852,15 @@ $t->get_ok('/url_for_foxy')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')->content_is('/firefox/%23test');

# UTF-8 form
$t->post_ok('/utf8' => form => {name => 'табак'} => charset => 'UTF-8')
->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
$t->post_ok('/utf8' => form => {name => 'табак'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('Content-Length' => 22)
->content_type_is('text/html;charset=UTF-8')
->content_is("табак ангел\n");

# UTF-8 "multipart/form-data" form
$t->post_ok('/utf8' => {'Content-Type' => 'multipart/form-data'} => form =>
{name => 'табак'} => charset => 'UTF-8')->status_is(200)
{name => 'табак'})->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('Content-Length' => 22)
->content_type_is('text/html;charset=UTF-8')
Expand Down

0 comments on commit 129e317

Please sign in to comment.