root/lang/perl/HTTP-Engine-Middleware/trunk/t/200_middlewares/unicode.t @ 29640

Revision 29640, 1.6 kB (checked in by yappo, 4 years ago)

add module preload check

Line 
1use strict;
2use warnings;
3use lib '.';
4use Test::Base;
5eval q{ use Data::Visitor::Encode };
6plan skip_all => "Data::Visitor::Encode is not installed" if $@;
7eval q{ use HTTP::Request };
8plan skip_all => "HTTP::Request is not installed" if $@;
9eval q{ use HTTP::Engine };
10plan skip_all => "HTTP::Engine is not installed: $@" if $@;
11
12eval q{ use HTTP::Engine::Middleware };
13
14plan tests => 4 * blocks;
15
16use Encode;
17use utf8;
18use URI;
19
20filters { params => [qw/eval/], };
21
22run {
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
Note: See TracBrowser for help on using the browser.