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 package MyApp::Controller::User;
47 $INC{'MyApp/Controller/User.pm'} = __FILE__;
49 use base 'Catalyst::Controller';
53 sub local_example :Local {
55 my $env = $self->get_env($c);
56 $c->res->from_psgi_response(
60 sub local_example_args1 :Local Args(1) {
62 my $env = $self->get_env($c);
63 $c->res->from_psgi_response(
67 sub path_example :Path('path-example') {
69 my $env = $self->get_env($c);
70 $c->res->from_psgi_response(
74 sub path_example_args1 :Path('path-example-args1') {
76 my $env = $self->get_env($c);
77 $c->res->from_psgi_response(
81 sub chained :Chained(/) PathPrefix CaptureArgs(0) { }
83 sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {}
85 sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1)
88 my $env = $self->get_env($c);
89 $c->res->from_psgi_response(
93 sub mounted :Local Args(1) {
94 my ($self, $c, $arg) = @_;
95 our $app ||= ref($c)->psgi_app;
96 my $env = $self->get_env($c);
97 $c->res->from_psgi_response(
101 sub mount_arg :Path(/mounted) Arg(1) {
102 my ($self, $c, $arg) = @_;
103 my $uri = $c->uri_for( $self->action_for('local_example_args1'),$arg);
104 $c->res->body("$uri");
107 sub mount_noarg :Path(/mounted_no_arg) {
109 my $uri = $c->uri_for( $self->action_for('local_example_args1'),444);
110 $c->res->body("$uri");
116 if($c->req->query_parameters->{path_prefix}) {
117 return $c->Catalyst::Utils::env_at_path_prefix;
118 } elsif($c->req->query_parameters->{env_path}) {
119 return $c->Catalyst::Utils::env_at_action;
120 } elsif($c->req->query_parameters->{path}) {
121 return $c->Catalyst::Utils::env_at_request_uri;
134 use Catalyst::Test 'MyApp';
137 my ($res, $c) = ctx_request('/docs/as_psgi');
138 is $res->content, 'as_psgi';
142 my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1');
143 is $c->action, 'user/mounted';
144 is $res->content, 'http://localhost/user/user/local_example_args1/111';
145 is_deeply $c->req->args, [111];
149 my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1');
150 is $c->action, 'user/mounted';
151 is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444';
152 is_deeply $c->req->args, ['mounted_no_arg'];
155 # BEGIN [user/local_example]
157 my ($res, $c) = ctx_request('/user/local_example');
158 is $c->action, 'user/local_example';
159 is $res->content, '/user/local_example';
160 is_deeply $c->req->args, [];
164 my ($res, $c) = ctx_request('/user/local_example/111/222');
165 is $c->action, 'user/local_example';
166 is $res->content, '/user/local_example/111/222';
167 is_deeply $c->req->args, [111,222];
171 my ($res, $c) = ctx_request('/user/local_example?path_prefix=1');
172 is $c->action, 'user/local_example';
173 is $res->content, '/local_example';
174 is_deeply $c->req->args, [];
178 my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1');
179 is $c->action, 'user/local_example';
180 is $res->content, '/local_example/111/222';
181 is_deeply $c->req->args, [111,222];
185 my ($res, $c) = ctx_request('/user/local_example?env_path=1');
186 is $c->action, 'user/local_example';
187 is $res->content, '/';
188 is_deeply $c->req->args, [];
192 my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1');
193 is $c->action, 'user/local_example';
194 is $res->content, '/111/222';
195 is_deeply $c->req->args, [111,222];
199 my ($res, $c) = ctx_request('/user/local_example?path=1');
200 is $c->action, 'user/local_example';
201 is $res->content, '/';
202 is_deeply $c->req->args, [];
206 my ($res, $c) = ctx_request('/user/local_example/111/222?path=1');
207 is $c->action, 'user/local_example';
208 is $res->content, '/';
209 is_deeply $c->req->args, [111,222];
212 # END [user/local_example]
214 # BEGIN [/user/local_example_args1/***/]
217 my ($res, $c) = ctx_request('/user/local_example_args1/333');
218 is $c->action, 'user/local_example_args1';
219 is $res->content, '/user/local_example_args1/333';
220 is_deeply $c->req->args, [333];
224 my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1');
225 is $c->action, 'user/local_example_args1';
226 is $res->content, '/local_example_args1/333';
227 is_deeply $c->req->args, [333];
231 my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1');
232 is $c->action, 'user/local_example_args1';
233 is $res->content, '/333';
234 is_deeply $c->req->args, [333];
238 my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1');
239 is $c->action, 'user/local_example_args1';
240 is $res->content, '/';
241 is_deeply $c->req->args, [333];
244 # END [/user/local_example_args1/***/]
246 # BEGIN [/user/path-example]
249 my ($res, $c) = ctx_request('/user/path-example');
250 is $c->action, 'user/path_example';
251 is $res->content, '/user/path-example';
252 is_deeply $c->req->args, [];
256 my ($res, $c) = ctx_request('/user/path-example?path_prefix=1');
257 is $c->action, 'user/path_example';
258 is $res->content, '/path-example';
259 is_deeply $c->req->args, [];
263 my ($res, $c) = ctx_request('/user/path-example?env_path=1');
264 is $c->action, 'user/path_example';
265 is $res->content, '/';
266 is_deeply $c->req->args, [];
270 my ($res, $c) = ctx_request('/user/path-example?path=1');
271 is $c->action, 'user/path_example';
272 is $res->content, '/';
273 is_deeply $c->req->args, [];
278 my ($res, $c) = ctx_request('/user/path-example/111/222');
279 is $c->action, 'user/path_example';
280 is $res->content, '/user/path-example/111/222';
281 is_deeply $c->req->args, [111,222];
285 my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1');
286 is $c->action, 'user/path_example';
287 is $res->content, '/path-example/111/222';
288 is_deeply $c->req->args, [111,222];
292 my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1');
293 is $c->action, 'user/path_example';
294 is $res->content, '/111/222';
295 is_deeply $c->req->args, [111,222];
299 my ($res, $c) = ctx_request('/user/path-example/111/222?path=1');
300 is $c->action, 'user/path_example';
301 is $res->content, '/';
302 is_deeply $c->req->args, [111,222];
306 my ($res, $c) = ctx_request('/user/path-example-args1/333');
307 is $c->action, 'user/path_example_args1';
308 is $res->content, '/user/path-example-args1/333';
309 is_deeply $c->req->args, [333];
313 my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1');
314 is $c->action, 'user/path_example_args1';
315 is $res->content, '/path-example-args1/333';
316 is_deeply $c->req->args, [333];
320 my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1');
321 is $c->action, 'user/path_example_args1';
322 is $res->content, '/333';
323 is_deeply $c->req->args, [333];
327 my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1');
328 is $c->action, 'user/path_example_args1';
329 is $res->content, '/';
330 is_deeply $c->req->args, [333];
333 # Chaining test /user/end_chain/*
338 my ($res, $c) = ctx_request('/user/end_chain/444');
339 is $c->action, 'user/end_chain';
340 is $res->content, '/user/end_chain/444';
341 is_deeply $c->req->args, [444];
345 my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
346 is $c->action, 'user/end_chain';
347 is $res->content, '/end_chain/444';
348 is_deeply $c->req->args, [444];
352 my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
353 is $c->action, 'user/end_chain';
354 is $res->content, '/444';
355 is_deeply $c->req->args, [444];
359 my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
360 is $c->action, 'user/end_chain';
361 is $res->content, '/';
362 is_deeply $c->req->args, [444];
366 my ($res, $c) = ctx_request('/docs/name');
367 is $c->action, 'docs/name';
368 is $res->content, '/';
369 is_deeply $c->req->args, [];
373 my ($res, $c) = ctx_request('/docs/name/111/222');
374 is $c->action, 'docs/name';
375 is $res->content, '/111/222';
376 is_deeply $c->req->args, [111,222];
380 my ($res, $c) = ctx_request('/docs/name_args/111');
381 is $c->action, 'docs/name_args';
382 is $res->content, '/111';
383 is_deeply $c->req->args, [111];