make sure we still properly decode the part name
[catagits/Catalyst-Runtime.git] / t / unicode_plugin_request_decode.t
CommitLineData
55046410 1use strict;
2use warnings;
4a62800d 3use Test::More;
55046410 4use utf8;
5
6# setup library path
7use FindBin qw($Bin);
8use lib "$Bin/lib";
9
10use Catalyst::Test 'TestAppUnicode';
11use Encode;
12use HTTP::Request::Common;
13use URI::Escape qw/uri_escape_utf8/;
14use HTTP::Status 'is_server_error';
15
16my $encode_str = "\x{e3}\x{81}\x{82}"; # e38182 is japanese 'あ'
17my $decode_str = Encode::decode('utf-8' => $encode_str);
18my $escape_str = uri_escape_utf8($decode_str);
19
55046410 20sub 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');
4a62800d 25 is $foo, $decode_str;
55046410 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};
4a62800d 32
55046410 33 is $other_foo => $decode_str;
34}
35
36sub 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];
55046410 41 is $foo => $decode_str;
42}
43
44sub 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];
55046410 49 is $foo => $decode_str;
50}
51
52sub check_fallback {
53 my ( $res, $c ) = ctx_request(shift);
54 ok(!is_server_error($res->code)) or diag('Response code is: ' . $res->code);
55}
4a62800d 56
57check_parameter(GET "/?foo=$escape_str");
58check_parameter(POST '/', ['foo' => $encode_str]);
59check_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
69check_argument(GET "/$escape_str");
70check_capture(GET "/capture/$escape_str");
71
72# sending non-utf8 data
73my $non_utf8_data = "%C3%E6%CB%AA";
74check_fallback(GET "/?q=${non_utf8_data}");
75check_fallback(GET "/${non_utf8_data}");
76check_fallback(GET "/capture/${non_utf8_data}");
77check_fallback(POST '/', ['foo' => $non_utf8_data]);
78
79done_testing;