Commit | Line | Data |
0ca510f0 |
1 | use utf8; |
2 | use warnings; |
3 | use strict; |
4 | use Test::More; |
b9d96e27 |
5 | use HTTP::Request::Common; |
0ca510f0 |
6 | |
7 | # Test cases for incoming utf8 |
8 | |
9 | { |
10 | package MyApp::Controller::Root; |
11 | $INC{'MyApp/Controller/Root.pm'} = __FILE__; |
12 | |
13 | use base 'Catalyst::Controller'; |
14 | |
15 | sub heart :Path('♥') { |
16 | my ($self, $c) = @_; |
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... |
20 | } |
21 | |
22 | sub hat :Path('^') { |
23 | my ($self, $c) = @_; |
24 | $c->response->content_type('text/html'); |
25 | $c->response->body("<p>This is path-hat action ^</p>"); |
26 | } |
27 | |
e5a5e80b |
28 | sub uri_for :Path('uri_for') { |
29 | my ($self, $c) = @_; |
30 | $c->response->content_type('text/html'); |
31 | $c->response->body("${\$c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'})}"); |
32 | } |
33 | |
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], '♥'; |
39 | } |
40 | |
0ca510f0 |
41 | sub base :Chained('/') CaptureArgs(0) { } |
42 | sub link :Chained('base') PathPart('♥') Args(0) { |
43 | my ($self, $c) = @_; |
44 | $c->response->content_type('text/html'); |
45 | $c->response->body("<p>This is base-link action ♥</p>"); |
46 | } |
e5a5e80b |
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>"); |
51 | } |
52 | sub capture :Chained('base') PathPart('♥') CaptureArgs(1) { |
53 | my ($self, $c, $arg) = @_; |
54 | $c->stash(capture=>$arg); |
55 | } |
56 | sub argend :Chained('capture') PathPart('♥') Args(1) { |
57 | my ($self, $c, $arg) = @_; |
58 | $c->response->content_type('text/html'); |
59 | |
60 | Test::More::is $c->req->args->[0], '♥'; |
61 | Test::More::is $c->req->captures->[0], '♥'; |
62 | |
63 | $c->response->body("<p>This is base-link action ♥ ${\$c->req->args->[0]}</p>"); |
dd096a3a |
64 | |
65 | # Test to make sure redirect can now take an object (sorry don't have a better place for it |
66 | # but wanted test coverage. |
67 | my $location = $c->res->redirect( $c->uri_for($c->controller('Root')->action_for('uri_for')) ); |
68 | Test::More::ok !ref $location; |
e5a5e80b |
69 | } |
0ca510f0 |
70 | |
dd096a3a |
71 | sub stream_write :Local { |
72 | my ($self, $c) = @_; |
73 | $c->response->content_type('text/html'); |
74 | $c->response->write("<p>This is stream_write action ♥</p>"); |
75 | } |
76 | |
fe1dfeaf |
77 | sub stream_write_fh :Local { |
78 | my ($self, $c) = @_; |
79 | $c->response->content_type('text/html'); |
80 | |
81 | my $writer = $c->res->write_fh; |
82 | |
83 | $writer->write("<p>This is stream_write_fh action ♥</p>"); |
84 | $writer->close("<p>This is stream_write_fh action ♥</p>"); |
85 | } |
86 | |
0ca510f0 |
87 | package MyApp; |
88 | use Catalyst; |
89 | |
90 | MyApp->config(encoding=>'UTF-8'); |
91 | |
92 | Test::More::ok(MyApp->setup, 'setup app'); |
93 | } |
94 | |
95 | ok my $psgi = MyApp->psgi_app, 'build psgi app'; |
96 | |
97 | use Catalyst::Test 'MyApp'; |
b9d96e27 |
98 | use Encode 2.21 'decode_utf8', 'encode_utf8'; |
0ca510f0 |
99 | |
100 | { |
101 | my $res = request "/root/♥"; |
102 | |
103 | is $res->code, 200, 'OK'; |
104 | is decode_utf8($res->content), '<p>This is path-heart action ♥</p>', 'correct body'; |
105 | is $res->content_length, 36, 'correct length'; |
106 | } |
107 | |
108 | { |
e5a5e80b |
109 | my $res = request "/root/a♥/♥"; |
110 | |
111 | is $res->code, 200, 'OK'; |
112 | is decode_utf8($res->content), '<p>This is path-heart-arg action ♥</p>', 'correct body'; |
113 | is $res->content_length, 40, 'correct length'; |
114 | } |
115 | |
116 | { |
0ca510f0 |
117 | my $res = request "/root/^"; |
118 | |
119 | is $res->code, 200, 'OK'; |
120 | is decode_utf8($res->content), '<p>This is path-hat action ^</p>', 'correct body'; |
121 | is $res->content_length, 32, 'correct length'; |
122 | } |
123 | |
124 | { |
125 | my $res = request "/base/♥"; |
126 | |
127 | is $res->code, 200, 'OK'; |
128 | is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body'; |
129 | is $res->content_length, 35, 'correct length'; |
130 | } |
131 | |
132 | { |
b9d96e27 |
133 | my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥']; |
0ca510f0 |
134 | |
135 | is $res->code, 200, 'OK'; |
136 | is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body'; |
137 | is $res->content_length, 35, 'correct length'; |
b9d96e27 |
138 | is $c->req->parameters->{'♥'}[0], '♥'; |
139 | is $c->req->query_parameters->{'♥'}[0], '♥'; |
140 | is $c->req->body_parameters->{'♥'}[0], '♥'; |
141 | is $c->req->parameters->{'♥'}[0], '♥'; |
4a62800d |
142 | is $c->req->parameters->{a}, 1; |
143 | is $c->req->body_parameters->{a}, 1; |
e5a5e80b |
144 | } |
4a62800d |
145 | |
e5a5e80b |
146 | { |
147 | my ($res, $c) = ctx_request GET "/base/♥?♥♥♥"; |
4a62800d |
148 | |
e5a5e80b |
149 | is $res->code, 200, 'OK'; |
150 | is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body'; |
151 | is $res->content_length, 35, 'correct length'; |
152 | is $c->req->query_keywords, '♥♥♥'; |
0ca510f0 |
153 | } |
154 | |
e5a5e80b |
155 | { |
156 | my $res = request "/base/♥/♥"; |
157 | |
158 | is $res->code, 200, 'OK'; |
159 | is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body'; |
160 | is $res->content_length, 39, 'correct length'; |
161 | } |
b9d96e27 |
162 | |
e5a5e80b |
163 | { |
164 | my $res = request "/base/♥/♥/♥/♥"; |
165 | |
e5a5e80b |
166 | is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body'; |
167 | is $res->content_length, 39, 'correct length'; |
168 | } |
169 | |
170 | { |
171 | my ($res, $c) = ctx_request POST "/base/♥/♥/♥/♥?♥=♥♥", [a=>1, b=>'2', '♥'=>'♥♥']; |
172 | |
173 | ## Make sure that the urls we generate work the same |
174 | my $uri_for = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'}); |
175 | my $uri = $c->req->uri; |
176 | |
177 | is "$uri", "$uri_for"; |
178 | |
179 | { |
180 | my ($res, $c) = ctx_request POST "$uri_for", [a=>1, b=>'2', '♥'=>'♥♥']; |
181 | is $c->req->query_parameters->{'♥'}, '♥♥'; |
182 | is $c->req->body_parameters->{'♥'}, '♥♥'; |
183 | is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body |
184 | } |
185 | } |
186 | |
187 | { |
188 | my ($res, $c) = ctx_request "/root/uri_for"; |
189 | my $url = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'}); |
190 | |
191 | is $res->code, 200, 'OK'; |
192 | is decode_utf8($res->content), "$url", 'correct body'; #should do nothing |
193 | is $res->content, "$url", 'correct body'; |
194 | is $res->content_length, 90, 'correct length'; |
dd096a3a |
195 | } |
196 | |
197 | { |
198 | my $res = request "/root/stream_write"; |
00038a21 |
199 | |
dd096a3a |
200 | is $res->code, 200, 'OK'; |
201 | is decode_utf8($res->content), '<p>This is stream_write action ♥</p>', 'correct body'; |
e5a5e80b |
202 | } |
0ca510f0 |
203 | |
fe1dfeaf |
204 | { |
205 | my $res = request "/root/stream_write_fh"; |
206 | |
207 | is $res->code, 200, 'OK'; |
208 | is decode_utf8($res->content), '<p>This is stream_write_fh action ♥</p>', 'correct body'; |
209 | } |
dd096a3a |
210 | |
0ca510f0 |
211 | done_testing; |