Browse files

Data::Visitorに依存しないようにした

  • Loading branch information...
1 parent a3b17be commit b1fb20ad705cae1a33f33d46cca49d38bee4dbe8 @nekokak committed Jan 6, 2010
Showing with 54 additions and 10 deletions.
  1. +9 −5 lib/Kamui/Plugin/Encode.pm
  2. +9 −5 lib/Kamui/Plugin/Mobile/Encode.pm
  3. +36 −0 t/030_plugin/encode.t
View
14 lib/Kamui/Plugin/Encode.pm
@@ -1,16 +1,20 @@
package Kamui::Plugin::Encode;
use Kamui;
use base 'Kamui::Plugin';
-use Encode;
-use Data::Visitor::Encode;
+use Encode ();
sub register_method {
+{
prepare_encoding => sub {
my $c = shift;
- $c->req->parameters(
- Data::Visitor::Encode->decode( 'utf8', $c->req->parameters )
- );
+
+ my $params = $c->req->parameters;
+ my $decoded_params = {};
+ while (my($k, $v) = each %$params) {
+ $decoded_params->{Encode::decode_utf8($k)} = ref $v eq 'ARRAY'
+ ? [ map Encode::decode_utf8($_), @$v ] : Encode::decode_utf8($v);
+ }
+ $c->req->parameters($params);
},
finalize_encoding => sub {
my $c = shift;
View
14 lib/Kamui/Plugin/Mobile/Encode.pm
@@ -1,17 +1,21 @@
package Kamui::Plugin::Mobile::Encode;
use Kamui;
use base 'Kamui::Plugin';
-use Data::Visitor::Encode;
use Encode ();
-use Encode::JP::Mobile ();
+use Encode::JP::Mobile;
sub register_method {
+{
prepare_encoding => sub {
my $c = shift;
- $c->req->parameters(
- Data::Visitor::Encode->decode( $c->mobile->encoding, $c->req->parameters )
- );
+
+ my $params = $c->req->parameters;
+ my $decoded_params = {};
+ while (my($k, $v) = each %$params) {
+ $decoded_params->{Encode::decode($c->mobile->encoding, $k)} = ref $v eq 'ARRAY'
+ ? [ map Encode::decode($c->mobile->encoding, $_), @$v ] : Encode::decode($c->mobile->encoding, $v);
+ }
+ $c->req->parameters($decoded_params);
},
finalize_encoding => sub {
View
36 t/030_plugin/encode.t
@@ -0,0 +1,36 @@
+use t::Utils;
+use Test::Declare;
+use Kamui::Web::Context;
+use Mock::Web::Handler;
+
+plan tests => blocks;
+
+describe 'plugin tests' => run {
+
+ my $env;
+ init {
+ Kamui::Web::Context->load_plugins([qw/Encode/]);
+
+ open my $in, '<', \do { my $d };
+ $env = +{
+ 'psgi.version' => [ 1, 0 ],
+ 'psgi.input' => $in,
+ 'psgi.errors' => *STDERR,
+ 'psgi.url_scheme' => 'http',
+ REQUEST_METHOD => 'GET',
+ SCRIPT_NAME => '/',
+ };
+ };
+
+ test 'basic test' => run {
+ my $c = Kamui::Web::Context->new(
+ env => $env,
+ app => 'Mock::Web::Handler',
+ );
+ $c->req->body_parameters(var => 'あいうえお');
+ $c->initialize;
+ my $params = $c->req->parameters;
+ ok Encode::is_utf8($params->{var});
+ };
+};
+

0 comments on commit b1fb20a

Please sign in to comment.