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::Controller::Docs;
13 $INC{'MyApp/Controller/Docs.pm'} = __FILE__;
15 use base 'Catalyst::Controller';
21 my $env = $c->Catalyst::Utils::env_at_action;
22 $c->res->from_psgi_response(
27 sub name_args :Local Args(1) {
28 my ($self, $c, $arg) = @_;
29 my $env = $c->Catalyst::Utils::env_at_action;
30 $c->res->from_psgi_response(
34 package MyApp::Controller::User;
35 $INC{'MyApp/Controller/User.pm'} = __FILE__;
37 use base 'Catalyst::Controller';
41 sub local_example :Local {
43 my $env = $self->get_env($c);
44 $c->res->from_psgi_response(
48 sub local_example_args1 :Local Args(1) {
50 my $env = $self->get_env($c);
51 $c->res->from_psgi_response(
55 sub path_example :Path('path-example') {
57 my $env = $self->get_env($c);
58 $c->res->from_psgi_response(
62 sub path_example_args1 :Path('path-example-args1') {
64 my $env = $self->get_env($c);
65 $c->res->from_psgi_response(
69 sub chained :Chained(/) PathPrefix CaptureArgs(0) { }
71 sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {}
73 sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1)
76 my $env = $self->get_env($c);
77 $c->res->from_psgi_response(
81 sub mounted :Local Args(1) {
82 my ($self, $c, $arg) = @_;
83 our $app ||= ref($c)->psgi_app;
84 my $env = $self->get_env($c);
85 $c->res->from_psgi_response(
89 sub mount_arg :Path(/mounted) Arg(1) {
90 my ($self, $c, $arg) = @_;
91 my $uri = $c->uri_for( $self->action_for('local_example_args1'),$arg);
92 $c->res->body("$uri");
95 sub mount_noarg :Path(/mounted_no_arg) {
97 my $uri = $c->uri_for( $self->action_for('local_example_args1'),444);
98 $c->res->body("$uri");
104 if($c->req->query_parameters->{path_prefix}) {
105 return $c->Catalyst::Utils::env_at_path_prefix;
106 } elsif($c->req->query_parameters->{env_path}) {
107 return $c->Catalyst::Utils::env_at_action;
108 } elsif($c->req->query_parameters->{path}) {
109 return $c->Catalyst::Utils::env_at_request_uri;
122 use Catalyst::Test 'MyApp';
125 my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1');
126 is $c->action, 'user/mounted';
127 is $res->content, 'http://localhost/user/user/local_example_args1/111';
128 is_deeply $c->req->args, [111];
132 my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1');
133 is $c->action, 'user/mounted';
134 is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444';
135 is_deeply $c->req->args, ['mounted_no_arg'];
138 # BEGIN [user/local_example]
140 my ($res, $c) = ctx_request('/user/local_example');
141 is $c->action, 'user/local_example';
142 is $res->content, '/user/local_example';
143 is_deeply $c->req->args, [];
147 my ($res, $c) = ctx_request('/user/local_example/111/222');
148 is $c->action, 'user/local_example';
149 is $res->content, '/user/local_example/111/222';
150 is_deeply $c->req->args, [111,222];
154 my ($res, $c) = ctx_request('/user/local_example?path_prefix=1');
155 is $c->action, 'user/local_example';
156 is $res->content, '/local_example';
157 is_deeply $c->req->args, [];
161 my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1');
162 is $c->action, 'user/local_example';
163 is $res->content, '/local_example/111/222';
164 is_deeply $c->req->args, [111,222];
168 my ($res, $c) = ctx_request('/user/local_example?env_path=1');
169 is $c->action, 'user/local_example';
170 is $res->content, '/';
171 is_deeply $c->req->args, [];
175 my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1');
176 is $c->action, 'user/local_example';
177 is $res->content, '/111/222';
178 is_deeply $c->req->args, [111,222];
182 my ($res, $c) = ctx_request('/user/local_example?path=1');
183 is $c->action, 'user/local_example';
184 is $res->content, '/';
185 is_deeply $c->req->args, [];
189 my ($res, $c) = ctx_request('/user/local_example/111/222?path=1');
190 is $c->action, 'user/local_example';
191 is $res->content, '/';
192 is_deeply $c->req->args, [111,222];
195 # END [user/local_example]
197 # BEGIN [/user/local_example_args1/***/]
200 my ($res, $c) = ctx_request('/user/local_example_args1/333');
201 is $c->action, 'user/local_example_args1';
202 is $res->content, '/user/local_example_args1/333';
203 is_deeply $c->req->args, [333];
207 my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1');
208 is $c->action, 'user/local_example_args1';
209 is $res->content, '/local_example_args1/333';
210 is_deeply $c->req->args, [333];
214 my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1');
215 is $c->action, 'user/local_example_args1';
216 is $res->content, '/333';
217 is_deeply $c->req->args, [333];
221 my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1');
222 is $c->action, 'user/local_example_args1';
223 is $res->content, '/';
224 is_deeply $c->req->args, [333];
227 # END [/user/local_example_args1/***/]
229 # BEGIN [/user/path-example]
232 my ($res, $c) = ctx_request('/user/path-example');
233 is $c->action, 'user/path_example';
234 is $res->content, '/user/path-example';
235 is_deeply $c->req->args, [];
239 my ($res, $c) = ctx_request('/user/path-example?path_prefix=1');
240 is $c->action, 'user/path_example';
241 is $res->content, '/path-example';
242 is_deeply $c->req->args, [];
246 my ($res, $c) = ctx_request('/user/path-example?env_path=1');
247 is $c->action, 'user/path_example';
248 is $res->content, '/';
249 is_deeply $c->req->args, [];
253 my ($res, $c) = ctx_request('/user/path-example?path=1');
254 is $c->action, 'user/path_example';
255 is $res->content, '/';
256 is_deeply $c->req->args, [];
261 my ($res, $c) = ctx_request('/user/path-example/111/222');
262 is $c->action, 'user/path_example';
263 is $res->content, '/user/path-example/111/222';
264 is_deeply $c->req->args, [111,222];
268 my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1');
269 is $c->action, 'user/path_example';
270 is $res->content, '/path-example/111/222';
271 is_deeply $c->req->args, [111,222];
275 my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1');
276 is $c->action, 'user/path_example';
277 is $res->content, '/111/222';
278 is_deeply $c->req->args, [111,222];
282 my ($res, $c) = ctx_request('/user/path-example/111/222?path=1');
283 is $c->action, 'user/path_example';
284 is $res->content, '/';
285 is_deeply $c->req->args, [111,222];
289 my ($res, $c) = ctx_request('/user/path-example-args1/333');
290 is $c->action, 'user/path_example_args1';
291 is $res->content, '/user/path-example-args1/333';
292 is_deeply $c->req->args, [333];
296 my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1');
297 is $c->action, 'user/path_example_args1';
298 is $res->content, '/path-example-args1/333';
299 is_deeply $c->req->args, [333];
303 my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1');
304 is $c->action, 'user/path_example_args1';
305 is $res->content, '/333';
306 is_deeply $c->req->args, [333];
310 my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1');
311 is $c->action, 'user/path_example_args1';
312 is $res->content, '/';
313 is_deeply $c->req->args, [333];
316 # Chaining test /user/end_chain/*
321 my ($res, $c) = ctx_request('/user/end_chain/444');
322 is $c->action, 'user/end_chain';
323 is $res->content, '/user/end_chain/444';
324 is_deeply $c->req->args, [444];
328 my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
329 is $c->action, 'user/end_chain';
330 is $res->content, '/end_chain/444';
331 is_deeply $c->req->args, [444];
335 my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
336 is $c->action, 'user/end_chain';
337 is $res->content, '/444';
338 is_deeply $c->req->args, [444];
342 my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
343 is $c->action, 'user/end_chain';
344 is $res->content, '/';
345 is_deeply $c->req->args, [444];
349 my ($res, $c) = ctx_request('/docs/name');
350 is $c->action, 'docs/name';
351 is $res->content, '/';
352 is_deeply $c->req->args, [];
356 my ($res, $c) = ctx_request('/docs/name/111/222');
357 is $c->action, 'docs/name';
358 is $res->content, '/111/222';
359 is_deeply $c->req->args, [111,222];
363 my ($res, $c) = ctx_request('/docs/name_args/111');
364 is $c->action, 'docs/name_args';
365 is $res->content, '/111';
366 is_deeply $c->req->args, [111];
374 use Plack::App::URLMap;
375 use HTTP::Request::Common;
376 use HTTP::Message::PSGI;
378 my $urlmap = Plack::App::URLMap->new;
383 "REQUEST_URI: $env->{REQUEST_URI}, FROM: $env->{MAP_TO}, PATH_INFO: $env->{PATH_INFO}, SCRIPT_NAME $env->{SCRIPT_NAME}"]];
386 $urlmap->map("/" => sub { my $env = shift; $env->{MAP_TO} = '/'; $app1->($env)});
387 $urlmap->map("/foo" => sub { my $env = shift; $env->{MAP_TO} = '/foo'; $app1->($env)});
388 $urlmap->map("/bar/baz" => sub { my $env = shift; $env->{MAP_TO} = '/foo/bar'; $app1->($env)});
390 my $app = $urlmap->to_app;
392 warn $app->(req_to_psgi(GET '/'))->[2]->[0];
393 warn $app->(req_to_psgi(GET '/111'))->[2]->[0];
394 warn $app->(req_to_psgi(GET '/foo'))->[2]->[0];
395 warn $app->(req_to_psgi(GET '/foo/222'))->[2]->[0];
396 warn $app->(req_to_psgi(GET '/bar/baz'))->[2]->[0];
397 warn $app->(req_to_psgi(GET '/bar/baz/333'))->[2]->[0];