5 use HTTP::Request::Common;
6 use Encode 2.21 'decode_utf8', 'encode_utf8';
10 # Test cases for incoming utf8
13 package MyApp::Controller::Root;
14 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
16 use base 'Catalyst::Controller';
18 sub heart :Path('♥') {
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...
27 $c->response->content_type('text/html');
28 $c->response->body("<p>This is path-hat action ^</p>");
31 sub uri_for :Path('uri_for') {
33 $c->response->content_type('text/html');
34 $c->response->body("${\$c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'})}");
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], '♥';
44 sub base :Chained('/') CaptureArgs(0) { }
45 sub link :Chained('base') PathPart('♥') Args(0) {
47 $c->response->content_type('text/html');
48 $c->response->body("<p>This is base-link action ♥</p>");
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>");
55 sub capture :Chained('base') PathPart('♥') CaptureArgs(1) {
56 my ($self, $c, $arg) = @_;
57 $c->stash(capture=>$arg);
59 sub argend :Chained('capture') PathPart('♥') Args(1) {
60 my ($self, $c, $arg) = @_;
61 $c->response->content_type('text/html');
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";
68 $c->response->body("<p>This is base-link action ♥ ${\$c->req->args->[0]}</p>");
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;
76 sub stream_write :Local {
78 $c->response->content_type('text/html');
79 $c->response->write("<p>This is stream_write action ♥</p>");
82 sub stream_write_fh :Local {
84 $c->response->content_type('text/html');
86 my $writer = $c->res->write_fh;
87 $writer->write_encoded('<p>This is stream_write_fh action ♥</p>');
91 # Stream a file with utf8 chars directly, you don't need to decode
92 sub stream_body_fh :Local {
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);
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 {
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> };
108 $c->response->content_type('text/html');
109 $c->response->body($contents);
112 sub file_upload :POST Consumes(Multipart) Local {
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';
118 my $text = $upload->slurp;
119 Test::More::is Encode::decode_utf8($text), "<p>This is stream_body_fh action ♥</p>\n";
121 my $decoded_text = $upload->decoded_slurp;
122 Test::More::is $decoded_text, "<p>This is stream_body_fh action ♥</p>\n";
124 Test::More::is $upload->filename, '♥ttachment.txt';
125 Test::More::is $upload->raw_basename, '♥ttachment.txt';
127 $c->response->content_type('text/html');
128 $c->response->body($decoded_text);
131 sub json :POST Consumes(JSON) Local {
133 my $post = $c->req->body_data;
135 Test::More::is $post->{'♥'}, '♥♥';
136 Test::More::is length($post->{'♥'}), 2;
137 $c->response->content_type('application/json');
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
142 $c->response->body(JSON::MaybeXS::encode_json($post));
145 ## If someone clears encoding, they can do as they wish
146 sub manual_1 :Local {
149 $c->res->content_type('text/plain');
150 $c->res->content_type_charset('UTF-8');
151 $c->response->body( Encode::encode_utf8("manual_1 ♥"));
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.
159 require Compress::Zlib;
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 ♥")));
167 sub override_encoding :Local {
169 $c->res->content_type('text/plain');
170 $c->encoding(Encode::find_encoding('UTF-8'));
171 $c->encoding(Encode::find_encoding('Shift_JIS'));
172 $c->response->body("テスト");
175 sub stream_write_error :Local {
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>");
186 Test::More::ok(MyApp->setup, 'setup app');
189 ok my $psgi = MyApp->psgi_app, 'build psgi app';
191 use Catalyst::Test 'MyApp';
194 my $res = request "/root/♥";
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';
199 is $res->content_charset, 'UTF-8';
203 my $res = request "/root/a♥/♥";
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';
208 is $res->content_charset, 'UTF-8';
212 my $res = request "/root/^";
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';
217 is $res->content_charset, 'UTF-8';
221 my $res = request "/base/♥";
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 $res->content_charset, 'UTF-8';
230 my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥'];
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';
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], '♥';
239 is $c->req->parameters->{a}, 1;
240 is $c->req->body_parameters->{a}, 1;
241 is $res->content_charset, 'UTF-8';
245 my ($res, $c) = ctx_request GET "/base/♥?♥♥♥";
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, '♥♥♥';
251 is $res->content_charset, 'UTF-8';
255 my $res = request "/base/♥/♥";
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';
260 is $res->content_charset, 'UTF-8';
264 my $res = request "/base/♥/♥/♥/♥";
266 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
267 is $res->content_length, 39, 'correct length';
268 is $res->content_charset, 'UTF-8';
272 my ($res, $c) = ctx_request POST "/base/♥/♥/♥/♥?♥=♥♥", [a=>1, b=>'2', '♥'=>'♥♥'];
274 ## Make sure that the urls we generate work the same
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'), ['♥', '♥'], {'♥'=>'♥♥'});
277 my $uri = $c->req->uri;
279 is "$uri_for1", "$uri_for2";
280 is "$uri", "$uri_for1";
283 my ($res, $c) = ctx_request POST "$uri_for1", [a=>1, b=>'2', '♥'=>'♥♥'];
284 is $c->req->query_parameters->{'♥'}, '♥♥';
285 is $c->req->body_parameters->{'♥'}, '♥♥';
286 is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body
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;
292 is $res->content_charset, 'UTF-8';
297 my ($res, $c) = ctx_request "/root/uri_for";
298 my $url = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
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';
304 is $res->content_charset, 'UTF-8';
307 my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), '♥');
308 is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5', "correct $url";
312 my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), ['♥']);
313 is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5', "correct $url";
318 my $res = request "/root/stream_write";
320 is $res->code, 200, 'OK GET /root/stream_write';
321 is decode_utf8($res->content), '<p>This is stream_write action ♥</p>', 'correct body';
322 is $res->content_charset, 'UTF-8';
326 my $res = request "/root/stream_body_fh";
328 is $res->code, 200, 'OK';
329 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
330 is $res->content_charset, 'UTF-8';
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
336 my $res = request "/root/stream_write_fh";
338 is $res->code, 200, 'OK';
339 is decode_utf8($res->content), '<p>This is stream_write_fh action ♥</p>', 'correct body';
340 #is $res->content_length, 41, 'correct length';
341 is $res->content_charset, 'UTF-8';
345 my $res = request "/root/stream_body_fh2";
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';
354 ok my $path = File::Spec->catfile('t', 'utf8.txt');
355 ok my $req = POST '/root/file_upload',
356 Content_Type => 'form-data',
357 Content => [encode_utf8('♥')=>encode_utf8('♥♥'), file=>["$path", encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8', ]];
359 ok my $res = request $req;
360 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n";
364 ok my $req = POST '/root/json',
365 Content_Type => 'application/json',
366 Content => encode_json +{'♥'=>'♥♥'}; # Note: JSON does the UTF* encoding for us
368 ok my $res = request $req;
370 ## decode_json expect the binary utf8 string and does the decoded bit for us.
371 is_deeply decode_json(($res->content)), +{'♥'=>'♥♥'};
375 ok my $res = request "/root/override_encoding";
376 ok my $enc = Encode::find_encoding('SHIFT_JIS');
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';
386 my $res = request "/root/manual_1";
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';
395 eval { require Compress::Zlib; 1} || do {
396 skip "Compress::Zlib needed to test gzip encoding", 5 };
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';
402 is $res->code, 200, 'OK';
403 is decode_utf8($content), "manual_1 ♥", 'correct body';
404 is $res->content_charset, 'UTF-8';
408 my $res = request "/root/stream_write_error";
410 is $res->code, 200, 'OK';
411 like decode_utf8($res->content), qr[<p>This is stream_write action ♥</p><!DOCTYPE html], 'correct body';
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...