373a4286c89358c0ecca0e5158b63cd121681526
[catagits/Catalyst-Runtime.git] / t / utf_incoming.t
1 use utf8;
2 use warnings;
3 use strict;
4 use Test::More;
5 use HTTP::Request::Common;
6 use Encode 2.21 'decode_utf8', 'encode_utf8';
7 use File::Spec;
8 use JSON::MaybeXS;
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
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
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     }
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], '♥';
65         Test::More::is $arg, '♥';
66         Test::More::is length($arg), 1, "got length of one";
67
68         $c->response->body("<p>This is base-link action ♥ ${\$c->req->args->[0]}</p>");
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; 
74       }
75
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
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;
87     $writer->write_encoded('<p>This is stream_write_fh action ♥</p>');
88     $writer->close;
89   }
90
91   # Stream a file with utf8 chars directly, you don't need to decode
92   sub stream_body_fh :Local {
93     my ($self, $c) = @_;
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);
98   }
99
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
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};
116     Test::More::is $upload->charset, 'UTF-8';
117
118     my $text = $upload->slurp;
119     Test::More::is Encode::decode_utf8($text), "<p>This is stream_body_fh action ♥</p>\n";
120
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
127     $c->response->content_type('text/html');
128     $c->response->body($decoded_text);
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->{'♥'}, '♥♥';
136     Test::More::is length($post->{'♥'}), 2;
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   }
144
145   ## If someone clears encoding, they can do as they wish
146   sub manual_1 :Local {
147     my ($self, $c) = @_;
148     $c->clear_encoding;
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
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
174   package MyApp;
175   use Catalyst;
176
177   Test::More::ok(MyApp->setup, 'setup app');
178 }
179
180 ok my $psgi = MyApp->psgi_app, 'build psgi app';
181
182 use Catalyst::Test 'MyApp';
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';
190   is $res->content_charset, 'UTF-8';
191 }
192
193 {
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';
199   is $res->content_charset, 'UTF-8';
200 }
201
202 {
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';
208   is $res->content_charset, 'UTF-8';
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';
217   is $res->content_charset, 'UTF-8';
218 }
219
220 {
221   my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥'];
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';
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], '♥';
230   is $c->req->parameters->{a}, 1;
231   is $c->req->body_parameters->{a}, 1;
232   is $res->content_charset, 'UTF-8';
233 }
234
235 {
236   my ($res, $c) = ctx_request GET "/base/♥?♥♥♥";
237
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, '♥♥♥';
242   is $res->content_charset, 'UTF-8';
243 }
244
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';
251   is $res->content_charset, 'UTF-8';
252 }
253
254 {
255   my $res = request "/base/♥/♥/♥/♥";
256
257   is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
258   is $res->content_length, 39, 'correct length';
259   is $res->content_charset, 'UTF-8';
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
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'), ['♥', '♥'], {'♥'=>'♥♥'});
268   my $uri = $c->req->uri;
269
270   is "$uri_for1", "$uri_for2";
271   is "$uri", "$uri_for1";
272
273   {
274     my ($res, $c) = ctx_request POST "$uri_for1", [a=>1, b=>'2', '♥'=>'♥♥'];
275     is $c->req->query_parameters->{'♥'}, '♥♥';
276     is $c->req->body_parameters->{'♥'}, '♥♥';
277     is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body
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;
283     is $res->content_charset, 'UTF-8';
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';
295   is $res->content_charset, 'UTF-8';
296
297   {
298     my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), '♥');
299     is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5', "correct $url";
300   }
301
302   {
303     my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), ['♥']);
304     is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5', "correct $url";
305   }
306 }
307
308 {
309   my $res = request "/root/stream_write";
310
311   is $res->code, 200, 'OK GET /root/stream_write';
312   is decode_utf8($res->content), '<p>This is stream_write action ♥</p>', 'correct body';
313   is $res->content_charset, 'UTF-8';
314 }
315
316 {
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';
321   is $res->content_charset, 'UTF-8';
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 {
327   my $res = request "/root/stream_write_fh";
328
329   is $res->code, 200, 'OK';
330   is decode_utf8($res->content), '<p>This is stream_write_fh action ♥</p>', 'correct body';
331   #is $res->content_length, 41, 'correct length';
332   is $res->content_charset, 'UTF-8';
333 }
334
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
344 {
345   ok my $path = File::Spec->catfile('t', 'utf8.txt');
346   ok my $req = POST '/root/file_upload',
347     Content_Type => 'form-data',
348     Content =>  [encode_utf8('♥')=>encode_utf8('♥♥'), file=>["$path", encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8', ]];
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
365 {
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 {
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
385 SKIP: {
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
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
401 done_testing;