blib/lib/Mason/Plugin/WithEncoding/t/UTF8.pm

filter: Statementstmt 90.7% 78 / 86 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 90.6% 87 / 96 CC 2 SCAR 2.2
CC
2
Cov
91%
test_withencoding
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« blib/lib/Mason/Plugin/WithEncoding/Test/Class.pm ↑ Summary blib/lib/Mason/Plugin/WithEncoding/t/NoUTF8.pm »
1 package Mason::Plugin::WithEncoding::t::UTF8;
2 2 $Mason::Plugin::WithEncoding::t::UTF8::VERSION = '0.2';
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Test::Class::Most parent => 'Mason::Plugin::WithEncoding::Test::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Poet::Tools qw(dirname mkpath trim write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Setup stolen from Poet::t::Run and Poet::t::PSGIHandler
10  
11 2 ▶sub test_withencoding : Tests {
Subroutine test_withencoding: called CC=2 SCAR=7.0
Pod undocumented
12 2 my $self = shift;
13 2 my $poet_conf = shift;
14  
15 2 my $conf_utf8 = {
16 'layer' => 'production',
17 'server.port' => 9999,
18  
19 'mason.extra_plugins' => [qw(WithEncoding)],
20 'server.load_modules' => ['Mason::Plugin::WithEncoding'],
21 'server.encoding.request' => 'UTF-8',
22 'server.encoding.response' => 'UTF-8',
23 'server.default_content_type' => 'text/html; charset=UTF-8',
24 };
25  
26 2 my $poet = $self->temp_env(conf => $conf_utf8);
27 2 my $root_dir = $poet->root_dir;
28 2 my $run_log = "$root_dir/logs/run.log";
29  
30 2 ▶ if ( my $pid = fork() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 1 1 2
31 # parent
32 1 ▶ scope_guard { kill( 1, $pid ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 sleep(2);
34  
35 1 my $mech = $self->mech($poet);
36 1 $self->add_comps($poet);
37  
38 #
39 # Unescaping the query string doesn't produce love hearts, which I don't
40 # really understand, since this does:
41 #
42 # $ perl -MURI::Escape -e 'print uri_unescape("%E2%99%A5%E2%99%A5=%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5")."\n"'
43 # $ ♥♥=♥♥♥♥♥♥♥
44 #
45  
46 # utf8 config, utf8 content, utf8 url, utf8 query
47 1 $mech->get_ok("http://127.0.0.1:9999/♥♥♥?♥♥=♥♥♥♥♥♥♥");
48 # query string goes over wires as encoded ascii, so clients use url encoding to preserve information
49 1 $mech->content_unlike(qr/QUERY STRING FROM REQ: ♥♥=♥♥♥♥♥♥/);
50 1 $mech->content_like(qr[QUERY STRING FROM REQ: \Q%E2%99%A5%E2%99%A5=%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5\E]);
51 1 $mech->content_unlike(qr/QUERY STRING UNESCAPED: ♥♥=♥♥♥♥♥♥♥/);
52 #warn $mech->content;
53 1 $mech->content_like(qr/A QUICK BROWN FOX JUMPS OVER THE LAZY DOG/);
54 1 $mech->content_unlike(qr/a quick brown fox jumps over the lazy dog/);
55 1 $mech->content_like(qr/ΔΙΑΦΥΛΆΞΤΕ ΓΕΝΙΚΆ ΤΗ ΖΩΉ ΣΑΣ ΑΠΌ ΒΑΘΕΙΆ ΨΥΧΙΚΆ ΤΡΑΎΜΑΤΑ/);
56 1 $mech->content_unlike(qr/διαφυλάξτε γενικά τη ζωή σας από βαθειά ψυχικά τραύματα/);
57 1 is($mech->content_type, 'text/html', 'Got correct content type');
58 1 is($mech->response->content_type_charset, 'UTF-8', 'Got correct content-type charset');
59  
60 # utf8 config, utf8 content, utf8 url, no query
61 1 $mech->get_ok("http://127.0.0.1:9999/♥♥♥");
62 1 $mech->content_like(qr/A QUICK BROWN FOX JUMPS OVER THE LAZY DOG/);
63 1 $mech->content_unlike(qr/a quick brown fox jumps over the lazy dog/);
64 1 $mech->content_like(qr/ΔΙΑΦΥΛΆΞΤΕ ΓΕΝΙΚΆ ΤΗ ΖΩΉ ΣΑΣ ΑΠΌ ΒΑΘΕΙΆ ΨΥΧΙΚΆ ΤΡΑΎΜΑΤΑ/);
65 1 $mech->content_unlike(qr/διαφυλάξτε γενικά τη ζωή σας από βαθειά ψυχικά τραύματα/);
66 1 is($mech->content_type, 'text/html', 'Got correct content type');
67 1 is($mech->response->content_type_charset, 'UTF-8', 'Got correct content-type charset');
68  
69 # utf8 config, utf8 content, ascii url, utf8 query
70 1 $mech->get_ok("http://127.0.0.1:9999/utf8?♥♥=♥♥♥♥♥♥♥");
71 # query string goes over wires as encoded ascii, so clients use url encoding to preserve information
72 1 $mech->content_unlike(qr/QUERY STRING FROM REQ: ♥♥=♥♥♥♥♥♥/);
73 1 $mech->content_like(qr[QUERY STRING FROM REQ: \Q%E2%99%A5%E2%99%A5=%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5%E2%99%A5\E]);
74 1 $mech->content_unlike(qr/QUERY STRING UNESCAPED: ♥♥=♥♥♥♥♥♥♥/);
75 #warn $mech->content;
76 1 $mech->content_like(qr/A QUICK BROWN FOX JUMPS OVER THE LAZY DOG/);
77 1 $mech->content_unlike(qr/a quick brown fox jumps over the lazy dog/);
78 1 $mech->content_like(qr/ΔΙΑΦΥΛΆΞΤΕ ΓΕΝΙΚΆ ΤΗ ΖΩΉ ΣΑΣ ΑΠΌ ΒΑΘΕΙΆ ΨΥΧΙΚΆ ΤΡΑΎΜΑΤΑ/);
79 1 $mech->content_unlike(qr/διαφυλάξτε γενικά τη ζωή σας από βαθειά ψυχικά τραύματα/);
80 1 is($mech->content_type, 'text/html', 'Got correct content type');
81 1 is($mech->response->content_type_charset, 'UTF-8', 'Got correct content-type charset');
82  
83 # utf8 config, utf8 content, ascii url, no query
84 1 $mech->get_ok("http://127.0.0.1:9999/utf8");
85 1 $mech->content_like(qr/A QUICK BROWN FOX JUMPS OVER THE LAZY DOG/);
86 1 $mech->content_unlike(qr/a quick brown fox jumps over the lazy dog/);
87 1 $mech->content_like(qr/ΔΙΑΦΥΛΆΞΤΕ ΓΕΝΙΚΆ ΤΗ ΖΩΉ ΣΑΣ ΑΠΌ ΒΑΘΕΙΆ ΨΥΧΙΚΆ ΤΡΑΎΜΑΤΑ/);
88 1 $mech->content_unlike(qr/διαφυλάξτε γενικά τη ζωή σας από βαθειά ψυχικά τραύματα/);
89 1 is($mech->content_type, 'text/html', 'Got correct content type');
90 1 is($mech->response->content_type_charset, 'UTF-8', 'Got correct content-type charset');
91  
92 # utf8 config, plain content, plain url, no query
93 1 $mech->get_ok("http://127.0.0.1:9999/plain");
94 1 $mech->content_like(qr/LOREM IPSUM DOLOR SIT AMET/);
95 1 $mech->content_unlike(qr/Lorem ipsum dolor sit amet/);
96 1 is($mech->content_type, 'text/html', 'Got correct content type');
97 1 is($mech->response->content_type_charset, 'UTF-8', 'Got correct content-type charset');
98  
99 # utf8 config, chokes on $.args->{♥} in the page, looks like a bug
100 1 $mech->get("http://127.0.0.1:9999/dies");
101 1 ok($mech->status == 500, 'UTF8 content bug');
102 1 is($mech->content_type, '', 'Got correct content type');
103 1 is($mech->response->content_type_charset, undef, 'Got correct content-type charset');
104  
105 # utf8 config, json content
106 1 $mech->get_ok("http://127.0.0.1:9999/json");
107 #warn $mech->content;
108 1 my $expected_from_json = {
109 foo => 'bar',
110 baz => [qw(barp beep)],
111 9 => { one => 1, ex => 'EKS' },
112 heart => '♥',
113 };
114  
115 1 cmp_deeply(JSON->new->utf8->decode($mech->content), $expected_from_json, 'Decoded and de-JSONified expected data from JSON');
116  
117 # this doesn't work
118 #my $expected_mangled_from_json = {%$expected_from_json, heart => '�'}; # �
119 # I can't figure out how to get this test to work. The cmp_deeply fails if fed
120 # the unmangled hashref as expected, I
121 # just can't figure out how to mangle the expected hashref to match the mangled hashref
122 # retrieved from the $mech content
123 #cmp_deeply(JSON->new->decode($mech->content), $expected_mangled_from_json, 'Mangled data from JSON'); # fails '�'
124  
125 0 my $from_decoded_utf8 = JSON->new->utf8->decode($mech->content); # should work
126 0 my $not_decoded = JSON->new->decode($mech->content); # should break
127  
128 0 ok($from_decoded_utf8->{heart}, 'My heart is true');
129 0 is($from_decoded_utf8->{heart}, '♥', 'Found my true ♥');
130 0 ok($not_decoded->{heart}, 'My heart is true');
131 0 ok($not_decoded->{heart} ne '♥', 'My heart has been mangled');
132  
133 0 is($mech->content_type, 'application/json', 'Got correct content type');
134 0 is($mech->response->content_type_charset, 'UTF-8', 'Got correct content-type charset');
135 }
136 else {
137 # child
138 1 close STDOUT;
139 1 close STDERR;
140 1 exec( $poet->bin_path("run.pl > $run_log 2>&1") );
141 }
142 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 2 1;
« blib/lib/Mason/Plugin/WithEncoding/Test/Class.pm ↑ Summary blib/lib/Mason/Plugin/WithEncoding/t/NoUTF8.pm »