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(
83 if($c->req->query_parameters->{path_prefix}) {
84 return $c->Catalyst::Utils::env_at_path_prefix;
85 } elsif($c->req->query_parameters->{env_path}) {
86 return $c->Catalyst::Utils::env_at_action;
87 } elsif($c->req->query_parameters->{path}) {
88 return $c->Catalyst::Utils::env_at_request_uri;
101 use Catalyst::Test 'MyApp';
103 # BEGIN [user/local_example]
105 my ($res, $c) = ctx_request('/user/local_example');
106 is $c->action, 'user/local_example';
107 is $res->content, '/user/local_example';
108 is_deeply $c->req->args, [];
112 my ($res, $c) = ctx_request('/user/local_example/111/222');
113 is $c->action, 'user/local_example';
114 is $res->content, '/user/local_example/111/222';
115 is_deeply $c->req->args, [111,222];
119 my ($res, $c) = ctx_request('/user/local_example?path_prefix=1');
120 is $c->action, 'user/local_example';
121 is $res->content, '/local_example';
122 is_deeply $c->req->args, [];
126 my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1');
127 is $c->action, 'user/local_example';
128 is $res->content, '/local_example/111/222';
129 is_deeply $c->req->args, [111,222];
133 my ($res, $c) = ctx_request('/user/local_example?env_path=1');
134 is $c->action, 'user/local_example';
135 is $res->content, '/';
136 is_deeply $c->req->args, [];
140 my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1');
141 is $c->action, 'user/local_example';
142 is $res->content, '/111/222';
143 is_deeply $c->req->args, [111,222];
147 my ($res, $c) = ctx_request('/user/local_example?path=1');
148 is $c->action, 'user/local_example';
149 is $res->content, '/';
150 is_deeply $c->req->args, [];
154 my ($res, $c) = ctx_request('/user/local_example/111/222?path=1');
155 is $c->action, 'user/local_example';
156 is $res->content, '/';
157 is_deeply $c->req->args, [111,222];
160 # END [user/local_example]
162 # BEGIN [/user/local_example_args1/***/]
165 my ($res, $c) = ctx_request('/user/local_example_args1/333');
166 is $c->action, 'user/local_example_args1';
167 is $res->content, '/user/local_example_args1/333';
168 is_deeply $c->req->args, [333];
172 my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1');
173 is $c->action, 'user/local_example_args1';
174 is $res->content, '/local_example_args1/333';
175 is_deeply $c->req->args, [333];
179 my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1');
180 is $c->action, 'user/local_example_args1';
181 is $res->content, '/333';
182 is_deeply $c->req->args, [333];
186 my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1');
187 is $c->action, 'user/local_example_args1';
188 is $res->content, '/';
189 is_deeply $c->req->args, [333];
192 # END [/user/local_example_args1/***/]
194 # BEGIN [/user/path-example]
197 my ($res, $c) = ctx_request('/user/path-example');
198 is $c->action, 'user/path_example';
199 is $res->content, '/user/path-example';
200 is_deeply $c->req->args, [];
204 my ($res, $c) = ctx_request('/user/path-example?path_prefix=1');
205 is $c->action, 'user/path_example';
206 is $res->content, '/path-example';
207 is_deeply $c->req->args, [];
211 my ($res, $c) = ctx_request('/user/path-example?env_path=1');
212 is $c->action, 'user/path_example';
213 is $res->content, '/';
214 is_deeply $c->req->args, [];
218 my ($res, $c) = ctx_request('/user/path-example?path=1');
219 is $c->action, 'user/path_example';
220 is $res->content, '/';
221 is_deeply $c->req->args, [];
226 my ($res, $c) = ctx_request('/user/path-example/111/222');
227 is $c->action, 'user/path_example';
228 is $res->content, '/user/path-example/111/222';
229 is_deeply $c->req->args, [111,222];
233 my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1');
234 is $c->action, 'user/path_example';
235 is $res->content, '/path-example/111/222';
236 is_deeply $c->req->args, [111,222];
240 my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1');
241 is $c->action, 'user/path_example';
242 is $res->content, '/111/222';
243 is_deeply $c->req->args, [111,222];
247 my ($res, $c) = ctx_request('/user/path-example/111/222?path=1');
248 is $c->action, 'user/path_example';
249 is $res->content, '/';
250 is_deeply $c->req->args, [111,222];
254 my ($res, $c) = ctx_request('/user/path-example-args1/333');
255 is $c->action, 'user/path_example_args1';
256 is $res->content, '/user/path-example-args1/333';
257 is_deeply $c->req->args, [333];
261 my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1');
262 is $c->action, 'user/path_example_args1';
263 is $res->content, '/path-example-args1/333';
264 is_deeply $c->req->args, [333];
268 my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1');
269 is $c->action, 'user/path_example_args1';
270 is $res->content, '/333';
271 is_deeply $c->req->args, [333];
275 my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1');
276 is $c->action, 'user/path_example_args1';
277 is $res->content, '/';
278 is_deeply $c->req->args, [333];
281 # Chaining test /user/end_chain/*
286 my ($res, $c) = ctx_request('/user/end_chain/444');
287 is $c->action, 'user/end_chain';
288 is $res->content, '/user/end_chain/444';
289 is_deeply $c->req->args, [444];
293 my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
294 is $c->action, 'user/end_chain';
295 is $res->content, '/end_chain/444';
296 is_deeply $c->req->args, [444];
300 my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
301 is $c->action, 'user/end_chain';
302 is $res->content, '/444';
303 is_deeply $c->req->args, [444];
307 my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
308 is $c->action, 'user/end_chain';
309 is $res->content, '/';
310 is_deeply $c->req->args, [444];
314 my ($res, $c) = ctx_request('/docs/name');
315 is $c->action, 'docs/name';
316 is $res->content, '/';
317 is_deeply $c->req->args, [];
321 my ($res, $c) = ctx_request('/docs/name/111/222');
322 is $c->action, 'docs/name';
323 is $res->content, '/111/222';
324 is_deeply $c->req->args, [111,222];
328 my ($res, $c) = ctx_request('/docs/name_args/111');
329 is $c->action, 'docs/name_args';
330 is $res->content, '/111';
331 is_deeply $c->req->args, [111];
339 use Plack::App::URLMap;
340 use HTTP::Request::Common;
341 use HTTP::Message::PSGI;
343 my $urlmap = Plack::App::URLMap->new;
348 "REQUEST_URI: $env->{REQUEST_URI}, FROM: $env->{MAP_TO}, PATH_INFO: $env->{PATH_INFO}, SCRIPT_NAME $env->{SCRIPT_NAME}"]];
351 $urlmap->map("/" => sub { my $env = shift; $env->{MAP_TO} = '/'; $app1->($env)});
352 $urlmap->map("/foo" => sub { my $env = shift; $env->{MAP_TO} = '/foo'; $app1->($env)});
353 $urlmap->map("/bar/baz" => sub { my $env = shift; $env->{MAP_TO} = '/foo/bar'; $app1->($env)});
355 my $app = $urlmap->to_app;
357 warn $app->(req_to_psgi(GET '/'))->[2]->[0];
358 warn $app->(req_to_psgi(GET '/111'))->[2]->[0];
359 warn $app->(req_to_psgi(GET '/foo'))->[2]->[0];
360 warn $app->(req_to_psgi(GET '/foo/222'))->[2]->[0];
361 warn $app->(req_to_psgi(GET '/bar/baz'))->[2]->[0];
362 warn $app->(req_to_psgi(GET '/bar/baz/333'))->[2]->[0];