-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicode.t
83 lines (58 loc) · 2.8 KB
/
unicode.t
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
use Kelp::Base -strict;
use Kelp;
use Kelp::Test;
use Test::More;
use HTTP::Request::Common;
use Encode;
use URI::Escape;
use utf8;
my $app = Kelp->new( mode => 'test' );
my $t = Kelp::Test->new( app => $app );
my $test_string = 'zażółć gęslą jaźń ZAŻÓŁĆ GĘŚLĄ JAŹŃ';
$app->add_route( [ POST => '/path_echo/:echo' ] => sub { return $_[1]; } );
$app->add_route( [ POST => '/body_echo' ] => sub { return $_[0]->param('śś'); } );
$app->add_route( [ POST => '/json_echo' ] => sub { return { 'śś' => $_[0]->param('śś') }; } );
subtest 'path encoding no charset ok' => sub {
my $string = uri_escape $app->charset_encode($test_string);
_t("/path_echo/$string", 'application/x-www-form-urlencoded', '', 200, encode($app->charset, $test_string));
};
subtest 'path encoding cp1250 ok' => sub {
my $string = uri_escape encode 'cp1250', $test_string;
_t("/path_echo/$string", 'application/x-www-form-urlencoded; charset=cp1250', '', 200, encode($app->charset, $test_string));
};
subtest 'plaintext encoding no charset ok' => sub {
my $string = join '=', map { uri_escape $app->charset_encode($_) } 'śś', $test_string;
_t('/body_echo', 'application/x-www-form-urlencoded', $string, 200, encode($app->charset, $test_string));
};
subtest 'plaintext encoding utf8 ok' => sub {
my $string = join '=', map { uri_escape encode 'utf-8', $_ } 'śś', $test_string;
_t('/body_echo', 'application/x-www-form-urlencoded; charset=utf-8', $string, 200, encode($app->charset, $test_string));
};
subtest 'plaintext encoding cp1250 ok' => sub {
my $string = join '=', map { uri_escape encode 'cp1250', $_ } 'śś', $test_string;
_t('/body_echo', 'application/x-www-form-urlencoded; charset=cp1250', $string, 200, encode($app->charset, $test_string));
};
subtest 'plaintext encoding unknown is utf8 ok' => sub {
my $string = join '=', map { uri_escape encode 'utf-8', $_ } 'śś', $test_string;
_t('/body_echo', 'application/x-www-form-urlencoded; charset=xxnotanencoding', $string, 200, encode($app->charset, $test_string));
};
subtest 'plaintext encoding unknown is not utf8 error ok' => sub {
my $string = join '=', map { uri_escape encode 'cp1252', $_ } 'śś', $test_string;
_t('/body_echo', 'application/x-www-form-urlencoded; charset=xxnotanencoding', $string, 500);
};
subtest 'json encoding ok' => sub {
my $string = Encode::encode('UTF-8', '{"śś":"' . $test_string . '"}');
_t('/json_echo', 'application/json', $string, 200, $string);
};
sub _t {
my ( $target, $ct, $content, $code, $expected, %headers) = @_;
$t->request( POST $target,
'Content-Type' => $ct,
%headers,
'Content' => $content,
)->code_is($code);
if ($expected) {
is $t->res->content, $expected, "expected string to $target ($ct) ok"
}
}
done_testing;