4 # Make it easier to mount PSGI apps under catalyst
7 my $req = Plack::Request->new(shift);
8 return [200,[],[$req->path]];
12 package MyApp::PSGIObject;
15 return [200, ['Content-Type' => 'text/plain'], ['as_psgi']];
18 package MyApp::Controller::Docs;
19 $INC{'MyApp/Controller/Docs.pm'} = __FILE__;
21 use base 'Catalyst::Controller';
27 my $as_psgi = bless +{}, 'MyApp::PSGIObject';
28 $c->res->from_psgi_response($as_psgi);
33 my $env = $c->Catalyst::Utils::env_at_action;
34 $c->res->from_psgi_response(
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(
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]);
54 my ($self, $c, $arg) = @_;
55 $c->res->from_psgi_response([200, ['Content-Type'=>'text/html'], ["hello","world"]]);
58 sub streaming_body :Local {
62 my $writer = $respond->([200,["Content-Type" => "text/plain"]]);
63 $writer->write("body");
66 $c->res->from_psgi_response($psgi_app);
68 sub streaming_body_with_charset :Local {
72 my $writer = $respond->([200,["Content-Type" => "text/plain; charset=utf-8"]]);
73 $writer->write("body");
76 $c->res->from_psgi_response($psgi_app);
79 package MyApp::Controller::User;
80 $INC{'MyApp/Controller/User.pm'} = __FILE__;
82 use base 'Catalyst::Controller';
86 sub local_example :Local {
88 my $env = $self->get_env($c);
89 $c->res->from_psgi_response(
93 sub local_example_args1 :Local Args(1) {
95 my $env = $self->get_env($c);
96 $c->res->from_psgi_response(
100 sub path_example :Path('path-example') {
102 my $env = $self->get_env($c);
103 $c->res->from_psgi_response(
107 sub path_example_args1 :Path('path-example-args1') {
109 my $env = $self->get_env($c);
110 $c->res->from_psgi_response(
114 sub chained :Chained(/) PathPrefix CaptureArgs(0) { }
116 sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {}
118 sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1)
121 my $env = $self->get_env($c);
122 $c->res->from_psgi_response(
126 sub mounted :Local Args(1) {
127 my ($self, $c, $arg) = @_;
128 our $app ||= ref($c)->psgi_app;
129 my $env = $self->get_env($c);
130 $c->res->from_psgi_response(
134 sub mount_arg :Path(/mounted) Arg(1) {
135 my ($self, $c, $arg) = @_;
136 my $uri = $c->uri_for( $self->action_for('local_example_args1'),$arg);
137 $c->res->body("$uri");
140 sub mount_noarg :Path(/mounted_no_arg) {
142 my $uri = $c->uri_for( $self->action_for('local_example_args1'),444);
143 $c->res->body("$uri");
149 if($c->req->query_parameters->{path_prefix}) {
150 return $c->Catalyst::Utils::env_at_path_prefix;
151 } elsif($c->req->query_parameters->{env_path}) {
152 return $c->Catalyst::Utils::env_at_action;
153 } elsif($c->req->query_parameters->{path}) {
154 return $c->Catalyst::Utils::env_at_request_uri;
167 use Catalyst::Test 'MyApp';
170 my ($res, $c) = ctx_request('/docs/as_psgi');
171 is $res->content, 'as_psgi';
175 my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1');
176 is $c->action, 'user/mounted';
177 is $res->content, 'http://localhost/user/user/local_example_args1/111';
178 is_deeply $c->req->args, [111];
182 my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1');
183 is $c->action, 'user/mounted';
184 is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444';
185 is_deeply $c->req->args, ['mounted_no_arg'];
188 # BEGIN [user/local_example]
190 my ($res, $c) = ctx_request('/user/local_example');
191 is $c->action, 'user/local_example';
192 is $res->content, '/user/local_example';
193 is_deeply $c->req->args, [];
197 my ($res, $c) = ctx_request('/user/local_example/111/222');
198 is $c->action, 'user/local_example';
199 is $res->content, '/user/local_example/111/222';
200 is_deeply $c->req->args, [111,222];
204 my ($res, $c) = ctx_request('/user/local_example?path_prefix=1');
205 is $c->action, 'user/local_example';
206 is $res->content, '/local_example';
207 is_deeply $c->req->args, [];
211 my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1');
212 is $c->action, 'user/local_example';
213 is $res->content, '/local_example/111/222';
214 is_deeply $c->req->args, [111,222];
218 my ($res, $c) = ctx_request('/user/local_example?env_path=1');
219 is $c->action, 'user/local_example';
220 is $res->content, '/';
221 is_deeply $c->req->args, [];
225 my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1');
226 is $c->action, 'user/local_example';
227 is $res->content, '/111/222';
228 is_deeply $c->req->args, [111,222];
232 my ($res, $c) = ctx_request('/user/local_example?path=1');
233 is $c->action, 'user/local_example';
234 is $res->content, '/';
235 is_deeply $c->req->args, [];
239 my ($res, $c) = ctx_request('/user/local_example/111/222?path=1');
240 is $c->action, 'user/local_example';
241 is $res->content, '/';
242 is_deeply $c->req->args, [111,222];
245 # END [user/local_example]
247 # BEGIN [/user/local_example_args1/***/]
250 my ($res, $c) = ctx_request('/user/local_example_args1/333');
251 is $c->action, 'user/local_example_args1';
252 is $res->content, '/user/local_example_args1/333';
253 is_deeply $c->req->args, [333];
257 my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1');
258 is $c->action, 'user/local_example_args1';
259 is $res->content, '/local_example_args1/333';
260 is_deeply $c->req->args, [333];
264 my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1');
265 is $c->action, 'user/local_example_args1';
266 is $res->content, '/333';
267 is_deeply $c->req->args, [333];
271 my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1');
272 is $c->action, 'user/local_example_args1';
273 is $res->content, '/';
274 is_deeply $c->req->args, [333];
277 # END [/user/local_example_args1/***/]
279 # BEGIN [/user/path-example]
282 my ($res, $c) = ctx_request('/user/path-example');
283 is $c->action, 'user/path_example';
284 is $res->content, '/user/path-example';
285 is_deeply $c->req->args, [];
289 my ($res, $c) = ctx_request('/user/path-example?path_prefix=1');
290 is $c->action, 'user/path_example';
291 is $res->content, '/path-example';
292 is_deeply $c->req->args, [];
296 my ($res, $c) = ctx_request('/user/path-example?env_path=1');
297 is $c->action, 'user/path_example';
298 is $res->content, '/';
299 is_deeply $c->req->args, [];
303 my ($res, $c) = ctx_request('/user/path-example?path=1');
304 is $c->action, 'user/path_example';
305 is $res->content, '/';
306 is_deeply $c->req->args, [];
311 my ($res, $c) = ctx_request('/user/path-example/111/222');
312 is $c->action, 'user/path_example';
313 is $res->content, '/user/path-example/111/222';
314 is_deeply $c->req->args, [111,222];
318 my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1');
319 is $c->action, 'user/path_example';
320 is $res->content, '/path-example/111/222';
321 is_deeply $c->req->args, [111,222];
325 my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1');
326 is $c->action, 'user/path_example';
327 is $res->content, '/111/222';
328 is_deeply $c->req->args, [111,222];
332 my ($res, $c) = ctx_request('/user/path-example/111/222?path=1');
333 is $c->action, 'user/path_example';
334 is $res->content, '/';
335 is_deeply $c->req->args, [111,222];
339 my ($res, $c) = ctx_request('/user/path-example-args1/333');
340 is $c->action, 'user/path_example_args1';
341 is $res->content, '/user/path-example-args1/333';
342 is_deeply $c->req->args, [333];
346 my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1');
347 is $c->action, 'user/path_example_args1';
348 is $res->content, '/path-example-args1/333';
349 is_deeply $c->req->args, [333];
353 my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1');
354 is $c->action, 'user/path_example_args1';
355 is $res->content, '/333';
356 is_deeply $c->req->args, [333];
360 my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1');
361 is $c->action, 'user/path_example_args1';
362 is $res->content, '/';
363 is_deeply $c->req->args, [333];
366 # Chaining test /user/end_chain/*
371 my ($res, $c) = ctx_request('/user/end_chain/444');
372 is $c->action, 'user/end_chain';
373 is $res->content, '/user/end_chain/444';
374 is_deeply $c->req->args, [444];
378 my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
379 is $c->action, 'user/end_chain';
380 is $res->content, '/end_chain/444';
381 is_deeply $c->req->args, [444];
385 my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
386 is $c->action, 'user/end_chain';
387 is $res->content, '/444';
388 is_deeply $c->req->args, [444];
392 my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
393 is $c->action, 'user/end_chain';
394 is $res->content, '/';
395 is_deeply $c->req->args, [444];
399 my ($res, $c) = ctx_request('/docs/name');
400 is $c->action, 'docs/name';
401 is $res->content, '/';
402 is_deeply $c->req->args, [];
406 my ($res, $c) = ctx_request('/docs/name/111/222');
407 is $c->action, 'docs/name';
408 is $res->content, '/111/222';
409 is_deeply $c->req->args, [111,222];
413 my ($res, $c) = ctx_request('/docs/name_args/111');
414 is $c->action, 'docs/name_args';
415 is $res->content, '/111';
416 is_deeply $c->req->args, [111];
422 my ($res, $c) = ctx_request('/docs/filehandle');
423 is Encode::decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n";
427 my ($res, $c) = ctx_request('/docs/direct');
428 is $res->content, "helloworld";
432 my ($res, $c) = ctx_request('/docs/streaming_body');
433 is $res->content, "body";
436 my ($res, $c) = ctx_request('/docs/streaming_body_with_charset');
437 is $res->content, "body";