set correct %INC
[catagits/Catalyst-Runtime.git] / t / utf_incoming.t
CommitLineData
0ca510f0 1use utf8;
2use warnings;
3use strict;
4use Test::More;
b9d96e27 5use HTTP::Request::Common;
d2000928 6use HTTP::Message::PSGI ();
be634ffb 7use Encode 2.21 'decode_utf8', 'encode_utf8', 'encode';
59e11cd7 8use File::Spec;
12982f86 9use JSON::MaybeXS;
0d94e986 10use Scalar::Util ();
0ca510f0 11
12# Test cases for incoming utf8
13
14{
15 package MyApp::Controller::Root;
16 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
17
18 use base 'Catalyst::Controller';
19
20 sub heart :Path('♥') {
21 my ($self, $c) = @_;
22 $c->response->content_type('text/html');
23 $c->response->body("<p>This is path-heart action ♥</p>");
24 # We let the content length middleware find the length...
25 }
26
27 sub hat :Path('^') {
28 my ($self, $c) = @_;
29 $c->response->content_type('text/html');
30 $c->response->body("<p>This is path-hat action ^</p>");
31 }
32
e5a5e80b 33 sub uri_for :Path('uri_for') {
34 my ($self, $c) = @_;
35 $c->response->content_type('text/html');
58b80ff1 36 $c->response->body("${\$c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥#X♥X', {'♥'=>'♥♥'})}");
e5a5e80b 37 }
38
39 sub heart_with_arg :Path('a♥') Args(1) {
40 my ($self, $c, $arg) = @_;
41 $c->response->content_type('text/html');
42 $c->response->body("<p>This is path-heart-arg action $arg</p>");
43 Test::More::is $c->req->args->[0], '♥';
44 }
45
0ca510f0 46 sub base :Chained('/') CaptureArgs(0) { }
47 sub link :Chained('base') PathPart('♥') Args(0) {
48 my ($self, $c) = @_;
49 $c->response->content_type('text/html');
50 $c->response->body("<p>This is base-link action ♥</p>");
51 }
e5a5e80b 52 sub arg :Chained('base') PathPart('♥') Args(1) {
53 my ($self, $c, $arg) = @_;
54 $c->response->content_type('text/html');
55 $c->response->body("<p>This is base-link action ♥ $arg</p>");
56 }
57 sub capture :Chained('base') PathPart('♥') CaptureArgs(1) {
58 my ($self, $c, $arg) = @_;
59 $c->stash(capture=>$arg);
60 }
61 sub argend :Chained('capture') PathPart('♥') Args(1) {
62 my ($self, $c, $arg) = @_;
63 $c->response->content_type('text/html');
64
65 Test::More::is $c->req->args->[0], '♥';
66 Test::More::is $c->req->captures->[0], '♥';
69fa672d 67 Test::More::is $arg, '♥';
68 Test::More::is length($arg), 1, "got length of one";
e5a5e80b 69
70 $c->response->body("<p>This is base-link action ♥ ${\$c->req->args->[0]}</p>");
dd096a3a 71
72 # Test to make sure redirect can now take an object (sorry don't have a better place for it
73 # but wanted test coverage.
74 my $location = $c->res->redirect( $c->uri_for($c->controller('Root')->action_for('uri_for')) );
75 Test::More::ok !ref $location;
e5a5e80b 76 }
0ca510f0 77
dd096a3a 78 sub stream_write :Local {
79 my ($self, $c) = @_;
80 $c->response->content_type('text/html');
81 $c->response->write("<p>This is stream_write action ♥</p>");
82 }
83
fe1dfeaf 84 sub stream_write_fh :Local {
85 my ($self, $c) = @_;
86 $c->response->content_type('text/html');
87
88 my $writer = $c->res->write_fh;
e8361cf8 89 $writer->write_encoded('<p>This is stream_write_fh action ♥</p>');
59e11cd7 90 $writer->close;
91 }
92
e8361cf8 93 # Stream a file with utf8 chars directly, you don't need to decode
59e11cd7 94 sub stream_body_fh :Local {
95 my ($self, $c) = @_;
59e11cd7 96 my $path = File::Spec->catfile('t', 'utf8.txt');
97 open(my $fh, '<', $path) || die "trouble: $!";
98 $c->response->content_type('text/html');
99 $c->response->body($fh);
fe1dfeaf 100 }
101
e8361cf8 102 # If you pull the file contents into a var, NOW you need to specify the
103 # IO encoding on the FH. Ultimately Plack at the end wants bytes...
104 sub stream_body_fh2 :Local {
105 my ($self, $c) = @_;
106 my $path = File::Spec->catfile('t', 'utf8.txt');
107 open(my $fh, '<:encoding(UTF-8)', $path) || die "trouble: $!";
108 my $contents = do { local $/; <$fh> };
109
110 $c->response->content_type('text/html');
111 $c->response->body($contents);
112 }
113
1728aeb7 114 sub write_then_body :Local {
115 my ($self, $c) = @_;
9c056c82 116
117 $c->res->content_type('text/html');
1728aeb7 118 $c->res->write("<p>This is early_write action ♥</p>");
119 $c->res->body("<p>This is body_write action ♥</p>");
120 }
121
12982f86 122 sub file_upload :POST Consumes(Multipart) Local {
123 my ($self, $c) = @_;
b0ff1be8 124
12982f86 125 Test::More::is $c->req->body_parameters->{'♥'}, '♥♥';
126 Test::More::ok my $upload = $c->req->uploads->{file};
6adc45cf 127 Test::More::is $upload->charset, 'UTF-8';
12982f86 128
129 my $text = $upload->slurp;
130 Test::More::is Encode::decode_utf8($text), "<p>This is stream_body_fh action ♥</p>\n";
131
6adc45cf 132 my $decoded_text = $upload->decoded_slurp;
133 Test::More::is $decoded_text, "<p>This is stream_body_fh action ♥</p>\n";
134
135 Test::More::is $upload->filename, '♥ttachment.txt';
136 Test::More::is $upload->raw_basename, '♥ttachment.txt';
137
12982f86 138 $c->response->content_type('text/html');
6adc45cf 139 $c->response->body($decoded_text);
12982f86 140 }
141
0d6aa161 142 sub file_upload_utf8_param :POST Consumes(Multipart) Local {
143 my ($self, $c) = @_;
144
145 Test::More::is $c->req->body_parameters->{'♥'}, '♥♥';
146 Test::More::ok my $upload = $c->req->uploads->{'♥'};
147 Test::More::is $upload->charset, 'UTF-8';
148
149 my $text = $upload->slurp;
150 Test::More::is Encode::decode_utf8($text), "<p>This is stream_body_fh action ♥</p>\n";
151
152 my $decoded_text = $upload->decoded_slurp;
153 Test::More::is $decoded_text, "<p>This is stream_body_fh action ♥</p>\n";
154
155 Test::More::is $upload->filename, '♥ttachment.txt';
156 Test::More::is $upload->raw_basename, '♥ttachment.txt';
157
158 $c->response->content_type('text/html');
159 $c->response->body($decoded_text);
160 }
161
12982f86 162 sub json :POST Consumes(JSON) Local {
163 my ($self, $c) = @_;
164 my $post = $c->req->body_data;
165
166 Test::More::is $post->{'♥'}, '♥♥';
69fa672d 167 Test::More::is length($post->{'♥'}), 2;
12982f86 168 $c->response->content_type('application/json');
169
170 # Encode JSON also encodes to a UTF-8 encoded, binary string. This is why we don't
171 # have application/json as one of the things we match, otherwise we get double
172 # encoding.
173 $c->response->body(JSON::MaybeXS::encode_json($post));
174 }
e8361cf8 175
6adc45cf 176 ## If someone clears encoding, they can do as they wish
177 sub manual_1 :Local {
178 my ($self, $c) = @_;
c5661910 179 $c->clear_encoding;
6adc45cf 180 $c->res->content_type('text/plain');
181 $c->res->content_type_charset('UTF-8');
182 $c->response->body( Encode::encode_utf8("manual_1 ♥"));
183 }
184
185 ## If you do like gzip, well handle that yourself! Basically if you do some sort
186 ## of content encoding like gzip, you must do on top of the encoding. We will fix
187 ## the encoding plugins (Catalyst::Plugin::Compress) to do this properly for you.
188 #
189 sub gzipped :Local {
190 require Compress::Zlib;
191 my ($self, $c) = @_;
192 $c->res->content_type('text/plain');
193 $c->res->content_type_charset('UTF-8');
194 $c->res->content_encoding('gzip');
195 $c->response->body(Compress::Zlib::memGzip(Encode::encode_utf8("manual_1 ♥")));
196 }
197
69fa672d 198 sub override_encoding :Local {
199 my ($self, $c) = @_;
200 $c->res->content_type('text/plain');
70005e98 201 $c->encoding(Encode::find_encoding('UTF-8'));
69fa672d 202 $c->encoding(Encode::find_encoding('Shift_JIS'));
203 $c->response->body("テスト");
204 }
205
70005e98 206 sub stream_write_error :Local {
207 my ($self, $c) = @_;
208 $c->response->content_type('text/html');
209 $c->response->write("<p>This is stream_write action ♥</p>");
210 $c->encoding(Encode::find_encoding('Shift_JIS'));
211 $c->response->write("<p>This is stream_write action ♥</p>");
212 }
213
d2000928 214 sub from_external_psgi :Local {
215 my ($self, $c) = @_;
216 my $env = HTTP::Message::PSGI::req_to_psgi( HTTP::Request::Common::GET '/root/♥');
217 $c->res->from_psgi_response( ref($c)->to_app->($env));
218 }
219
be634ffb 220 sub echo_arg :Local {
221 my ($self, $c) = @_;
222 $c->response->content_type('text/plain');
223 $c->response->body($c->req->body_parameters->{arg});
224 }
225
0ca510f0 226 package MyApp;
227 use Catalyst;
228
0ca510f0 229 Test::More::ok(MyApp->setup, 'setup app');
230}
231
232ok my $psgi = MyApp->psgi_app, 'build psgi app';
233
234use Catalyst::Test 'MyApp';
0ca510f0 235
236{
237 my $res = request "/root/♥";
238
239 is $res->code, 200, 'OK';
240 is decode_utf8($res->content), '<p>This is path-heart action ♥</p>', 'correct body';
241 is $res->content_length, 36, 'correct length';
4a64c27b 242 is $res->content_charset, 'UTF-8';
0ca510f0 243}
244
245{
e5a5e80b 246 my $res = request "/root/a♥/♥";
247
248 is $res->code, 200, 'OK';
249 is decode_utf8($res->content), '<p>This is path-heart-arg action ♥</p>', 'correct body';
250 is $res->content_length, 40, 'correct length';
4a64c27b 251 is $res->content_charset, 'UTF-8';
e5a5e80b 252}
253
254{
0ca510f0 255 my $res = request "/root/^";
256
257 is $res->code, 200, 'OK';
258 is decode_utf8($res->content), '<p>This is path-hat action ^</p>', 'correct body';
259 is $res->content_length, 32, 'correct length';
4a64c27b 260 is $res->content_charset, 'UTF-8';
0ca510f0 261}
262
263{
264 my $res = request "/base/♥";
265
266 is $res->code, 200, 'OK';
267 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
268 is $res->content_length, 35, 'correct length';
4a64c27b 269 is $res->content_charset, 'UTF-8';
0ca510f0 270}
271
272{
b9d96e27 273 my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥'];
0ca510f0 274
275 is $res->code, 200, 'OK';
276 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
277 is $res->content_length, 35, 'correct length';
b9d96e27 278 is $c->req->parameters->{'♥'}[0], '♥';
279 is $c->req->query_parameters->{'♥'}[0], '♥';
280 is $c->req->body_parameters->{'♥'}[0], '♥';
281 is $c->req->parameters->{'♥'}[0], '♥';
4a62800d 282 is $c->req->parameters->{a}, 1;
283 is $c->req->body_parameters->{a}, 1;
4a64c27b 284 is $res->content_charset, 'UTF-8';
e5a5e80b 285}
4a62800d 286
e5a5e80b 287{
288 my ($res, $c) = ctx_request GET "/base/♥?♥♥♥";
4a62800d 289
e5a5e80b 290 is $res->code, 200, 'OK';
291 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
292 is $res->content_length, 35, 'correct length';
293 is $c->req->query_keywords, '♥♥♥';
4a64c27b 294 is $res->content_charset, 'UTF-8';
0ca510f0 295}
296
e5a5e80b 297{
298 my $res = request "/base/♥/♥";
299
300 is $res->code, 200, 'OK';
301 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
302 is $res->content_length, 39, 'correct length';
4a64c27b 303 is $res->content_charset, 'UTF-8';
e5a5e80b 304}
b9d96e27 305
e5a5e80b 306{
307 my $res = request "/base/♥/♥/♥/♥";
308
e5a5e80b 309 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
310 is $res->content_length, 39, 'correct length';
4a64c27b 311 is $res->content_charset, 'UTF-8';
e5a5e80b 312}
313
314{
315 my ($res, $c) = ctx_request POST "/base/♥/♥/♥/♥?♥=♥♥", [a=>1, b=>'2', '♥'=>'♥♥'];
316
317 ## Make sure that the urls we generate work the same
b063a165 318 my $uri_for1 = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
319 my $uri_for2 = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥', '♥'], {'♥'=>'♥♥'});
e5a5e80b 320 my $uri = $c->req->uri;
321
b063a165 322 is "$uri_for1", "$uri_for2";
323 is "$uri", "$uri_for1";
e5a5e80b 324
325 {
b063a165 326 my ($res, $c) = ctx_request POST "$uri_for1", [a=>1, b=>'2', '♥'=>'♥♥'];
e5a5e80b 327 is $c->req->query_parameters->{'♥'}, '♥♥';
328 is $c->req->body_parameters->{'♥'}, '♥♥';
329 is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body
69fa672d 330 is $c->req->args->[0], '♥';
331 is length($c->req->parameters->{'♥'}[0]), 2;
332 is length($c->req->query_parameters->{'♥'}), 2;
333 is length($c->req->body_parameters->{'♥'}), 2;
334 is length($c->req->args->[0]), 1;
4a64c27b 335 is $res->content_charset, 'UTF-8';
e5a5e80b 336 }
337}
338
339{
340 my ($res, $c) = ctx_request "/root/uri_for";
58b80ff1 341 my $url = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥#X♥X', {'♥'=>'♥♥'});
e5a5e80b 342
343 is $res->code, 200, 'OK';
344 is decode_utf8($res->content), "$url", 'correct body'; #should do nothing
345 is $res->content, "$url", 'correct body';
6b9f9ef7 346 is $res->content_length, 104, 'correct length';
4a64c27b 347 is $res->content_charset, 'UTF-8';
b063a165 348
349 {
350 my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), '♥');
6adc45cf 351 is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5', "correct $url";
b063a165 352 }
353
354 {
355 my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), ['♥']);
6adc45cf 356 is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5', "correct $url";
b063a165 357 }
dd096a3a 358}
359
360{
361 my $res = request "/root/stream_write";
00038a21 362
6adc45cf 363 is $res->code, 200, 'OK GET /root/stream_write';
dd096a3a 364 is decode_utf8($res->content), '<p>This is stream_write action ♥</p>', 'correct body';
4a64c27b 365 is $res->content_charset, 'UTF-8';
e5a5e80b 366}
0ca510f0 367
fe1dfeaf 368{
59e11cd7 369 my $res = request "/root/stream_body_fh";
370
371 is $res->code, 200, 'OK';
372 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
4a64c27b 373 is $res->content_charset, 'UTF-8';
59e11cd7 374 # Not sure why there is a trailing newline above... its not in catalyst code I can see. Not sure
375 # if is a problem or just an artifact of the why the test stuff works - JNAP
376}
377
378{
7b39dea1 379 my $res = request "/root/stream_write_fh";
fe1dfeaf 380
381 is $res->code, 200, 'OK';
382 is decode_utf8($res->content), '<p>This is stream_write_fh action ♥</p>', 'correct body';
8a79126d 383 #is $res->content_length, 41, 'correct length';
4a64c27b 384 is $res->content_charset, 'UTF-8';
fe1dfeaf 385}
dd096a3a 386
e8361cf8 387{
388 my $res = request "/root/stream_body_fh2";
389
390 is $res->code, 200, 'OK';
391 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
392 is $res->content_length, 41, 'correct length';
393 is $res->content_charset, 'UTF-8';
394}
395
12982f86 396{
1728aeb7 397 my $res = request "/root/write_then_body";
398
399 is $res->code, 200, 'OK';
400 is decode_utf8($res->content), "<p>This is early_write action ♥</p><p>This is body_write action ♥</p>";
401 is $res->content_charset, 'UTF-8';
402}
403
404{
12982f86 405 ok my $path = File::Spec->catfile('t', 'utf8.txt');
406 ok my $req = POST '/root/file_upload',
407 Content_Type => 'form-data',
6adc45cf 408 Content => [encode_utf8('♥')=>encode_utf8('♥♥'), file=>["$path", encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8', ]];
12982f86 409
410 ok my $res = request $req;
411 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n";
412}
413
414{
0d6aa161 415 ok my $path = File::Spec->catfile('t', 'utf8.txt');
416 ok my $req = POST '/root/file_upload_utf8_param',
417 Content_Type => 'form-data',
418 Content => [encode_utf8('♥')=>encode_utf8('♥♥'), encode_utf8('♥')=>["$path", encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8', ]];
419
420 ok my $res = request $req;
421 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n";
422}
423
424{
12982f86 425 ok my $req = POST '/root/json',
426 Content_Type => 'application/json',
427 Content => encode_json +{'♥'=>'♥♥'}; # Note: JSON does the UTF* encoding for us
428
429 ok my $res = request $req;
430
431 ## decode_json expect the binary utf8 string and does the decoded bit for us.
ddc88fbd 432 is_deeply decode_json(($res->content)), +{'♥'=>'♥♥'}, 'JSON was decoded correctly';
12982f86 433}
434
6adc45cf 435{
69fa672d 436 ok my $res = request "/root/override_encoding";
437 ok my $enc = Encode::find_encoding('SHIFT_JIS');
438
439 is $res->code, 200, 'OK';
440 is $enc->decode($res->content), "テスト", 'correct body';
441 is $res->content_length, 6, 'correct length'; # Bytes over the wire
442 is length($enc->decode($res->content)), 3;
ddc88fbd 443 is $res->content_charset, 'SHIFT_JIS', 'content charset is SHIFT_JIS as expected';
69fa672d 444}
445
446{
6adc45cf 447 my $res = request "/root/manual_1";
448
449 is $res->code, 200, 'OK';
450 is decode_utf8($res->content), "manual_1 ♥", 'correct body';
451 is $res->content_length, 12, 'correct length';
452 is $res->content_charset, 'UTF-8';
453}
454
455SKIP: {
456 eval { require Compress::Zlib; 1} || do {
457 skip "Compress::Zlib needed to test gzip encoding", 5 };
458
459 my $res = request "/root/gzipped";
460 ok my $raw_content = $res->content;
461 ok my $content = Compress::Zlib::memGunzip($raw_content), 'no gunzip error';
462
463 is $res->code, 200, 'OK';
464 is decode_utf8($content), "manual_1 ♥", 'correct body';
ddc88fbd 465 is $res->content_charset, 'UTF-8', 'zlib charset is set correctly';
6adc45cf 466}
467
70005e98 468{
469 my $res = request "/root/stream_write_error";
470
471 is $res->code, 200, 'OK';
472 like decode_utf8($res->content), qr[<p>This is stream_write action ♥</p><!DOCTYPE html], 'correct body';
473}
474
d2000928 475{
476 my $res = request "/root/from_external_psgi";
477
478 is $res->code, 200, 'OK';
479 is decode_utf8($res->content), '<p>This is path-heart action ♥</p>', 'correct body';
480 is $res->content_length, 36, 'correct length';
ddc88fbd 481 is $res->content_charset, 'UTF-8', 'external PSGI app has expected charset';
d2000928 482}
70005e98 483
c4d66db2 484{
be634ffb 485 my $utf8 = 'test ♥';
486 my $shiftjs = 'test テスト';
487
488 ok my $req = POST '/root/echo_arg',
489 Content_Type => 'form-data',
490 Content => [
491 arg0 => 'helloworld',
0d94e986 492 Encode::encode('UTF-8','♥') => Encode::encode('UTF-8','♥♥'), # Long form POST simple does not auto encode...
c463b49c 493 Encode::encode('UTF-8','♥♥♥') => [
494 undef, '',
495 'Content-Type' =>'text/plain; charset=SHIFT_JIS',
496 'Content' => Encode::encode('SHIFT_JIS', $shiftjs)],
be634ffb 497 arg1 => [
498 undef, '',
499 'Content-Type' =>'text/plain; charset=UTF-8',
500 'Content' => Encode::encode('UTF-8', $utf8)],
501 arg2 => [
502 undef, '',
503 'Content-Type' =>'text/plain; charset=SHIFT_JIS',
504 'Content' => Encode::encode('SHIFT_JIS', $shiftjs)],
505 arg2 => [
506 undef, '',
507 'Content-Type' =>'text/plain; charset=SHIFT_JIS',
508 'Content' => Encode::encode('SHIFT_JIS', $shiftjs)],
509 ];
510
511 my ($res, $c) = ctx_request $req;
512
ddc88fbd 513 is $c->req->body_parameters->{'arg0'}, 'helloworld', 'got helloworld value';
0d94e986 514 is $c->req->body_parameters->{'♥'}, '♥♥';
b0ff1be8 515 is $c->req->body_parameters->{'arg1'}, $utf8, 'decoded utf8 param';
516 is $c->req->body_parameters->{'arg2'}[0], $shiftjs, 'decoded shiftjs param';
517 is $c->req->body_parameters->{'arg2'}[1], $shiftjs, 'decoded shiftjs param';
518 is $c->req->body_parameters->{'♥♥♥'}, $shiftjs, 'decoded shiftjs param';
be634ffb 519
520}
521
6cf77e11 522{
523 my $shiftjs = 'test テスト';
524 my $encoded = Encode::encode('UTF-8', $shiftjs);
525
526 ok my $req = GET "/root/echo_arg?a=$encoded";
527 my ($res, $c) = ctx_request $req;
528
529 is $c->req->query_parameters->{'a'}, $shiftjs, 'got expected value';
530}
531
12982f86 532## should we use binmode on filehandles to force the encoding...?
533## Not sure what else to do with multipart here, if docs are enough...
534
0ca510f0 535done_testing;