| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | use lib '.'; |
|---|
| 4 | use Test::Base; |
|---|
| 5 | eval q{ use Data::Visitor::Encode }; |
|---|
| 6 | plan skip_all => "Data::Visitor::Encode is not installed" if $@; |
|---|
| 7 | eval q{ use HTTP::Request }; |
|---|
| 8 | plan skip_all => "HTTP::Request is not installed" if $@; |
|---|
| 9 | eval q{ use HTTP::Engine }; |
|---|
| 10 | plan skip_all => "HTTP::Engine is not installed: $@" if $@; |
|---|
| 11 | |
|---|
| 12 | eval q{ use HTTP::Engine::Middleware }; |
|---|
| 13 | |
|---|
| 14 | plan tests => 4 * blocks; |
|---|
| 15 | |
|---|
| 16 | use Encode; |
|---|
| 17 | use utf8; |
|---|
| 18 | use URI; |
|---|
| 19 | |
|---|
| 20 | filters { params => [qw/eval/], }; |
|---|
| 21 | |
|---|
| 22 | run { |
|---|
| 23 | my $block = shift; |
|---|
| 24 | |
|---|
| 25 | my $mw = HTTP::Engine::Middleware->new; |
|---|
| 26 | $mw->install( 'HTTP::Engine::Middleware::Unicode', ); |
|---|
| 27 | |
|---|
| 28 | my $request = HTTP::Request->new( |
|---|
| 29 | GET => $block->uri, |
|---|
| 30 | [ 'Content-Type' => $block->content_type ] |
|---|
| 31 | ); |
|---|
| 32 | |
|---|
| 33 | my $do_test = sub { |
|---|
| 34 | my $req = shift; |
|---|
| 35 | ok utf8::is_utf8( $req->params->{'nite'} ) , 'utf8'; |
|---|
| 36 | is_deeply $req->params, $block->params, $block->name; |
|---|
| 37 | HTTP::Engine::Response->new( body => "日本" ); |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | my $response = HTTP::Engine->new( |
|---|
| 41 | interface => { |
|---|
| 42 | module => 'Test', |
|---|
| 43 | request_handler => $mw->handler($do_test), |
|---|
| 44 | }, |
|---|
| 45 | )->run($request); |
|---|
| 46 | |
|---|
| 47 | my $content = $response->content; |
|---|
| 48 | |
|---|
| 49 | ok !utf8::is_utf8( $content ), 'not utf8'; |
|---|
| 50 | utf8::decode($content); |
|---|
| 51 | ok utf8::is_utf8($content), 'now its utf8'; |
|---|
| 52 | |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | __END__ |
|---|
| 56 | |
|---|
| 57 | === ascii |
|---|
| 58 | --- uri: http://localhost/?nite=nipotan |
|---|
| 59 | --- content_type: text/plain;charset=ascii |
|---|
| 60 | --- params : {nite => 'nipotan'} |
|---|
| 61 | |
|---|
| 62 | === utf-8 |
|---|
| 63 | --- uri: http://localhost/?nite=%E3%81%97%E3%83%BC%E3%81%88%E3%81%99%E3%81%88%E3%81%99 |
|---|
| 64 | --- content_type : text/plain; charset=utf-8 |
|---|
| 65 | --- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } |
|---|
| 66 | |
|---|