File: unicode-exception-return-value.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 (96 lines) | stat: -rw-r--r-- 2,529 bytes parent folder | download | duplicates (4)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use strict;
use warnings;
use Test::More;
use HTTP::Request::Common;

BEGIN {
    package TestApp::Controller::Root;
    $INC{'TestApp/Controller/Root.pm'} = __FILE__;

    use Moose;
    use MooseX::MethodAttributes;
    extends 'Catalyst::Controller';

    sub main :Path('') :Args(1) {
        my ($self, $c, $arg) = @_;
        my $body = $arg . "\n";
        my $query_params = $c->request->query_params;
        my $body_params = $c->request->body_params;
        foreach my $key (sort keys %$query_params) {
            $body .= "Q $key => " . $query_params->{$key} . "\n";
        }
        foreach my $key (sort keys %$body_params) {
            $body .= "B $key => " . $body_params->{$key} . "\n";
        }
        $c->res->body($body);
        $c->res->content_type('text/plain');
    }
    TestApp::Controller::Root->config(namespace => '');
}

{
    package TestApp;
    $INC{'TestApp.pm'} = __FILE__;

    use Catalyst;

    sub handle_unicode_encoding_exception {
        my ( $self, $param_value, $error_msg ) = @_;
        # totally dummy: we return any invalid string with a fixed
        # value. a more clever thing would be try to decode it from
        # latin1 or latin2.
        return "INVALID-UNICODE";
    }

    __PACKAGE__->setup;
}


use Catalyst::Test 'TestApp';

{
    my $res = request('/ok');
    is ($res->content, "ok\n", "app is echoing arguments");
}

{
    my $res = request('/%E2%C3%83%C6%92%C3%8');
    is ($res->content, "INVALID-UNICODE\n",
        "replacement ok in arguments");
}
{
    my $res = request('/p?valid_key=%e2');
    is ($res->content, "p\nQ valid_key => INVALID-UNICODE\n",
        "replacement ok in query");
}
{
    my $res = request('/p?%e2=%e2');
    is ($res->content, "p\nQ INVALID-UNICODE => INVALID-UNICODE\n",
        "replacement ok in query");
}
{
    my $req = POST "/p", Content => "%e2=%e2";
    my $res = request($req);
    is ($res->content, "p\nB INVALID-UNICODE => INVALID-UNICODE\n", "replacement ok in body");
}
{
    my $req = POST "/p", Content => "valid_key=%e2";
    my $res = request($req);
    is ($res->content, "p\nB valid_key => INVALID-UNICODE\n", "replacement ok in body");
}
{
    # and a superset of problems:
    my $req = POST "/%e5?%e3=%e3", Content => "%e4=%e4";
    my $res = request($req);
    my $expected = <<'BODY';
INVALID-UNICODE
Q INVALID-UNICODE => INVALID-UNICODE
B INVALID-UNICODE => INVALID-UNICODE
BODY
    is ($res->content, $expected, "Found the replacement strings everywhere");
}


done_testing;

#TestApp->to_app;