make sure we do not require unfount dependencies
[catagits/Catalyst-Runtime.git] / t / author / unicode_plugin_nested_params.t
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use utf8;
7
8 # setup library path
9 use FindBin qw($Bin);
10 use lib "$Bin/../lib";
11
12 BEGIN { eval { require Catalyst::Plugin::Params::Nested; 1; } ||
13     plan skip_all => 'Need Catalyst::Plugin::Params::Nested' }
14
15 use Catalyst::Test 'TestApp2';
16 use Encode;
17 use HTTP::Request::Common;
18 use URI::Escape qw/uri_escape_utf8/;
19 use HTTP::Status 'is_server_error';
20
21 my $encode_str = "\x{e3}\x{81}\x{82}"; # e38182 is japanese 'あ'
22 my $decode_str = Encode::decode('utf-8' => $encode_str);
23 my $escape_str = uri_escape_utf8($decode_str);
24
25 BEGIN {
26     eval 'require Catalyst::Plugin::Params::Nested';
27     plan skip_all => 'Catalyst::Plugin::Params::Nested is required' if $@;
28 }
29
30 {
31     my ($res, $c) = ctx_request("/?foo.1=bar&foo.2=$escape_str");
32     is( $c->res->output, '<h1>It works</h1>', 'Content displayed' );
33     
34     my $got = $c->request->parameters;
35     my $expected = {
36         'foo.1' => 'bar',
37         'foo.2' => $decode_str,
38         'foo'   => [undef, 'bar', $decode_str],
39     };
40     
41     is( $got->{foo}->[0], undef, '{foo}->[0] is undef' );
42     is( $got->{foo}->[1], 'bar', '{foo}->[1] is bar' );
43     ok( utf8::is_utf8( $got->{'foo.2'}       ), '{foo.2} is utf8' );
44     ok( utf8::is_utf8( $got->{foo}->[2]      ), '{foo}->[2] is utf8' );
45     is_deeply($got, $expected, 'nested params' );
46 }
47
48 {
49     my ($res, $c) = ctx_request("/?foo.1=bar&foo.2=$escape_str&bar.baz=$escape_str&baz.bar.foo=$escape_str&&arr.0.1=$escape_str");
50     
51     my $got = $c->request->parameters;
52     my $expected = {
53         'foo.1'       => 'bar',
54         'foo.2'       => $decode_str,
55         'bar.baz'     => $decode_str,
56         'baz.bar.foo' => $decode_str,
57         'arr.0.1'     => $decode_str,
58         'arr'         => [ [undef, $decode_str] ],
59         'foo'         => [undef, 'bar', $decode_str],
60         'bar'         => { baz => $decode_str },
61         'baz'         => { bar => { foo => $decode_str } },
62     };
63     
64     is( ref $got->{arr}->[0], 'ARRAY', '{arr}->[0] is ARRAY' );
65     ok( utf8::is_utf8( $got->{arr}->[0]->[1] ), '{arr}->[0]->[1] is utf8' );
66     ok( utf8::is_utf8( $got->{bar}{baz}      ), '{bar}{baz} is utf8' );
67     ok( utf8::is_utf8( $got->{baz}{bar}{foo} ), '{baz}{bar}{foo} is utf8' );
68     is_deeply($got, $expected, 'nested params' );
69 }
70
71 done_testing();