Permalink
Browse files

最新のPlackに追従

  • Loading branch information...
1 parent d5c5744 commit ea0c06441befbdf54f705162d5884d666335fafd @nekokak committed Feb 24, 2010
View
@@ -2,19 +2,18 @@ package Kamui::Plugin::Encode;
use Kamui;
use base 'Kamui::Plugin';
use Encode ();
+use Hash::MultiValue;
sub register_method {
+{
prepare_encoding => sub {
my $c = shift;
- 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->env->{'plack.request.query'} ||= _decode_multivalue( Hash::MultiValue->new($c->req->uri->query_form) );
+ unless ($c->req->env->{'plack.request.body'}) {
+ $c->req->_parse_request_body;
+ $c->req->env->{'plack.request.body'} = _decode_multivalue( $c->req->env->{'plack.request.body'} );
}
- $c->req->parameters($params);
},
finalize_encoding => sub {
my $c = shift;
@@ -25,5 +24,17 @@ sub register_method {
},
};
+sub _decode_multivalue {
+ my $hash = shift;
+
+ my $params = $hash->mixed;
+ 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);
+ }
+ return Hash::MultiValue->from_mixed(%$decoded_params);
+}
+
1;
@@ -3,19 +3,18 @@ use Kamui;
use base 'Kamui::Plugin';
use Encode ();
use Encode::JP::Mobile;
+use Hash::MultiValue;
sub register_method {
+{
prepare_encoding => sub {
my $c = shift;
- 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->env->{'plack.request.query'} ||= _decode_multivalue( Hash::MultiValue->new($c->req->uri->query_form), $c->mobile->encoding );
+ unless ($c->req->env->{'plack.request.body'}) {
+ $c->req->_parse_request_body;
+ $c->req->env->{'plack.request.body'} = _decode_multivalue( $c->req->env->{'plack.request.body'}, $c->mobile->encoding );
}
- $c->req->parameters($decoded_params);
},
finalize_encoding => sub {
@@ -48,5 +47,17 @@ sub register_method {
};
}
+sub _decode_multivalue {
+ my ($hash, $encoding) = @_;
+
+ my $params = $hash->mixed;
+ my $decoded_params = {};
+ while (my($k, $v) = each %$params) {
+ $decoded_params->{Encode::decode($encoding, $k)} = ref $v eq 'ARRAY'
+ ? [ map Encode::decode($encoding, $_), @$v ] : Encode::decode($encoding, $v);
+ }
+ return Hash::MultiValue->from_mixed(%$decoded_params);
+}
+
1;
@@ -22,9 +22,8 @@ sub get_session_id {
my $req = $self->{c}->req;
- if ( my $cookie = $req->cookies->{ $self->{cookie_name} } ) {
- my $sid = $cookie->value;
- return $sid if $sid;
+ if ( my $sid = $req->cookies->{ $self->{cookie_name} } ) {
+ return $sid;
}
return;
View
@@ -81,6 +81,7 @@ sub fillin_fdat {
sub view {
my ($self, $view) = @_;
+ $self->{view} ||= 'Kamui::View::TT';
if ($view) {
$self->{view} = 'Kamui::View::'.$view;
} else {
View
@@ -87,12 +87,14 @@ sub handler {
conf => container('conf'),
app => $self,
);
+ $context->initialize();
+
my $rule = $self->dispatcher->determine($context);
$context->dispatch_rule($rule);
- $context->initialize();
my $response = $self->dispatch($context);
$context->finalize($response);
+
$response->finalize;
};
}
View
@@ -1,36 +1,65 @@
use t::Utils;
-use Test::Declare;
+use utf8;
+use Plack::Test;
+use Test::More;
use Kamui::Web::Context;
use Mock::Web::Handler;
+use HTTP::Request::Common;
-plan tests => blocks;
-
-describe 'plugin tests' => run {
+subtest 'plugin tests / get encode' => sub {
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 => '/',
- };
+ 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 => '/',
+ QUERY_STRING => 'foo=%E6%97%A5%E6%9C%AC%E8%AA%9E&bar=%E6%97%A5%E6%9C%AC%E8%AA%9E&bar=%E6%97%A5%E6%9C%AC%E8%AA%9E',
};
- test 'basic test' => run {
+ my $c = Kamui::Web::Context->new(
+ env => $env,
+ app => 'Mock::Web::Handler',
+ );
+ $c->initialize;
+
+ ok $c->req->param('foo');
+ is $c->req->param('foo'), '日本語';
+ my @bar = $c->req->param('bar');
+ is_deeply \@bar, ['日本語','日本語'];
+
+ done_testing;
+};
+
+subtest 'plugin tests / post encode' => sub {
+ my $app = sub {
+ my $env = shift;;
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});
+
+ is(length $c->req->param('foo'), 3);
+ my $res = $c->res;
+ $res->status(200);
+ $res->finalize;
};
+
+ test_psgi ( $app,
+ sub {
+ my $cb = shift;
+ my $res = $cb->( POST "/", { foo => "日本語" });
+ ok( $res->is_success );
+ }
+ );
+ done_testing;
};
+done_testing;
+

0 comments on commit ea0c064

Please sign in to comment.