fix issues with middleware stash localizing stuff
[catagits/Catalyst-Runtime.git] / t / unicode_plugin_request_decode.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use utf8;
5
6 # setup library path
7 use FindBin qw($Bin);
8 use lib "$Bin/lib";
9
10 use Catalyst::Test 'TestAppUnicode';
11 use Encode;
12 use HTTP::Request::Common;
13 use URI::Escape qw/uri_escape_utf8/;
14 use HTTP::Status 'is_server_error';
15
16 my $encode_str = "\x{e3}\x{81}\x{82}"; # e38182 is japanese 'あ'
17 my $decode_str = Encode::decode('utf-8' => $encode_str);
18 my $escape_str = uri_escape_utf8($decode_str);
19
20 sub check_parameter {
21     my ( undef, $c ) = ctx_request(shift);
22     is $c->res->output => '<h1>It works</h1>';
23
24     my $foo = $c->req->param('foo');
25     is $foo, $decode_str;
26
27     my $other_foo = $c->req->method eq 'POST'
28         ? $c->req->upload('foo')
29             ? $c->req->upload('foo')->filename
30             : $c->req->body_parameters->{foo}
31         : $c->req->query_parameters->{foo};
32
33     is $other_foo => $decode_str;
34 }
35
36 sub check_argument {
37     my ( undef, $c ) = ctx_request(shift);
38     is $c->res->output => '<h1>It works</h1>';
39
40     my $foo = $c->req->args->[0];
41     is $foo => $decode_str;
42 }
43
44 sub check_capture {
45     my ( undef, $c ) = ctx_request(shift);
46     is $c->res->output => '<h1>It works</h1>';
47
48     my $foo = $c->req->captures->[0];
49     is $foo => $decode_str;
50 }
51
52 sub check_fallback {
53   my ( $res, $c ) = ctx_request(shift);
54   ok(!is_server_error($res->code)) or diag('Response code is: ' . $res->code);
55 }
56
57 check_parameter(GET "/?foo=$escape_str");
58 check_parameter(POST '/', ['foo' => $encode_str]);
59 check_parameter(POST '/',
60     Content_Type => 'form-data',
61     Content => [
62         'foo' => [
63             "$Bin/unicode_plugin_request_decode.t",
64             $encode_str,
65         ]
66     ],
67 );
68
69 check_argument(GET "/$escape_str");
70 check_capture(GET "/capture/$escape_str");
71
72 # sending non-utf8 data
73 my $non_utf8_data = "%C3%E6%CB%AA";
74 check_fallback(GET "/?q=${non_utf8_data}");
75 check_fallback(GET "/${non_utf8_data}");
76 check_fallback(GET "/capture/${non_utf8_data}");
77 check_fallback(POST '/', ['foo' => $non_utf8_data]);
78
79 done_testing;