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