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