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], '♥';
66 $c->response->body("<p>This is base-link action ♥ ${\$c->req->args->[0]}</p>");
68 # Test to make sure redirect can now take an object (sorry don't have a better place for it
69 # but wanted test coverage.
70 my $location = $c->res->redirect( $c->uri_for($c->controller('Root')->action_for('uri_for')) );
71 Test::More::ok !ref $location;
74 sub stream_write :Local {
76 $c->response->content_type('text/html');
77 $c->response->write("<p>This is stream_write action ♥</p>");
80 sub stream_write_fh :Local {
82 $c->response->content_type('text/html');
84 my $writer = $c->res->write_fh;
85 $writer->write_encoded('<p>This is stream_write_fh action ♥</p>');
89 # Stream a file with utf8 chars directly, you don't need to decode
90 sub stream_body_fh :Local {
92 my $path = File::Spec->catfile('t', 'utf8.txt');
93 open(my $fh, '<', $path) || die "trouble: $!";
94 $c->response->content_type('text/html');
95 $c->response->body($fh);
98 # If you pull the file contents into a var, NOW you need to specify the
99 # IO encoding on the FH. Ultimately Plack at the end wants bytes...
100 sub stream_body_fh2 :Local {
102 my $path = File::Spec->catfile('t', 'utf8.txt');
103 open(my $fh, '<:encoding(UTF-8)', $path) || die "trouble: $!";
104 my $contents = do { local $/; <$fh> };
106 $c->response->content_type('text/html');
107 $c->response->body($contents);
110 sub file_upload :POST Consumes(Multipart) Local {
112 Test::More::is $c->req->body_parameters->{'♥'}, '♥♥';
113 Test::More::ok my $upload = $c->req->uploads->{file};
115 my $text = $upload->slurp;
116 Test::More::is Encode::decode_utf8($text), "<p>This is stream_body_fh action ♥</p>\n";
118 $c->response->content_type('text/html');
119 $c->response->body($upload->fh);
122 sub json :POST Consumes(JSON) Local {
124 my $post = $c->req->body_data;
126 Test::More::is $post->{'♥'}, '♥♥';
127 $c->response->content_type('application/json');
129 # Encode JSON also encodes to a UTF-8 encoded, binary string. This is why we don't
130 # have application/json as one of the things we match, otherwise we get double
132 $c->response->body(JSON::MaybeXS::encode_json($post));
138 # Default encoding is now UTF-8
139 # MyApp->config(encoding=>'UTF-8');
141 Test::More::ok(MyApp->setup, 'setup app');
144 ok my $psgi = MyApp->psgi_app, 'build psgi app';
146 use Catalyst::Test 'MyApp';
149 my $res = request "/root/♥";
151 is $res->code, 200, 'OK';
152 is decode_utf8($res->content), '<p>This is path-heart action ♥</p>', 'correct body';
153 is $res->content_length, 36, 'correct length';
154 is $res->content_charset, 'UTF-8';
158 my $res = request "/root/a♥/♥";
160 is $res->code, 200, 'OK';
161 is decode_utf8($res->content), '<p>This is path-heart-arg action ♥</p>', 'correct body';
162 is $res->content_length, 40, 'correct length';
163 is $res->content_charset, 'UTF-8';
167 my $res = request "/root/^";
169 is $res->code, 200, 'OK';
170 is decode_utf8($res->content), '<p>This is path-hat action ^</p>', 'correct body';
171 is $res->content_length, 32, 'correct length';
172 is $res->content_charset, 'UTF-8';
176 my $res = request "/base/♥";
178 is $res->code, 200, 'OK';
179 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
180 is $res->content_length, 35, 'correct length';
181 is $res->content_charset, 'UTF-8';
185 my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥'];
187 is $res->code, 200, 'OK';
188 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
189 is $res->content_length, 35, 'correct length';
190 is $c->req->parameters->{'♥'}[0], '♥';
191 is $c->req->query_parameters->{'♥'}[0], '♥';
192 is $c->req->body_parameters->{'♥'}[0], '♥';
193 is $c->req->parameters->{'♥'}[0], '♥';
194 is $c->req->parameters->{a}, 1;
195 is $c->req->body_parameters->{a}, 1;
196 is $res->content_charset, 'UTF-8';
200 my ($res, $c) = ctx_request GET "/base/♥?♥♥♥";
202 is $res->code, 200, 'OK';
203 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
204 is $res->content_length, 35, 'correct length';
205 is $c->req->query_keywords, '♥♥♥';
206 is $res->content_charset, 'UTF-8';
210 my $res = request "/base/♥/♥";
212 is $res->code, 200, 'OK';
213 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
214 is $res->content_length, 39, 'correct length';
215 is $res->content_charset, 'UTF-8';
219 my $res = request "/base/♥/♥/♥/♥";
221 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
222 is $res->content_length, 39, 'correct length';
223 is $res->content_charset, 'UTF-8';
227 my ($res, $c) = ctx_request POST "/base/♥/♥/♥/♥?♥=♥♥", [a=>1, b=>'2', '♥'=>'♥♥'];
229 ## Make sure that the urls we generate work the same
230 my $uri_for1 = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
231 my $uri_for2 = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥', '♥'], {'♥'=>'♥♥'});
232 my $uri = $c->req->uri;
234 is "$uri_for1", "$uri_for2";
235 is "$uri", "$uri_for1";
238 my ($res, $c) = ctx_request POST "$uri_for1", [a=>1, b=>'2', '♥'=>'♥♥'];
239 is $c->req->query_parameters->{'♥'}, '♥♥';
240 is $c->req->body_parameters->{'♥'}, '♥♥';
241 is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body
242 is $res->content_charset, 'UTF-8';
247 my ($res, $c) = ctx_request "/root/uri_for";
248 my $url = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
250 is $res->code, 200, 'OK';
251 is decode_utf8($res->content), "$url", 'correct body'; #should do nothing
252 is $res->content, "$url", 'correct body';
253 is $res->content_length, 90, 'correct length';
254 is $res->content_charset, 'UTF-8';
257 my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), '♥');
258 is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5';
262 my $url = $c->uri_for($c->controller->action_for('heart_with_arg'), ['♥']);
263 is "$url", 'http://localhost/root/a%E2%99%A5/%E2%99%A5';
268 my $res = request "/root/stream_write";
270 is $res->code, 200, 'OK';
271 is decode_utf8($res->content), '<p>This is stream_write action ♥</p>', 'correct body';
272 is $res->content_charset, 'UTF-8';
276 my $res = request "/root/stream_body_fh";
278 is $res->code, 200, 'OK';
279 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
280 is $res->content_charset, 'UTF-8';
281 # Not sure why there is a trailing newline above... its not in catalyst code I can see. Not sure
282 # if is a problem or just an artifact of the why the test stuff works - JNAP
286 my $res = request "/root/stream_write_fh";
288 is $res->code, 200, 'OK';
289 is decode_utf8($res->content), '<p>This is stream_write_fh action ♥</p>', 'correct body';
290 #is $res->content_length, 41, 'correct length';
291 is $res->content_charset, 'UTF-8';
295 my $res = request "/root/stream_body_fh2";
297 is $res->code, 200, 'OK';
298 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
299 is $res->content_length, 41, 'correct length';
300 is $res->content_charset, 'UTF-8';
304 ok my $path = File::Spec->catfile('t', 'utf8.txt');
305 ok my $req = POST '/root/file_upload',
306 Content_Type => 'form-data',
307 Content => [encode_utf8('♥')=>encode_utf8('♥♥'), file=>["$path", 'attachment.txt', 'Content-Type' =>'text/html; charset=UTF-8', ]];
309 ok my $res = request $req;
310 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n";
314 ok my $req = POST '/root/json',
315 Content_Type => 'application/json',
316 Content => encode_json +{'♥'=>'♥♥'}; # Note: JSON does the UTF* encoding for us
318 ok my $res = request $req;
320 ## decode_json expect the binary utf8 string and does the decoded bit for us.
321 is_deeply decode_json(($res->content)), +{'♥'=>'♥♥'};
324 ## should we use binmode on filehandles to force the encoding...?
325 ## Not sure what else to do with multipart here, if docs are enough...