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 | |
cc7738c0 |
58 | sub streaming_body :Local { |
59 | my ($self, $c) = @_; |
60 | my $psgi_app = sub { |
61 | my $respond = shift; |
62 | my $writer = $respond->([200,["Content-Type" => "text/plain"]]); |
63 | $writer->write("body"); |
64 | $writer->close; |
65 | }; |
66 | $c->res->from_psgi_response($psgi_app); |
67 | } |
68 | sub streaming_body_with_charset :Local { |
69 | my ($self, $c) = @_; |
70 | my $psgi_app = sub { |
71 | my $respond = shift; |
72 | my $writer = $respond->([200,["Content-Type" => "text/plain; charset=utf-8"]]); |
73 | $writer->write("body"); |
74 | $writer->close; |
75 | }; |
5757858f |
76 | #$c->clear_encoding; |
cc7738c0 |
77 | $c->res->from_psgi_response($psgi_app); |
78 | } |
79 | |
9c7b6768 |
80 | package MyApp::Controller::User; |
09b86ef3 |
81 | $INC{'MyApp/Controller/User.pm'} = __FILE__; |
9c7b6768 |
82 | |
83 | use base 'Catalyst::Controller'; |
84 | use Plack::Request; |
85 | use Catalyst::Utils; |
9c7b6768 |
86 | |
87 | sub local_example :Local { |
88 | my ($self, $c) = @_; |
89 | my $env = $self->get_env($c); |
90 | $c->res->from_psgi_response( |
91 | $psgi_app->($env)); |
92 | } |
93 | |
94 | sub local_example_args1 :Local Args(1) { |
95 | my ($self, $c) = @_; |
96 | my $env = $self->get_env($c); |
97 | $c->res->from_psgi_response( |
98 | $psgi_app->($env)); |
99 | } |
100 | |
101 | sub path_example :Path('path-example') { |
102 | my ($self, $c) = @_; |
103 | my $env = $self->get_env($c); |
104 | $c->res->from_psgi_response( |
105 | $psgi_app->($env)); |
106 | } |
107 | |
108 | sub path_example_args1 :Path('path-example-args1') { |
109 | my ($self, $c) = @_; |
110 | my $env = $self->get_env($c); |
111 | $c->res->from_psgi_response( |
112 | $psgi_app->($env)); |
113 | } |
114 | |
115 | sub chained :Chained(/) PathPrefix CaptureArgs(0) { } |
116 | |
117 | sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {} |
118 | |
119 | sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1) |
120 | { |
121 | my ($self, $c) = @_; |
122 | my $env = $self->get_env($c); |
123 | $c->res->from_psgi_response( |
124 | $psgi_app->($env)); |
125 | } |
d12acbe6 |
126 | |
127 | sub mounted :Local Args(1) { |
128 | my ($self, $c, $arg) = @_; |
129 | our $app ||= ref($c)->psgi_app; |
130 | my $env = $self->get_env($c); |
131 | $c->res->from_psgi_response( |
132 | $app->($env)); |
133 | } |
134 | |
135 | sub mount_arg :Path(/mounted) Arg(1) { |
136 | my ($self, $c, $arg) = @_; |
137 | my $uri = $c->uri_for( $self->action_for('local_example_args1'),$arg); |
138 | $c->res->body("$uri"); |
139 | } |
140 | |
141 | sub mount_noarg :Path(/mounted_no_arg) { |
142 | my ($self, $c) = @_; |
143 | my $uri = $c->uri_for( $self->action_for('local_example_args1'),444); |
144 | $c->res->body("$uri"); |
145 | } |
146 | |
88e5a8b0 |
147 | |
9c7b6768 |
148 | sub get_env { |
149 | my ($self, $c) = @_; |
150 | if($c->req->query_parameters->{path_prefix}) { |
151 | return $c->Catalyst::Utils::env_at_path_prefix; |
152 | } elsif($c->req->query_parameters->{env_path}) { |
153 | return $c->Catalyst::Utils::env_at_action; |
154 | } elsif($c->req->query_parameters->{path}) { |
155 | return $c->Catalyst::Utils::env_at_request_uri; |
156 | } else { |
157 | return $c->req->env; |
158 | } |
159 | } |
160 | |
9c7b6768 |
161 | package MyApp; |
162 | use Catalyst; |
163 | MyApp->setup; |
164 | |
165 | } |
166 | |
167 | use Test::More; |
168 | use Catalyst::Test 'MyApp'; |
169 | |
d12acbe6 |
170 | { |
b194746d |
171 | my ($res, $c) = ctx_request('/docs/as_psgi'); |
172 | is $res->content, 'as_psgi'; |
173 | } |
174 | |
175 | { |
d12acbe6 |
176 | my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1'); |
177 | is $c->action, 'user/mounted'; |
178 | is $res->content, 'http://localhost/user/user/local_example_args1/111'; |
179 | is_deeply $c->req->args, [111]; |
180 | } |
181 | |
182 | { |
183 | my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1'); |
184 | is $c->action, 'user/mounted'; |
185 | is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444'; |
186 | is_deeply $c->req->args, ['mounted_no_arg']; |
187 | } |
188 | |
9c7b6768 |
189 | # BEGIN [user/local_example] |
190 | { |
191 | my ($res, $c) = ctx_request('/user/local_example'); |
192 | is $c->action, 'user/local_example'; |
193 | is $res->content, '/user/local_example'; |
194 | is_deeply $c->req->args, []; |
195 | } |
196 | |
197 | { |
198 | my ($res, $c) = ctx_request('/user/local_example/111/222'); |
199 | is $c->action, 'user/local_example'; |
200 | is $res->content, '/user/local_example/111/222'; |
201 | is_deeply $c->req->args, [111,222]; |
202 | } |
203 | |
204 | { |
205 | my ($res, $c) = ctx_request('/user/local_example?path_prefix=1'); |
206 | is $c->action, 'user/local_example'; |
207 | is $res->content, '/local_example'; |
208 | is_deeply $c->req->args, []; |
209 | } |
210 | |
211 | { |
212 | my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1'); |
213 | is $c->action, 'user/local_example'; |
214 | is $res->content, '/local_example/111/222'; |
215 | is_deeply $c->req->args, [111,222]; |
216 | } |
217 | |
218 | { |
219 | my ($res, $c) = ctx_request('/user/local_example?env_path=1'); |
220 | is $c->action, 'user/local_example'; |
221 | is $res->content, '/'; |
222 | is_deeply $c->req->args, []; |
223 | } |
224 | |
225 | { |
226 | my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1'); |
227 | is $c->action, 'user/local_example'; |
228 | is $res->content, '/111/222'; |
229 | is_deeply $c->req->args, [111,222]; |
230 | } |
231 | |
232 | { |
233 | my ($res, $c) = ctx_request('/user/local_example?path=1'); |
234 | is $c->action, 'user/local_example'; |
235 | is $res->content, '/'; |
236 | is_deeply $c->req->args, []; |
237 | } |
238 | |
239 | { |
240 | my ($res, $c) = ctx_request('/user/local_example/111/222?path=1'); |
241 | is $c->action, 'user/local_example'; |
242 | is $res->content, '/'; |
243 | is_deeply $c->req->args, [111,222]; |
244 | } |
245 | |
246 | # END [user/local_example] |
247 | |
248 | # BEGIN [/user/local_example_args1/***/] |
249 | |
250 | { |
251 | my ($res, $c) = ctx_request('/user/local_example_args1/333'); |
252 | is $c->action, 'user/local_example_args1'; |
253 | is $res->content, '/user/local_example_args1/333'; |
254 | is_deeply $c->req->args, [333]; |
255 | } |
256 | |
257 | { |
258 | my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1'); |
259 | is $c->action, 'user/local_example_args1'; |
260 | is $res->content, '/local_example_args1/333'; |
261 | is_deeply $c->req->args, [333]; |
262 | } |
263 | |
264 | { |
265 | my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1'); |
266 | is $c->action, 'user/local_example_args1'; |
267 | is $res->content, '/333'; |
268 | is_deeply $c->req->args, [333]; |
269 | } |
270 | |
271 | { |
272 | my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1'); |
273 | is $c->action, 'user/local_example_args1'; |
274 | is $res->content, '/'; |
275 | is_deeply $c->req->args, [333]; |
276 | } |
277 | |
278 | # END [/user/local_example_args1/***/] |
279 | |
88e5a8b0 |
280 | # BEGIN [/user/path-example] |
9c7b6768 |
281 | |
282 | { |
283 | my ($res, $c) = ctx_request('/user/path-example'); |
284 | is $c->action, 'user/path_example'; |
285 | is $res->content, '/user/path-example'; |
286 | is_deeply $c->req->args, []; |
287 | } |
288 | |
289 | { |
290 | my ($res, $c) = ctx_request('/user/path-example?path_prefix=1'); |
291 | is $c->action, 'user/path_example'; |
292 | is $res->content, '/path-example'; |
293 | is_deeply $c->req->args, []; |
294 | } |
295 | |
296 | { |
297 | my ($res, $c) = ctx_request('/user/path-example?env_path=1'); |
298 | is $c->action, 'user/path_example'; |
299 | is $res->content, '/'; |
300 | is_deeply $c->req->args, []; |
301 | } |
302 | |
303 | { |
304 | my ($res, $c) = ctx_request('/user/path-example?path=1'); |
305 | is $c->action, 'user/path_example'; |
306 | is $res->content, '/'; |
307 | is_deeply $c->req->args, []; |
308 | } |
309 | |
310 | |
311 | { |
312 | my ($res, $c) = ctx_request('/user/path-example/111/222'); |
313 | is $c->action, 'user/path_example'; |
314 | is $res->content, '/user/path-example/111/222'; |
315 | is_deeply $c->req->args, [111,222]; |
316 | } |
317 | |
318 | { |
319 | my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1'); |
320 | is $c->action, 'user/path_example'; |
321 | is $res->content, '/path-example/111/222'; |
322 | is_deeply $c->req->args, [111,222]; |
323 | } |
324 | |
325 | { |
326 | my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1'); |
327 | is $c->action, 'user/path_example'; |
328 | is $res->content, '/111/222'; |
329 | is_deeply $c->req->args, [111,222]; |
330 | } |
331 | |
332 | { |
333 | my ($res, $c) = ctx_request('/user/path-example/111/222?path=1'); |
334 | is $c->action, 'user/path_example'; |
335 | is $res->content, '/'; |
336 | is_deeply $c->req->args, [111,222]; |
337 | } |
338 | |
339 | { |
340 | my ($res, $c) = ctx_request('/user/path-example-args1/333'); |
341 | is $c->action, 'user/path_example_args1'; |
342 | is $res->content, '/user/path-example-args1/333'; |
343 | is_deeply $c->req->args, [333]; |
344 | } |
345 | |
346 | { |
347 | my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1'); |
348 | is $c->action, 'user/path_example_args1'; |
349 | is $res->content, '/path-example-args1/333'; |
350 | is_deeply $c->req->args, [333]; |
351 | } |
352 | |
353 | { |
354 | my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1'); |
355 | is $c->action, 'user/path_example_args1'; |
356 | is $res->content, '/333'; |
357 | is_deeply $c->req->args, [333]; |
358 | } |
359 | |
360 | { |
361 | my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1'); |
362 | is $c->action, 'user/path_example_args1'; |
363 | is $res->content, '/'; |
364 | is_deeply $c->req->args, [333]; |
365 | } |
366 | |
367 | # Chaining test /user/end_chain/* |
368 | # |
369 | # |
370 | |
371 | { |
372 | my ($res, $c) = ctx_request('/user/end_chain/444'); |
373 | is $c->action, 'user/end_chain'; |
374 | is $res->content, '/user/end_chain/444'; |
375 | is_deeply $c->req->args, [444]; |
376 | } |
377 | |
378 | { |
379 | my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1'); |
380 | is $c->action, 'user/end_chain'; |
381 | is $res->content, '/end_chain/444'; |
382 | is_deeply $c->req->args, [444]; |
383 | } |
384 | |
385 | { |
386 | my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1'); |
387 | is $c->action, 'user/end_chain'; |
388 | is $res->content, '/444'; |
389 | is_deeply $c->req->args, [444]; |
390 | } |
391 | |
392 | { |
393 | my ($res, $c) = ctx_request('/user/end_chain/444?path=1'); |
394 | is $c->action, 'user/end_chain'; |
395 | is $res->content, '/'; |
396 | is_deeply $c->req->args, [444]; |
397 | } |
398 | |
4477b313 |
399 | { |
400 | my ($res, $c) = ctx_request('/docs/name'); |
401 | is $c->action, 'docs/name'; |
402 | is $res->content, '/'; |
403 | is_deeply $c->req->args, []; |
404 | } |
405 | |
406 | { |
407 | my ($res, $c) = ctx_request('/docs/name/111/222'); |
408 | is $c->action, 'docs/name'; |
409 | is $res->content, '/111/222'; |
410 | is_deeply $c->req->args, [111,222]; |
411 | } |
412 | |
413 | { |
414 | my ($res, $c) = ctx_request('/docs/name_args/111'); |
415 | is $c->action, 'docs/name_args'; |
416 | is $res->content, '/111'; |
417 | is_deeply $c->req->args, [111]; |
418 | } |
9c7b6768 |
419 | |
67fd25bc |
420 | { |
421 | use utf8; |
422 | use Encode; |
423 | my ($res, $c) = ctx_request('/docs/filehandle'); |
424 | is Encode::decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n"; |
425 | } |
426 | |
427 | { |
428 | my ($res, $c) = ctx_request('/docs/direct'); |
429 | is $res->content, "helloworld"; |
430 | } |
431 | |
cc7738c0 |
432 | { |
433 | my ($res, $c) = ctx_request('/docs/streaming_body'); |
434 | is $res->content, "body"; |
435 | } |
436 | { |
437 | my ($res, $c) = ctx_request('/docs/streaming_body_with_charset'); |
438 | is $res->content, "body"; |
439 | } |
440 | |
9c7b6768 |
441 | done_testing(); |