From: Tomas Doran Date: Mon, 26 Oct 2009 23:52:23 +0000 (+0000) Subject: Tests which I believe show that JSON encoding is handled correctly. I.E. You pass... X-Git-Tag: 0.79~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Action-REST.git;a=commitdiff_plain;h=c6c4ff28f913e990540021611f0b4bd3b5eda494;hp=72b4a0bb753b350c5ccb9a8b037497adf3efe239 Tests which I believe show that JSON encoding is handled correctly. I.E. You pass in a character string (in perl's internal representation), and it's correctly encoded as utf8 in the JSON. It's then decoded when it arrives in the test controller and needs to be re-encoded to push into the output. Then for comparison in the tests we need to encode the character string back into bytes so that the test is testing like for like.. --- diff --git a/t/json.t b/t/json.t index b601c64..673c0a9 100644 --- a/t/json.t +++ b/t/json.t @@ -5,6 +5,7 @@ use FindBin; use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib"); use Test::Rest; +use utf8; eval 'use JSON 2.12'; plan skip_all => 'Install JSON 2.12 or later to run this test' if ($@); @@ -13,7 +14,7 @@ plan tests => 9; use_ok 'Catalyst::Test', 'Test::Serialize'; -my $json = JSON->new; +my $json = JSON->new->utf8; # The text/x-json should throw a warning for ('text/x-json', 'application/json') { my $t = Test::Rest->new('content_type' => $_); @@ -26,10 +27,13 @@ for ('text/x-json', 'application/json') { my $post_data = { 'sushi' => 'is good for monkey', + 'chicken' => ' 佐藤 純', }; my $mres_post = request($t->post(url => '/monkey_put', data => $json->encode($post_data))); ok( $mres_post->is_success, "POST to the monkey succeeded"); - is_deeply($mres_post->content, "is good for monkey", "POST data matches"); + my $exp = "is good for monkey 佐藤 純"; + utf8::encode($exp); + is_deeply($mres_post->content, $exp, "POST data matches"); } 1; diff --git a/t/lib/Test/Serialize/Controller/REST.pm b/t/lib/Test/Serialize/Controller/REST.pm index 733508a..dad5483 100644 --- a/t/lib/Test/Serialize/Controller/REST.pm +++ b/t/lib/Test/Serialize/Controller/REST.pm @@ -32,7 +32,9 @@ __PACKAGE__->config( sub monkey_put : Local : ActionClass('Deserialize') { my ( $self, $c ) = @_; if ( ref($c->req->data) eq "HASH" ) { - $c->res->output( $c->req->data->{'sushi'} ); + my $out = $c->req->data->{'sushi'} . $c->req->data->{'chicken'}||''; + utf8::encode($out); + $c->res->output( $out ); } else { $c->res->output(1); }