4 # Make it easier to mount PSGI apps under catalyst
7 package MyApp::Controller::User;
9 use base 'Catalyst::Controller';
14 my $req = Plack::Request->new(shift);
15 return [200,[],[$req->path]];
18 sub local_example :Local {
20 my $env = $self->get_env($c);
21 $c->res->from_psgi_response(
25 sub local_example_args1 :Local Args(1) {
27 my $env = $self->get_env($c);
28 $c->res->from_psgi_response(
32 sub path_example :Path('path-example') {
34 my $env = $self->get_env($c);
35 $c->res->from_psgi_response(
39 sub path_example_args1 :Path('path-example-args1') {
41 my $env = $self->get_env($c);
42 $c->res->from_psgi_response(
46 sub chained :Chained(/) PathPrefix CaptureArgs(0) { }
48 sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {}
50 sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1)
53 my $env = $self->get_env($c);
54 $c->res->from_psgi_response(
58 sub mounted :Local Args(1) {
59 my ($self, $c, $arg) = @_;
60 our $app ||= ref($c)->psgi_app;
61 my $env = $self->get_env($c);
62 $c->res->from_psgi_response(
66 sub mount_arg :Path(/mounted) Arg(1) {
67 my ($self, $c, $arg) = @_;
68 my $uri = $c->uri_for( $self->action_for('local_example_args1'),$arg);
69 $c->res->body("$uri");
72 sub mount_noarg :Path(/mounted_no_arg) {
74 my $uri = $c->uri_for( $self->action_for('local_example_args1'),444);
75 $c->res->body("$uri");
81 if($c->req->query_parameters->{path_prefix}) {
82 return $c->Catalyst::Utils::env_at_path_prefix;
83 } elsif($c->req->query_parameters->{env_path}) {
84 return $c->Catalyst::Utils::env_at_action;
85 } elsif($c->req->query_parameters->{path}) {
86 return $c->Catalyst::Utils::env_at_request_uri;
92 $INC{'MyApp/Controller/User.pm'} = __FILE__;
101 use Catalyst::Test 'MyApp';
104 my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1');
105 is $c->action, 'user/mounted';
106 is $res->content, 'http://localhost/user/user/local_example_args1/111';
107 is_deeply $c->req->args, [111];
111 my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1');
112 is $c->action, 'user/mounted';
113 is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444';
114 is_deeply $c->req->args, ['mounted_no_arg'];
117 # BEGIN [user/local_example]
119 my ($res, $c) = ctx_request('/user/local_example');
120 is $c->action, 'user/local_example';
121 is $res->content, '/user/local_example';
122 is_deeply $c->req->args, [];
126 my ($res, $c) = ctx_request('/user/local_example/111/222');
127 is $c->action, 'user/local_example';
128 is $res->content, '/user/local_example/111/222';
129 is_deeply $c->req->args, [111,222];
133 my ($res, $c) = ctx_request('/user/local_example?path_prefix=1');
134 is $c->action, 'user/local_example';
135 is $res->content, '/local_example';
136 is_deeply $c->req->args, [];
140 my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1');
141 is $c->action, 'user/local_example';
142 is $res->content, '/local_example/111/222';
143 is_deeply $c->req->args, [111,222];
147 my ($res, $c) = ctx_request('/user/local_example?env_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?env_path=1');
155 is $c->action, 'user/local_example';
156 is $res->content, '/111/222';
157 is_deeply $c->req->args, [111,222];
161 my ($res, $c) = ctx_request('/user/local_example?path=1');
162 is $c->action, 'user/local_example';
163 is $res->content, '/';
164 is_deeply $c->req->args, [];
168 my ($res, $c) = ctx_request('/user/local_example/111/222?path=1');
169 is $c->action, 'user/local_example';
170 is $res->content, '/';
171 is_deeply $c->req->args, [111,222];
174 # END [user/local_example]
176 # BEGIN [/user/local_example_args1/***/]
179 my ($res, $c) = ctx_request('/user/local_example_args1/333');
180 is $c->action, 'user/local_example_args1';
181 is $res->content, '/user/local_example_args1/333';
182 is_deeply $c->req->args, [333];
186 my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1');
187 is $c->action, 'user/local_example_args1';
188 is $res->content, '/local_example_args1/333';
189 is_deeply $c->req->args, [333];
193 my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1');
194 is $c->action, 'user/local_example_args1';
195 is $res->content, '/333';
196 is_deeply $c->req->args, [333];
200 my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1');
201 is $c->action, 'user/local_example_args1';
202 is $res->content, '/';
203 is_deeply $c->req->args, [333];
206 # END [/user/local_example_args1/***/]
208 # BEGIN [/user/path-example]
211 my ($res, $c) = ctx_request('/user/path-example');
212 is $c->action, 'user/path_example';
213 is $res->content, '/user/path-example';
214 is_deeply $c->req->args, [];
218 my ($res, $c) = ctx_request('/user/path-example?path_prefix=1');
219 is $c->action, 'user/path_example';
220 is $res->content, '/path-example';
221 is_deeply $c->req->args, [];
225 my ($res, $c) = ctx_request('/user/path-example?env_path=1');
226 is $c->action, 'user/path_example';
227 is $res->content, '/';
228 is_deeply $c->req->args, [];
232 my ($res, $c) = ctx_request('/user/path-example?path=1');
233 is $c->action, 'user/path_example';
234 is $res->content, '/';
235 is_deeply $c->req->args, [];
240 my ($res, $c) = ctx_request('/user/path-example/111/222');
241 is $c->action, 'user/path_example';
242 is $res->content, '/user/path-example/111/222';
243 is_deeply $c->req->args, [111,222];
247 my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1');
248 is $c->action, 'user/path_example';
249 is $res->content, '/path-example/111/222';
250 is_deeply $c->req->args, [111,222];
254 my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1');
255 is $c->action, 'user/path_example';
256 is $res->content, '/111/222';
257 is_deeply $c->req->args, [111,222];
261 my ($res, $c) = ctx_request('/user/path-example/111/222?path=1');
262 is $c->action, 'user/path_example';
263 is $res->content, '/';
264 is_deeply $c->req->args, [111,222];
268 my ($res, $c) = ctx_request('/user/path-example-args1/333');
269 is $c->action, 'user/path_example_args1';
270 is $res->content, '/user/path-example-args1/333';
271 is_deeply $c->req->args, [333];
275 my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1');
276 is $c->action, 'user/path_example_args1';
277 is $res->content, '/path-example-args1/333';
278 is_deeply $c->req->args, [333];
282 my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1');
283 is $c->action, 'user/path_example_args1';
284 is $res->content, '/333';
285 is_deeply $c->req->args, [333];
289 my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1');
290 is $c->action, 'user/path_example_args1';
291 is $res->content, '/';
292 is_deeply $c->req->args, [333];
295 # Chaining test /user/end_chain/*
300 my ($res, $c) = ctx_request('/user/end_chain/444');
301 is $c->action, 'user/end_chain';
302 is $res->content, '/user/end_chain/444';
303 is_deeply $c->req->args, [444];
307 my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
308 is $c->action, 'user/end_chain';
309 is $res->content, '/end_chain/444';
310 is_deeply $c->req->args, [444];
314 my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
315 is $c->action, 'user/end_chain';
316 is $res->content, '/444';
317 is_deeply $c->req->args, [444];
321 my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
322 is $c->action, 'user/end_chain';
323 is $res->content, '/';
324 is_deeply $c->req->args, [444];
333 use Plack::App::URLMap;
334 use HTTP::Request::Common;
335 use HTTP::Message::PSGI;
337 my $urlmap = Plack::App::URLMap->new;
342 "REQUEST_URI: $env->{REQUEST_URI}, FROM: $env->{MAP_TO}, PATH_INFO: $env->{PATH_INFO}, SCRIPT_NAME $env->{SCRIPT_NAME}"]];
345 $urlmap->map("/" => sub { my $env = shift; $env->{MAP_TO} = '/'; $app1->($env)});
346 $urlmap->map("/foo" => sub { my $env = shift; $env->{MAP_TO} = '/foo'; $app1->($env)});
347 $urlmap->map("/bar/baz" => sub { my $env = shift; $env->{MAP_TO} = '/foo/bar'; $app1->($env)});
349 my $app = $urlmap->to_app;
351 warn $app->(req_to_psgi(GET '/'))->[2]->[0];
352 warn $app->(req_to_psgi(GET '/111'))->[2]->[0];
353 warn $app->(req_to_psgi(GET '/foo'))->[2]->[0];
354 warn $app->(req_to_psgi(GET '/foo/222'))->[2]->[0];
355 warn $app->(req_to_psgi(GET '/bar/baz'))->[2]->[0];
356 warn $app->(req_to_psgi(GET '/bar/baz/333'))->[2]->[0];