5 use HTTP::Request::Common;
6 use Encode 2.21 'decode_utf8', 'encode_utf8';
9 # Test cases for incoming utf8
12 package MyApp::Controller::Root;
13 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
15 use base 'Catalyst::Controller';
17 sub heart :Path('♥') {
19 $c->response->content_type('text/html');
20 $c->response->body("<p>This is path-heart action ♥</p>");
21 # We let the content length middleware find the length...
26 $c->response->content_type('text/html');
27 $c->response->body("<p>This is path-hat action ^</p>");
30 sub uri_for :Path('uri_for') {
32 $c->response->content_type('text/html');
33 $c->response->body("${\$c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'})}");
36 sub heart_with_arg :Path('a♥') Args(1) {
37 my ($self, $c, $arg) = @_;
38 $c->response->content_type('text/html');
39 $c->response->body("<p>This is path-heart-arg action $arg</p>");
40 Test::More::is $c->req->args->[0], '♥';
43 sub base :Chained('/') CaptureArgs(0) { }
44 sub link :Chained('base') PathPart('♥') Args(0) {
46 $c->response->content_type('text/html');
47 $c->response->body("<p>This is base-link action ♥</p>");
49 sub arg :Chained('base') PathPart('♥') Args(1) {
50 my ($self, $c, $arg) = @_;
51 $c->response->content_type('text/html');
52 $c->response->body("<p>This is base-link action ♥ $arg</p>");
54 sub capture :Chained('base') PathPart('♥') CaptureArgs(1) {
55 my ($self, $c, $arg) = @_;
56 $c->stash(capture=>$arg);
58 sub argend :Chained('capture') PathPart('♥') Args(1) {
59 my ($self, $c, $arg) = @_;
60 $c->response->content_type('text/html');
62 Test::More::is $c->req->args->[0], '♥';
63 Test::More::is $c->req->captures->[0], '♥';
65 $c->response->body("<p>This is base-link action ♥ ${\$c->req->args->[0]}</p>");
67 # Test to make sure redirect can now take an object (sorry don't have a better place for it
68 # but wanted test coverage.
69 my $location = $c->res->redirect( $c->uri_for($c->controller('Root')->action_for('uri_for')) );
70 Test::More::ok !ref $location;
73 sub stream_write :Local {
75 $c->response->content_type('text/html');
76 $c->response->write("<p>This is stream_write action ♥</p>");
79 sub stream_write_fh :Local {
81 $c->response->content_type('text/html');
83 my $writer = $c->res->write_fh;
84 $writer->write_encoded('<p>This is stream_write_fh action ♥</p>');
88 # Stream a file with utf8 chars directly, you don't need to decode
89 sub stream_body_fh :Local {
91 my $path = File::Spec->catfile('t', 'utf8.txt');
92 open(my $fh, '<', $path) || die "trouble: $!";
93 $c->response->content_type('text/html');
94 $c->response->body($fh);
97 # If you pull the file contents into a var, NOW you need to specify the
98 # IO encoding on the FH. Ultimately Plack at the end wants bytes...
99 sub stream_body_fh2 :Local {
101 my $path = File::Spec->catfile('t', 'utf8.txt');
102 open(my $fh, '<:encoding(UTF-8)', $path) || die "trouble: $!";
103 my $contents = do { local $/; <$fh> };
105 $c->response->content_type('text/html');
106 $c->response->body($contents);
113 # Default encoding is now UTF-8
114 # MyApp->config(encoding=>'UTF-8');
116 Test::More::ok(MyApp->setup, 'setup app');
119 ok my $psgi = MyApp->psgi_app, 'build psgi app';
121 use Catalyst::Test 'MyApp';
124 my $res = request "/root/♥";
126 is $res->code, 200, 'OK';
127 is decode_utf8($res->content), '<p>This is path-heart action ♥</p>', 'correct body';
128 is $res->content_length, 36, 'correct length';
129 is $res->content_charset, 'UTF-8';
133 my $res = request "/root/a♥/♥";
135 is $res->code, 200, 'OK';
136 is decode_utf8($res->content), '<p>This is path-heart-arg action ♥</p>', 'correct body';
137 is $res->content_length, 40, 'correct length';
138 is $res->content_charset, 'UTF-8';
142 my $res = request "/root/^";
144 is $res->code, 200, 'OK';
145 is decode_utf8($res->content), '<p>This is path-hat action ^</p>', 'correct body';
146 is $res->content_length, 32, 'correct length';
147 is $res->content_charset, 'UTF-8';
151 my $res = request "/base/♥";
153 is $res->code, 200, 'OK';
154 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
155 is $res->content_length, 35, 'correct length';
156 is $res->content_charset, 'UTF-8';
160 my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥'];
162 is $res->code, 200, 'OK';
163 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
164 is $res->content_length, 35, 'correct length';
165 is $c->req->parameters->{'♥'}[0], '♥';
166 is $c->req->query_parameters->{'♥'}[0], '♥';
167 is $c->req->body_parameters->{'♥'}[0], '♥';
168 is $c->req->parameters->{'♥'}[0], '♥';
169 is $c->req->parameters->{a}, 1;
170 is $c->req->body_parameters->{a}, 1;
171 is $res->content_charset, 'UTF-8';
175 my ($res, $c) = ctx_request GET "/base/♥?♥♥♥";
177 is $res->code, 200, 'OK';
178 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
179 is $res->content_length, 35, 'correct length';
180 is $c->req->query_keywords, '♥♥♥';
181 is $res->content_charset, 'UTF-8';
185 my $res = request "/base/♥/♥";
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, 39, 'correct length';
190 is $res->content_charset, 'UTF-8';
194 my $res = request "/base/♥/♥/♥/♥";
196 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
197 is $res->content_length, 39, 'correct length';
198 is $res->content_charset, 'UTF-8';
202 my ($res, $c) = ctx_request POST "/base/♥/♥/♥/♥?♥=♥♥", [a=>1, b=>'2', '♥'=>'♥♥'];
204 ## Make sure that the urls we generate work the same
205 my $uri_for = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
206 my $uri = $c->req->uri;
208 is "$uri", "$uri_for";
211 my ($res, $c) = ctx_request POST "$uri_for", [a=>1, b=>'2', '♥'=>'♥♥'];
212 is $c->req->query_parameters->{'♥'}, '♥♥';
213 is $c->req->body_parameters->{'♥'}, '♥♥';
214 is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body
215 is $res->content_charset, 'UTF-8';
220 my ($res, $c) = ctx_request "/root/uri_for";
221 my $url = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
223 is $res->code, 200, 'OK';
224 is decode_utf8($res->content), "$url", 'correct body'; #should do nothing
225 is $res->content, "$url", 'correct body';
226 is $res->content_length, 90, 'correct length';
227 is $res->content_charset, 'UTF-8';
231 my $res = request "/root/stream_write";
233 is $res->code, 200, 'OK';
234 is decode_utf8($res->content), '<p>This is stream_write action ♥</p>', 'correct body';
235 is $res->content_charset, 'UTF-8';
239 my $res = request "/root/stream_body_fh";
241 is $res->code, 200, 'OK';
242 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
243 is $res->content_charset, 'UTF-8';
244 # Not sure why there is a trailing newline above... its not in catalyst code I can see. Not sure
245 # if is a problem or just an artifact of the why the test stuff works - JNAP
249 my $res = request "/root/stream_write_fh";
251 is $res->code, 200, 'OK';
252 is decode_utf8($res->content), '<p>This is stream_write_fh action ♥</p>', 'correct body';
253 #is $res->content_length, 41, 'correct length';
254 is $res->content_charset, 'UTF-8';
258 my $res = request "/root/stream_body_fh2";
260 is $res->code, 200, 'OK';
261 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
262 is $res->content_length, 41, 'correct length';
263 is $res->content_charset, 'UTF-8';