Commit | Line | Data |
9c7b6768 |
1 | use warnings; |
2 | use strict; |
3 | |
4 | # Make it easier to mount PSGI apps under catalyst |
5 | |
b5436c19 |
6 | my $psgi_app = sub { |
7 | my $req = Plack::Request->new(shift); |
8 | return [200,[],[$req->path]]; |
9 | }; |
10 | |
9c7b6768 |
11 | { |
b194746d |
12 | package MyApp::PSGIObject; |
13 | |
14 | sub as_psgi { |
15 | return [200, ['Content-Type' => 'text/plain'], ['as_psgi']]; |
16 | }; |
17 | |
4477b313 |
18 | package MyApp::Controller::Docs; |
09b86ef3 |
19 | $INC{'MyApp/Controller/Docs.pm'} = __FILE__; |
9c7b6768 |
20 | |
21 | use base 'Catalyst::Controller'; |
22 | use Plack::Request; |
23 | use Catalyst::Utils; |
24 | |
b194746d |
25 | sub as_psgi :Local { |
26 | my ($self, $c) = @_; |
27 | my $as_psgi = bless +{}, 'MyApp::PSGIObject'; |
28 | $c->res->from_psgi_response($as_psgi); |
29 | } |
30 | |
4477b313 |
31 | sub name :Local { |
32 | my ($self, $c) = @_; |
33 | my $env = $c->Catalyst::Utils::env_at_action; |
34 | $c->res->from_psgi_response( |
35 | $psgi_app->($env)); |
36 | |
37 | } |
38 | |
39 | sub name_args :Local Args(1) { |
40 | my ($self, $c, $arg) = @_; |
41 | my $env = $c->Catalyst::Utils::env_at_action; |
42 | $c->res->from_psgi_response( |
43 | $psgi_app->($env)); |
44 | } |
45 | |
67fd25bc |
46 | sub filehandle :Local { |
47 | my ($self, $c, $arg) = @_; |
48 | my $path = File::Spec->catfile('t', 'utf8.txt'); |
49 | open(my $fh, '<', $path) || die "trouble: $!"; |
50 | $c->res->from_psgi_response([200, ['Content-Type'=>'text/html'], $fh]); |
51 | } |
52 | |
53 | sub direct :Local { |
54 | my ($self, $c, $arg) = @_; |
55 | $c->res->from_psgi_response([200, ['Content-Type'=>'text/html'], ["hello","world"]]); |
56 | } |
57 | |
9c7b6768 |
58 | package MyApp::Controller::User; |
09b86ef3 |
59 | $INC{'MyApp/Controller/User.pm'} = __FILE__; |
9c7b6768 |
60 | |
61 | use base 'Catalyst::Controller'; |
62 | use Plack::Request; |
63 | use Catalyst::Utils; |
9c7b6768 |
64 | |
65 | sub local_example :Local { |
66 | my ($self, $c) = @_; |
67 | my $env = $self->get_env($c); |
68 | $c->res->from_psgi_response( |
69 | $psgi_app->($env)); |
70 | } |
71 | |
72 | sub local_example_args1 :Local Args(1) { |
73 | my ($self, $c) = @_; |
74 | my $env = $self->get_env($c); |
75 | $c->res->from_psgi_response( |
76 | $psgi_app->($env)); |
77 | } |
78 | |
79 | sub path_example :Path('path-example') { |
80 | my ($self, $c) = @_; |
81 | my $env = $self->get_env($c); |
82 | $c->res->from_psgi_response( |
83 | $psgi_app->($env)); |
84 | } |
85 | |
86 | sub path_example_args1 :Path('path-example-args1') { |
87 | my ($self, $c) = @_; |
88 | my $env = $self->get_env($c); |
89 | $c->res->from_psgi_response( |
90 | $psgi_app->($env)); |
91 | } |
92 | |
93 | sub chained :Chained(/) PathPrefix CaptureArgs(0) { } |
94 | |
95 | sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {} |
96 | |
97 | sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1) |
98 | { |
99 | my ($self, $c) = @_; |
100 | my $env = $self->get_env($c); |
101 | $c->res->from_psgi_response( |
102 | $psgi_app->($env)); |
103 | } |
d12acbe6 |
104 | |
105 | sub mounted :Local Args(1) { |
106 | my ($self, $c, $arg) = @_; |
107 | our $app ||= ref($c)->psgi_app; |
108 | my $env = $self->get_env($c); |
109 | $c->res->from_psgi_response( |
110 | $app->($env)); |
111 | } |
112 | |
113 | sub mount_arg :Path(/mounted) Arg(1) { |
114 | my ($self, $c, $arg) = @_; |
115 | my $uri = $c->uri_for( $self->action_for('local_example_args1'),$arg); |
116 | $c->res->body("$uri"); |
117 | } |
118 | |
119 | sub mount_noarg :Path(/mounted_no_arg) { |
120 | my ($self, $c) = @_; |
121 | my $uri = $c->uri_for( $self->action_for('local_example_args1'),444); |
122 | $c->res->body("$uri"); |
123 | } |
124 | |
9c7b6768 |
125 | |
126 | sub get_env { |
127 | my ($self, $c) = @_; |
128 | if($c->req->query_parameters->{path_prefix}) { |
129 | return $c->Catalyst::Utils::env_at_path_prefix; |
130 | } elsif($c->req->query_parameters->{env_path}) { |
131 | return $c->Catalyst::Utils::env_at_action; |
132 | } elsif($c->req->query_parameters->{path}) { |
133 | return $c->Catalyst::Utils::env_at_request_uri; |
134 | } else { |
135 | return $c->req->env; |
136 | } |
137 | } |
138 | |
9c7b6768 |
139 | package MyApp; |
140 | use Catalyst; |
141 | MyApp->setup; |
142 | |
143 | } |
144 | |
145 | use Test::More; |
146 | use Catalyst::Test 'MyApp'; |
147 | |
d12acbe6 |
148 | { |
b194746d |
149 | my ($res, $c) = ctx_request('/docs/as_psgi'); |
150 | is $res->content, 'as_psgi'; |
151 | } |
152 | |
153 | { |
d12acbe6 |
154 | my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1'); |
155 | is $c->action, 'user/mounted'; |
156 | is $res->content, 'http://localhost/user/user/local_example_args1/111'; |
157 | is_deeply $c->req->args, [111]; |
158 | } |
159 | |
160 | { |
161 | my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1'); |
162 | is $c->action, 'user/mounted'; |
163 | is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444'; |
164 | is_deeply $c->req->args, ['mounted_no_arg']; |
165 | } |
166 | |
9c7b6768 |
167 | # BEGIN [user/local_example] |
168 | { |
169 | my ($res, $c) = ctx_request('/user/local_example'); |
170 | is $c->action, 'user/local_example'; |
171 | is $res->content, '/user/local_example'; |
172 | is_deeply $c->req->args, []; |
173 | } |
174 | |
175 | { |
176 | my ($res, $c) = ctx_request('/user/local_example/111/222'); |
177 | is $c->action, 'user/local_example'; |
178 | is $res->content, '/user/local_example/111/222'; |
179 | is_deeply $c->req->args, [111,222]; |
180 | } |
181 | |
182 | { |
183 | my ($res, $c) = ctx_request('/user/local_example?path_prefix=1'); |
184 | is $c->action, 'user/local_example'; |
185 | is $res->content, '/local_example'; |
186 | is_deeply $c->req->args, []; |
187 | } |
188 | |
189 | { |
190 | my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1'); |
191 | is $c->action, 'user/local_example'; |
192 | is $res->content, '/local_example/111/222'; |
193 | is_deeply $c->req->args, [111,222]; |
194 | } |
195 | |
196 | { |
197 | my ($res, $c) = ctx_request('/user/local_example?env_path=1'); |
198 | is $c->action, 'user/local_example'; |
199 | is $res->content, '/'; |
200 | is_deeply $c->req->args, []; |
201 | } |
202 | |
203 | { |
204 | my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1'); |
205 | is $c->action, 'user/local_example'; |
206 | is $res->content, '/111/222'; |
207 | is_deeply $c->req->args, [111,222]; |
208 | } |
209 | |
210 | { |
211 | my ($res, $c) = ctx_request('/user/local_example?path=1'); |
212 | is $c->action, 'user/local_example'; |
213 | is $res->content, '/'; |
214 | is_deeply $c->req->args, []; |
215 | } |
216 | |
217 | { |
218 | my ($res, $c) = ctx_request('/user/local_example/111/222?path=1'); |
219 | is $c->action, 'user/local_example'; |
220 | is $res->content, '/'; |
221 | is_deeply $c->req->args, [111,222]; |
222 | } |
223 | |
224 | # END [user/local_example] |
225 | |
226 | # BEGIN [/user/local_example_args1/***/] |
227 | |
228 | { |
229 | my ($res, $c) = ctx_request('/user/local_example_args1/333'); |
230 | is $c->action, 'user/local_example_args1'; |
231 | is $res->content, '/user/local_example_args1/333'; |
232 | is_deeply $c->req->args, [333]; |
233 | } |
234 | |
235 | { |
236 | my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1'); |
237 | is $c->action, 'user/local_example_args1'; |
238 | is $res->content, '/local_example_args1/333'; |
239 | is_deeply $c->req->args, [333]; |
240 | } |
241 | |
242 | { |
243 | my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1'); |
244 | is $c->action, 'user/local_example_args1'; |
245 | is $res->content, '/333'; |
246 | is_deeply $c->req->args, [333]; |
247 | } |
248 | |
249 | { |
250 | my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1'); |
251 | is $c->action, 'user/local_example_args1'; |
252 | is $res->content, '/'; |
253 | is_deeply $c->req->args, [333]; |
254 | } |
255 | |
256 | # END [/user/local_example_args1/***/] |
257 | |
258 | # BEGIN [/user/path-example] |
259 | |
260 | { |
261 | my ($res, $c) = ctx_request('/user/path-example'); |
262 | is $c->action, 'user/path_example'; |
263 | is $res->content, '/user/path-example'; |
264 | is_deeply $c->req->args, []; |
265 | } |
266 | |
267 | { |
268 | my ($res, $c) = ctx_request('/user/path-example?path_prefix=1'); |
269 | is $c->action, 'user/path_example'; |
270 | is $res->content, '/path-example'; |
271 | is_deeply $c->req->args, []; |
272 | } |
273 | |
274 | { |
275 | my ($res, $c) = ctx_request('/user/path-example?env_path=1'); |
276 | is $c->action, 'user/path_example'; |
277 | is $res->content, '/'; |
278 | is_deeply $c->req->args, []; |
279 | } |
280 | |
281 | { |
282 | my ($res, $c) = ctx_request('/user/path-example?path=1'); |
283 | is $c->action, 'user/path_example'; |
284 | is $res->content, '/'; |
285 | is_deeply $c->req->args, []; |
286 | } |
287 | |
288 | |
289 | { |
290 | my ($res, $c) = ctx_request('/user/path-example/111/222'); |
291 | is $c->action, 'user/path_example'; |
292 | is $res->content, '/user/path-example/111/222'; |
293 | is_deeply $c->req->args, [111,222]; |
294 | } |
295 | |
296 | { |
297 | my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1'); |
298 | is $c->action, 'user/path_example'; |
299 | is $res->content, '/path-example/111/222'; |
300 | is_deeply $c->req->args, [111,222]; |
301 | } |
302 | |
303 | { |
304 | my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1'); |
305 | is $c->action, 'user/path_example'; |
306 | is $res->content, '/111/222'; |
307 | is_deeply $c->req->args, [111,222]; |
308 | } |
309 | |
310 | { |
311 | my ($res, $c) = ctx_request('/user/path-example/111/222?path=1'); |
312 | is $c->action, 'user/path_example'; |
313 | is $res->content, '/'; |
314 | is_deeply $c->req->args, [111,222]; |
315 | } |
316 | |
317 | { |
318 | my ($res, $c) = ctx_request('/user/path-example-args1/333'); |
319 | is $c->action, 'user/path_example_args1'; |
320 | is $res->content, '/user/path-example-args1/333'; |
321 | is_deeply $c->req->args, [333]; |
322 | } |
323 | |
324 | { |
325 | my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1'); |
326 | is $c->action, 'user/path_example_args1'; |
327 | is $res->content, '/path-example-args1/333'; |
328 | is_deeply $c->req->args, [333]; |
329 | } |
330 | |
331 | { |
332 | my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1'); |
333 | is $c->action, 'user/path_example_args1'; |
334 | is $res->content, '/333'; |
335 | is_deeply $c->req->args, [333]; |
336 | } |
337 | |
338 | { |
339 | my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1'); |
340 | is $c->action, 'user/path_example_args1'; |
341 | is $res->content, '/'; |
342 | is_deeply $c->req->args, [333]; |
343 | } |
344 | |
345 | # Chaining test /user/end_chain/* |
346 | # |
347 | # |
348 | |
349 | { |
350 | my ($res, $c) = ctx_request('/user/end_chain/444'); |
351 | is $c->action, 'user/end_chain'; |
352 | is $res->content, '/user/end_chain/444'; |
353 | is_deeply $c->req->args, [444]; |
354 | } |
355 | |
356 | { |
357 | my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1'); |
358 | is $c->action, 'user/end_chain'; |
359 | is $res->content, '/end_chain/444'; |
360 | is_deeply $c->req->args, [444]; |
361 | } |
362 | |
363 | { |
364 | my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1'); |
365 | is $c->action, 'user/end_chain'; |
366 | is $res->content, '/444'; |
367 | is_deeply $c->req->args, [444]; |
368 | } |
369 | |
370 | { |
371 | my ($res, $c) = ctx_request('/user/end_chain/444?path=1'); |
372 | is $c->action, 'user/end_chain'; |
373 | is $res->content, '/'; |
374 | is_deeply $c->req->args, [444]; |
375 | } |
376 | |
4477b313 |
377 | { |
378 | my ($res, $c) = ctx_request('/docs/name'); |
379 | is $c->action, 'docs/name'; |
380 | is $res->content, '/'; |
381 | is_deeply $c->req->args, []; |
382 | } |
383 | |
384 | { |
385 | my ($res, $c) = ctx_request('/docs/name/111/222'); |
386 | is $c->action, 'docs/name'; |
387 | is $res->content, '/111/222'; |
388 | is_deeply $c->req->args, [111,222]; |
389 | } |
390 | |
391 | { |
392 | my ($res, $c) = ctx_request('/docs/name_args/111'); |
393 | is $c->action, 'docs/name_args'; |
394 | is $res->content, '/111'; |
395 | is_deeply $c->req->args, [111]; |
396 | } |
9c7b6768 |
397 | |
67fd25bc |
398 | { |
399 | use utf8; |
400 | use Encode; |
401 | my ($res, $c) = ctx_request('/docs/filehandle'); |
402 | is Encode::decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n"; |
403 | } |
404 | |
405 | { |
406 | my ($res, $c) = ctx_request('/docs/direct'); |
407 | is $res->content, "helloworld"; |
408 | } |
409 | |
9c7b6768 |
410 | done_testing(); |