File: unicode_plugin_request_decode.t

package info (click to toggle)
libcatalyst-perl 5.90132-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: perl: 11,061; makefile: 7
file content (79 lines) | stat: -rw-r--r-- 1,997 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use strict;
use warnings;
use Test::More;
use utf8;

# setup library path
use FindBin qw($Bin);
use lib "$Bin/lib";

use Catalyst::Test 'TestAppUnicode';
use Encode;
use HTTP::Request::Common;
use URI::Escape qw/uri_escape_utf8/;
use HTTP::Status 'is_server_error';

my $encode_str = "\x{e3}\x{81}\x{82}"; # e38182 is japanese 'あ'
my $decode_str = Encode::decode('utf-8' => $encode_str);
my $escape_str = uri_escape_utf8($decode_str);

sub check_parameter {
    my ( undef, $c ) = ctx_request(shift);
    is $c->res->output => '<h1>It works</h1>';

    my $foo = $c->req->param('foo');
    is $foo, $decode_str;

    my $other_foo = $c->req->method eq 'POST'
        ? $c->req->upload('foo')
            ? $c->req->upload('foo')->filename
            : $c->req->body_parameters->{foo}
        : $c->req->query_parameters->{foo};

    is $other_foo => $decode_str;
}

sub check_argument {
    my ( undef, $c ) = ctx_request(shift);
    is $c->res->output => '<h1>It works</h1>';

    my $foo = $c->req->args->[0];
    is $foo => $decode_str;
}

sub check_capture {
    my ( undef, $c ) = ctx_request(shift);
    is $c->res->output => '<h1>It works</h1>';

    my $foo = $c->req->captures->[0];
    is $foo => $decode_str;
}

sub check_fallback {
  my ( $res, $c ) = ctx_request(shift);
  ok(!is_server_error($res->code)) or diag('Response code is: ' . $res->code);
}

check_parameter(GET "/?foo=$escape_str");
check_parameter(POST '/', ['foo' => $encode_str]);
check_parameter(POST '/',
    Content_Type => 'form-data',
    Content => [
        'foo' => [
            "$Bin/unicode_plugin_request_decode.t",
            $encode_str,
        ]
    ],
);

check_argument(GET "/$escape_str");
check_capture(GET "/capture/$escape_str");

# sending non-utf8 data
my $non_utf8_data = "%C3%E6%CB%AA";
check_fallback(GET "/?q=${non_utf8_data}");
check_fallback(GET "/${non_utf8_data}");
check_fallback(GET "/capture/${non_utf8_data}");
check_fallback(POST '/', ['foo' => $non_utf8_data]);

done_testing;