5 use HTTP::Request::Common;
7 # Test cases for incoming utf8
10 package MyApp::Controller::Root;
11 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
13 use base 'Catalyst::Controller';
15 sub heart :Path('♥') {
17 $c->response->content_type('text/html');
18 $c->response->body("<p>This is path-heart action ♥</p>");
19 # We let the content length middleware find the length...
24 $c->response->content_type('text/html');
25 $c->response->body("<p>This is path-hat action ^</p>");
28 sub uri_for :Path('uri_for') {
30 $c->response->content_type('text/html');
31 $c->response->body("${\$c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'})}");
34 sub heart_with_arg :Path('a♥') Args(1) {
35 my ($self, $c, $arg) = @_;
36 $c->response->content_type('text/html');
37 $c->response->body("<p>This is path-heart-arg action $arg</p>");
38 Test::More::is $c->req->args->[0], '♥';
41 sub base :Chained('/') CaptureArgs(0) { }
42 sub link :Chained('base') PathPart('♥') Args(0) {
44 $c->response->content_type('text/html');
45 $c->response->body("<p>This is base-link action ♥</p>");
47 sub arg :Chained('base') PathPart('♥') Args(1) {
48 my ($self, $c, $arg) = @_;
49 $c->response->content_type('text/html');
50 $c->response->body("<p>This is base-link action ♥ $arg</p>");
52 sub capture :Chained('base') PathPart('♥') CaptureArgs(1) {
53 my ($self, $c, $arg) = @_;
54 $c->stash(capture=>$arg);
56 sub argend :Chained('capture') PathPart('♥') Args(1) {
57 my ($self, $c, $arg) = @_;
58 $c->response->content_type('text/html');
60 Test::More::is $c->req->args->[0], '♥';
61 Test::More::is $c->req->captures->[0], '♥';
63 $c->response->body("<p>This is base-link action ♥ ${\$c->req->args->[0]}</p>");
69 MyApp->config(encoding=>'UTF-8');
71 Test::More::ok(MyApp->setup, 'setup app');
74 ok my $psgi = MyApp->psgi_app, 'build psgi app';
76 use Catalyst::Test 'MyApp';
77 use Encode 2.21 'decode_utf8', 'encode_utf8';
80 my $res = request "/root/♥";
82 is $res->code, 200, 'OK';
83 is decode_utf8($res->content), '<p>This is path-heart action ♥</p>', 'correct body';
84 is $res->content_length, 36, 'correct length';
88 my $res = request "/root/a♥/♥";
90 is $res->code, 200, 'OK';
91 is decode_utf8($res->content), '<p>This is path-heart-arg action ♥</p>', 'correct body';
92 is $res->content_length, 40, 'correct length';
96 my $res = request "/root/^";
98 is $res->code, 200, 'OK';
99 is decode_utf8($res->content), '<p>This is path-hat action ^</p>', 'correct body';
100 is $res->content_length, 32, 'correct length';
104 my $res = request "/base/♥";
106 is $res->code, 200, 'OK';
107 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
108 is $res->content_length, 35, 'correct length';
112 my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥'];
114 is $res->code, 200, 'OK';
115 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
116 is $res->content_length, 35, 'correct length';
117 is $c->req->parameters->{'♥'}[0], '♥';
118 is $c->req->query_parameters->{'♥'}[0], '♥';
119 is $c->req->body_parameters->{'♥'}[0], '♥';
120 is $c->req->parameters->{'♥'}[0], '♥';
121 is $c->req->parameters->{a}, 1;
122 is $c->req->body_parameters->{a}, 1;
126 my ($res, $c) = ctx_request GET "/base/♥?♥♥♥";
128 is $res->code, 200, 'OK';
129 is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
130 is $res->content_length, 35, 'correct length';
131 is $c->req->query_keywords, '♥♥♥';
135 my $res = request "/base/♥/♥";
137 is $res->code, 200, 'OK';
138 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
139 is $res->content_length, 39, 'correct length';
143 my $res = request "/base/♥/♥/♥/♥";
145 is $res->code, 200, 'OK';
146 is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
147 is $res->content_length, 39, 'correct length';
151 my ($res, $c) = ctx_request POST "/base/♥/♥/♥/♥?♥=♥♥", [a=>1, b=>'2', '♥'=>'♥♥'];
153 ## Make sure that the urls we generate work the same
154 my $uri_for = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
155 my $uri = $c->req->uri;
157 is "$uri", "$uri_for";
160 my ($res, $c) = ctx_request POST "$uri_for", [a=>1, b=>'2', '♥'=>'♥♥'];
161 is $c->req->query_parameters->{'♥'}, '♥♥';
162 is $c->req->body_parameters->{'♥'}, '♥♥';
163 is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body
168 my ($res, $c) = ctx_request "/root/uri_for";
169 my $url = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
171 is $res->code, 200, 'OK';
172 is decode_utf8($res->content), "$url", 'correct body'; #should do nothing
173 is $res->content, "$url", 'correct body';
174 is $res->content_length, 90, 'correct length';
176 # Test to make sure redirect can now take an object (sorry don't have a better place for it
177 # but wanted test coverage.
178 my $location = $c->res->redirect( $c->uri_for($c->controller('Root')->action_for('uri_for')) );