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;
85 $writer->write(Encode::encode_utf8('<p>This is stream_write_fh action ♥</p>'));
89 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);
101 # Default encoding is now UTF-8
102 # MyApp->config(encoding=>'UTF-8');
104 Test::More::ok(MyApp->setup, 'setup app');
107 ok my $psgi = MyApp->psgi_app, 'build psgi app';
109 use Catalyst::Test 'MyApp';
112 my $res = request "/root/♥";
114 is $res->code, 200, 'OK';
115 is decode_utf8($res->content), '<p>This is path-heart action ♥</p>', 'correct body';
116 is $res->content_length, 36, 'correct length';
120 my $res = request "/root/a♥/♥";
122 is $res->code, 200, 'OK';
123 is decode_utf8($res->content), '<p>This is path-heart-arg action ♥</p>', 'correct body';
124 is $res->content_length, 40, 'correct length';
128 my $res = request "/root/^";
130 is $res->code, 200, 'OK';
131 is decode_utf8($res->content), '<p>This is path-hat action ^</p>', 'correct body';
132 is $res->content_length, 32, 'correct length';
136 my $res = request "/base/♥";
138 is $res->code, 200, 'OK';
139 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
140 is $res->content_length, 35, 'correct length';
144 my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥'];
146 is $res->code, 200, 'OK';
147 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
148 is $res->content_length, 35, 'correct length';
149 is $c->req->parameters->{'♥'}[0], '♥';
150 is $c->req->query_parameters->{'♥'}[0], '♥';
151 is $c->req->body_parameters->{'♥'}[0], '♥';
152 is $c->req->parameters->{'♥'}[0], '♥';
153 is $c->req->parameters->{a}, 1;
154 is $c->req->body_parameters->{a}, 1;
158 my ($res, $c) = ctx_request GET "/base/♥?♥♥♥";
160 is $res->code, 200, 'OK';
161 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
162 is $res->content_length, 35, 'correct length';
163 is $c->req->query_keywords, '♥♥♥';
167 my $res = request "/base/♥/♥";
169 is $res->code, 200, 'OK';
170 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
171 is $res->content_length, 39, 'correct length';
175 my $res = request "/base/♥/♥/♥/♥";
177 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
178 is $res->content_length, 39, 'correct length';
182 my ($res, $c) = ctx_request POST "/base/♥/♥/♥/♥?♥=♥♥", [a=>1, b=>'2', '♥'=>'♥♥'];
184 ## Make sure that the urls we generate work the same
185 my $uri_for = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
186 my $uri = $c->req->uri;
188 is "$uri", "$uri_for";
191 my ($res, $c) = ctx_request POST "$uri_for", [a=>1, b=>'2', '♥'=>'♥♥'];
192 is $c->req->query_parameters->{'♥'}, '♥♥';
193 is $c->req->body_parameters->{'♥'}, '♥♥';
194 is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body
199 my ($res, $c) = ctx_request "/root/uri_for";
200 my $url = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
202 is $res->code, 200, 'OK';
203 is decode_utf8($res->content), "$url", 'correct body'; #should do nothing
204 is $res->content, "$url", 'correct body';
205 is $res->content_length, 90, 'correct length';
209 my $res = request "/root/stream_write";
211 is $res->code, 200, 'OK';
212 is decode_utf8($res->content), '<p>This is stream_write action ♥</p>', 'correct body';
216 my $res = request "/root/stream_body_fh";
218 is $res->code, 200, 'OK';
219 is decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n", 'correct body';
220 # Not sure why there is a trailing newline above... its not in catalyst code I can see. Not sure
221 # if is a problem or just an artifact of the why the test stuff works - JNAP
225 my $res = request "/root/stream_write_fh";
227 is $res->code, 200, 'OK';
228 is decode_utf8($res->content), '<p>This is stream_write_fh action ♥</p>', 'correct body';