if we change encoding on $c after asking for a write_fh we want to make sure catalyst...
[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');
170 $c->encoding(Encode::find_encoding('Shift_JIS'));
171 $c->response->body("テスト");
172 }
173
0ca510f0 174 package MyApp;
175 use Catalyst;
176
0ca510f0 177 Test::More::ok(MyApp->setup, 'setup app');
178}
179
180ok my $psgi = MyApp->psgi_app, 'build psgi app';
181
182use Catalyst::Test 'MyApp';
0ca510f0 183
184{
185 my $res = request "/root/♥";
186
187 is $res->code, 200, 'OK';
188 is decode_utf8($res->content), '<p>This is path-heart action ♥</p>', 'correct body';
189 is $res->content_length, 36, 'correct length';
4a64c27b 190 is $res->content_charset, 'UTF-8';
0ca510f0 191}
192
193{
e5a5e80b 194 my $res = request "/root/a♥/♥";
195
196 is $res->code, 200, 'OK';
197 is decode_utf8($res->content), '<p>This is path-heart-arg action ♥</p>', 'correct body';
198 is $res->content_length, 40, 'correct length';
4a64c27b 199 is $res->content_charset, 'UTF-8';
e5a5e80b 200}
201
202{
0ca510f0 203 my $res = request "/root/^";
204
205 is $res->code, 200, 'OK';
206 is decode_utf8($res->content), '<p>This is path-hat action ^</p>', 'correct body';
207 is $res->content_length, 32, 'correct length';
4a64c27b 208 is $res->content_charset, 'UTF-8';
0ca510f0 209}
210
211{
212 my $res = request "/base/♥";
213
214 is $res->code, 200, 'OK';
215 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
216 is $res->content_length, 35, 'correct length';
4a64c27b 217 is $res->content_charset, 'UTF-8';
0ca510f0 218}
219
220{
b9d96e27 221 my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥'];
0ca510f0 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';
b9d96e27 226 is $c->req->parameters->{'♥'}[0], '♥';
227 is $c->req->query_parameters->{'♥'}[0], '♥';
228 is $c->req->body_parameters->{'♥'}[0], '♥';
229 is $c->req->parameters->{'♥'}[0], '♥';
4a62800d 230 is $c->req->parameters->{a}, 1;
231 is $c->req->body_parameters->{a}, 1;
4a64c27b 232 is $res->content_charset, 'UTF-8';
e5a5e80b 233}
4a62800d 234
e5a5e80b 235{
236 my ($res, $c) = ctx_request GET "/base/♥?♥♥♥";
4a62800d 237
e5a5e80b 238 is $res->code, 200, 'OK';
239 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
240 is $res->content_length, 35, 'correct length';
241 is $c->req->query_keywords, '♥♥♥';
4a64c27b 242 is $res->content_charset, 'UTF-8';
0ca510f0 243}
244
e5a5e80b 245{
246 my $res = request "/base/♥/♥";
247
248 is $res->code, 200, 'OK';
249 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
250 is $res->content_length, 39, 'correct length';
4a64c27b 251 is $res->content_charset, 'UTF-8';
e5a5e80b 252}
b9d96e27 253
e5a5e80b 254{
255 my $res = request "/base/♥/♥/♥/♥";
256
e5a5e80b 257 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
258 is $res->content_length, 39, 'correct length';
4a64c27b 259 is $res->content_charset, 'UTF-8';
e5a5e80b 260}
261
262{
263 my ($res, $c) = ctx_request POST "/base/♥/♥/♥/♥?♥=♥♥", [a=>1, b=>'2', '♥'=>'♥♥'];
264
265 ## Make sure that the urls we generate work the same
b063a165 266 my $uri_for1 = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
267 my $uri_for2 = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥', '♥'], {'♥'=>'♥♥'});
e5a5e80b 268 my $uri = $c->req->uri;
269
b063a165 270 is "$uri_for1", "$uri_for2";
271 is "$uri", "$uri_for1";
e5a5e80b 272
273 {
b063a165 274 my ($res, $c) = ctx_request POST "$uri_for1", [a=>1, b=>'2', '♥'=>'♥♥'];
e5a5e80b 275 is $c->req->query_parameters->{'♥'}, '♥♥';
276 is $c->req->body_parameters->{'♥'}, '♥♥';
277 is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body
69fa672d 278 is $c->req->args->[0], '♥';
279 is length($c->req->parameters->{'♥'}[0]), 2;
280 is length($c->req->query_parameters->{'♥'}), 2;
281 is length($c->req->body_parameters->{'♥'}), 2;
282 is length($c->req->args->[0]), 1;
4a64c27b 283 is $res->content_charset, 'UTF-8';
e5a5e80b 284 }
285}
286
287{
288 my ($res, $c) = ctx_request "/root/uri_for";
289 my $url = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
290
291 is $res->code, 200, 'OK';
292 is decode_utf8($res->content), "$url", 'correct body'; #should do nothing
293 is $res->content, "$url", 'correct body';
294 is $res->content_length, 90, 'correct length';
4a64c27b 295 is $res->content_charset, 'UTF-8';
b063a165 296
297 {
298 my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), '♥');
6adc45cf 299 is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5', "correct $url";
b063a165 300 }
301
302 {
303 my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), ['♥']);
6adc45cf 304 is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5', "correct $url";
b063a165 305 }
dd096a3a 306}
307
308{
309 my $res = request "/root/stream_write";
00038a21 310
6adc45cf 311 is $res->code, 200, 'OK GET /root/stream_write';
dd096a3a 312 is decode_utf8($res->content), '<p>This is stream_write action ♥</p>', 'correct body';
4a64c27b 313 is $res->content_charset, 'UTF-8';
e5a5e80b 314}
0ca510f0 315
fe1dfeaf 316{
59e11cd7 317 my $res = request "/root/stream_body_fh";
318
319 is $res->code, 200, 'OK';
320 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
4a64c27b 321 is $res->content_charset, 'UTF-8';
59e11cd7 322 # Not sure why there is a trailing newline above... its not in catalyst code I can see. Not sure
323 # if is a problem or just an artifact of the why the test stuff works - JNAP
324}
325
326{
7b39dea1 327 my $res = request "/root/stream_write_fh";
fe1dfeaf 328
329 is $res->code, 200, 'OK';
330 is decode_utf8($res->content), '<p>This is stream_write_fh action ♥</p>', 'correct body';
8a79126d 331 #is $res->content_length, 41, 'correct length';
4a64c27b 332 is $res->content_charset, 'UTF-8';
fe1dfeaf 333}
dd096a3a 334
e8361cf8 335{
336 my $res = request "/root/stream_body_fh2";
337
338 is $res->code, 200, 'OK';
339 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
340 is $res->content_length, 41, 'correct length';
341 is $res->content_charset, 'UTF-8';
342}
343
12982f86 344{
345 ok my $path = File::Spec->catfile('t', 'utf8.txt');
346 ok my $req = POST '/root/file_upload',
347 Content_Type => 'form-data',
6adc45cf 348 Content => [encode_utf8('♥')=>encode_utf8('♥♥'), file=>["$path", encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8', ]];
12982f86 349
350 ok my $res = request $req;
351 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n";
352}
353
354{
355 ok my $req = POST '/root/json',
356 Content_Type => 'application/json',
357 Content => encode_json +{'♥'=>'♥♥'}; # Note: JSON does the UTF* encoding for us
358
359 ok my $res = request $req;
360
361 ## decode_json expect the binary utf8 string and does the decoded bit for us.
362 is_deeply decode_json(($res->content)), +{'♥'=>'♥♥'};
363}
364
6adc45cf 365{
69fa672d 366 ok my $res = request "/root/override_encoding";
367 ok my $enc = Encode::find_encoding('SHIFT_JIS');
368
369 is $res->code, 200, 'OK';
370 is $enc->decode($res->content), "テスト", 'correct body';
371 is $res->content_length, 6, 'correct length'; # Bytes over the wire
372 is length($enc->decode($res->content)), 3;
373 is $res->content_charset, 'SHIFT_JIS';
374}
375
376{
6adc45cf 377 my $res = request "/root/manual_1";
378
379 is $res->code, 200, 'OK';
380 is decode_utf8($res->content), "manual_1 ♥", 'correct body';
381 is $res->content_length, 12, 'correct length';
382 is $res->content_charset, 'UTF-8';
383}
384
385SKIP: {
386 eval { require Compress::Zlib; 1} || do {
387 skip "Compress::Zlib needed to test gzip encoding", 5 };
388
389 my $res = request "/root/gzipped";
390 ok my $raw_content = $res->content;
391 ok my $content = Compress::Zlib::memGunzip($raw_content), 'no gunzip error';
392
393 is $res->code, 200, 'OK';
394 is decode_utf8($content), "manual_1 ♥", 'correct body';
395 is $res->content_charset, 'UTF-8';
396}
397
12982f86 398## should we use binmode on filehandles to force the encoding...?
399## Not sure what else to do with multipart here, if docs are enough...
400
0ca510f0 401done_testing;